Writing portable code regarding the processor architecture/zh CN: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 编写处理器架构无关的可移植代码 | |
| 2 | 2 | ||
| 3 | 3 | 在编写处理器架构无关的可移植代码时,主要问题有:字节序、[[32 bit|32]] 位或 [[64 bit]] 位处理器。 | |
| 4 | 4 | ||
| 5 | 5 | ## 字节序 | |
| 6 | 6 | ||
| 7 | 7 | 字节序是指处理器如何存储超过单个[[Byte|字节]]的数据(比如 16/32/64 位[[Integer|整数]])。 | |
| 8 | 8 | ||
| 9 | 9 | Free Pascal 支持两种字节序的处理器: | |
| 10 | 10 | ||
| 11 | 11 | 1. 低位存于低地址;longint(4) 编码为 04 00 00 00 ( _[小端序](<http://en.wikipedia.org/wiki/Little_endian>)_ ) | |
| 12 | 12 | 2. 高位存于地地址;longint(4) 编码为 00 00 00 04 ( _[大端序](<http://en.wikipedia.org/wiki/Big_endian>)_ ) | |
| 13 | 13 | ||
| 14 | 14 | 旁注: | |
| 15 | 15 | ||
| 16 | 16 | 中端序(也称混合序)处理器目前已很少见了。最有名的中端序处理器是 DEC 的 [PDP-11](<http://en.wikipedia.org/wiki/PDP-11>),但已作古。 | |
| 17 | 17 | ||
| 18 | 18 | 每种系列的处理器通常会固定采用一种字节序,但有些系列处理器能根据主板的不同而采用大端或小端序(如ARM、PPC)。 | |
| 19 | 19 | ||
| 20 | 20 | 最有名的小端序处理器系列就是用于 PC 的 x86,以及其兄弟 x86-64。典型的大端序处理器有 PPC(通常情况下,参阅上注)、[m68k](<http://en.wikipedia.org/wiki/68k>)、诸如 [HP3000](<http://en.wikipedia.org/wiki/HP3000>) 之类的小型机、诸如 [IBM 370](<http://en.wikipedia.org/wiki/IBM_370>)(Z 系列)之类的大型机。 | |
| 21 | 21 | ||
| 22 | 22 | 因为 TCP/IP 规定了所有网络协议头部结构都应该是大端序,所以有时大端序也被称做“网络序”。 | |
| 23 | 23 | ||
| 24 | 24 | 在以下情况下,字节序十分的重要: | |
| 25 | 25 | ||
| 26 | 26 | 1. 在不同架构的处理器之间交换数据 | |
| 27 | 27 | 2. 访问同一块数据,有时视作较大类型(如整数)的数组,有时又要视作字节数组 | |
| 28 | 28 | ||
| 29 | 29 | 以下是后一种情况的示例 | |
| 30 | 30 | ``` | |
| 31 | 31 | type | |
| 32 | 32 | Q = record | |
| 33 | 33 | case Boolean of | |
| 34 | 34 | True: (i: Integer); | |
| 35 | 35 | False: (p: array[1..4] of Byte); | |
| 36 | 36 | end; | |
| 37 | 37 | ||
| 38 | 38 | var | |
| 39 | 39 | x:^Q; | |
| 40 | 40 | ||
| 41 | 41 | begin | |
| 42 | 42 | // 首先显示一下编译器识别出来的字节序 | |
| 43 | 43 | {$IFDEF ENDIAN_LITTLE} | |
| 44 | 44 | Writeln('The compiler has compiled this program for Little Endian machines (such as Intel x86, ARMEL)'); | |
| 45 | 45 | {$ENDIF} | |
| 46 | 46 | {$IFDEF ENDIAN_BIG} | |
| 47 | 47 | Writeln('The compiler has compiled this program for Big Endian machines (such as PowerPC, ARMEB)'); | |
| 48 | 48 | {$ENDIF} | |
| 49 | 49 | New(x); | |
| 50 | 50 | x^.i := 5; | |
| 51 | 51 | if x^.p[1] = 5 then | |
| 52 | 52 | WriteLn(x^.p[1],' Your machine is Little Endian') | |
| 53 | 53 | else | |
| 54 | 54 | if x^.p[4] = 5 then | |
| 55 | 55 | WriteLn(x^.p[1],' Your machine is Big Endian') | |
| 56 | 56 | else | |
| 57 | 57 | WriteLn(x^.p[1],' ',x^.p[2],' ',x^.p[3],' ',x^.p[4],' Your machine''s endianness is indeterminate; please report the results to the compiler development team'); | |
| 58 | 58 | WriteLn; | |
| 59 | 59 | ||
| 60 | 60 | { 暂停一下,便于用户查看结果 } | |
| 61 | 61 | Write('Press Enter when you finish reading this'); | |
| 62 | 62 | ReadLn; | |
| 63 | 63 | end. | |
| 64 | 64 | ``` | |
| 65 | 65 | ||
| 66 | 66 | 在小端序机器(PC)中,上述代码将会输出 '5'(因为 longint(5) 在内存中存为 05 00 00 00),而在大端序机器上(如 Powermac)则会显示 '0'(因为 longint(5) 在内存中存为 00 00 00 05)。(如果显示 indeterminate,请在本 wiki 页报告,并注明处理器型号和显示的内容!) | |
| 67 | 67 | ||
| 68 | 68 | 若要检测处理器的字节序,请使用 ENDIAN_BIG 或 ENDIAN_LITTLE(自 1.9 版起还可用 FPC_LITTLE_ENDIAN 和 FPC_BIG_ENDIAN),Free Pascal 会自动根据处理器进行定义。 | |
| 69 | 69 | ||
| 70 | 70 | ### 改变字节序 | |
| 71 | 71 | ||
| 72 | 72 | system 单元中包含了将指定字节序转换为本机(CPU)字节序的函数,大端序([BEtoN](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/beton.html> "doc:rtl/system/beton.html")、 [NtoBE](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/ntobe.html> "doc:rtl/system/ntobe.html")),小端序为([LEtoN](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/leton.html> "doc:rtl/system/leton.html")、[NtoLE](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/ntole.html> "doc:rtl/system/ntole.html")),还有 [SwapEndian](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/swapendian.html> "doc:rtl/system/swapendian.html")。 | |
| 73 | 73 | ||
| 74 | 74 | 很多网络库(如 Synapse)都提供了自己的转换函数,以供在网络和主机之间转换字节序。 | |
| 75 | 75 | ||
| 76 | 76 | ## 内存对齐 | |
| 77 | 77 | ||
| 78 | 78 | 有些处理器允许处理未对齐的数据,只是会降低性能(IBM 370/Z 系列)。而有些处理器在数据未对齐时则会触发硬件异常(如 Alpha 和 ARM)。有时操作系统会捕获这类硬件异常并通过模拟器进行修复,但处理过程会非常缓慢,应该尽量避免。未对齐的数据还会导致记录大小不一致,因此一定要用 sizeof(记录类型); 作为记录的大小。如果定义的是 packed record,请尽量确保数据的自然对齐。有些处理器(如老款 PowerPC)只会对特定的数据类型有对齐的要求,比如浮点数。 | |
| 79 | 79 | ||
| 80 | 80 | 若要知道 CPU 是否要求数据对齐,可以检测 FPC_REQUIRES_PROPER_ALIGNMENT(1.9 及以上版本)。在32位 CPU 中,通常意味着小于 4 字节的数据必须自然对齐。 如果要访问未对齐的数据,请在处理之前用 [[move]] 将其转移至对齐的内存区域。move 函数会检查未对齐数据并进行妥善处理。 | |
| 81 | 81 | ||
| 82 | 82 | 对齐有以下多种策略: | |
| 83 | 83 | ||
| 84 | 84 | * 每个字段都对齐至某值的倍数(通常是2的幂,1、2、4、8。1 等同于 packed) | |
| 85 | 85 | * 在每个字段前填充值,确保按自身大小的倍数对齐(longint 按4字节对齐,int64 按8字节对齐,依此类推)。通常这会由 C 编译器完成,因此 FPC 称为 {$packrecords C}。 | |
| 86 | 86 | ||
| 87 | 87 | (对于 [[Array|数组]] 或是 [[nested record|嵌套记录]],会按最大子项对齐) | |
| 88 | 88 | ||
| 89 | 89 | macOS 中的 {$packrecords C} 貌似在末尾填充了整条记录,使其达到一定大小。此问题还在调查中,以后可能会在编译器中修复。 | |
| 90 | 90 | ||
| 91 | 91 | ## 32 位 和 64 位 | |
| 92 | 92 | ||
| 93 | 93 | 为了最大程度保持与老旧代码的兼容性,在从 32 位转为 64 位时,FPC 不会更改预定义数据类型的大小(如 `[[Integer|integer]]`、`[[longint]]` 或 `[[Word|word]]`)。但在 64 位架构中,指针的大小为 8 个字节,因此像 `longint([[Pointer|pointer]](p))` 这种代码必定会崩溃。但为了支持可移植代码的编写,FPC 的 system 单元引入了 `[[Ptrint|PtrInt]]` 和 `[[Ptruint|PtrUInt]]` 类型,表示有符号和无符号整数,大小与指针相同。 | |
| 94 | 94 | ||
| 95 | 95 | 请记住,修改 "pointer" 类型的大小也会影响其指向的记录大小。如果记录大小是固定的,而不是通过 [[new]] 或 [[getmem]] (<x>,[[sizeof]](<x>)) 分配内存的,那就必须对修改指针的代码进行修正。 | |
| 96 | 96 | ||
| 97 | 97 | 上述规则与大多数开放 Unix 平台一致。不过在商用领域有一些例外,比如 Tru64 和 ILP64。 | |
| 98 | 98 | ||
| 99 | 99 | ## 函数调用约定 | |
| 100 | 100 | ||
| 101 | 101 | 一般而言:请勿依赖内部代码,例如 const 参数的传入是通过栈还是直接传值。 | |
| 102 | 102 | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 103 | + | | 修饰符 | 顺序 | 栈的清理者 | 对齐方式 | 寄存器值保存吗? | | |
| 104 | + | |---|---|---|---|---| | |
| 105 | + | | none | 从左到右 | 函数 | 默认值 | 否 | | |
| 106 | + | | [[register\|Register]] | 从左到右 | 函数 | 默认值 | 否 | | |
| 107 | + | | [[cdecl\|CDecl]] | 从右到左 | 调用方 | GCC 对齐 | GCC 寄存器 | | |
| 108 | + | | [[interrupt\|Interrupt]] | 从右到左 | 函数 | 默认值 | 全部寄存器 | | |
| 109 | + | | [[pascal\|Pascal]] | 从左到右 | 函数 | 默认值 | 否 | | |
| 110 | + | | [[safecall\|SafeCall]] | 从右到左 | 函数 | 默认值 | GCC 寄存器 | | |
| 111 | + | | [[stdcall\|StdCall]] | 从右到左 | 函数 | GCC 对齐 | GCC 寄存器 | | |
| 112 | + | | [[oldfpccall\|OldFPCCall]] | 从右到左 | 调用方 | 默认值 | 否 | | |
| 113 | + | ||
| 114 | 114 | 详细信息和其他约定,请参阅 [$CALLING](<http://www.freepascal.org/docs-html/prog/progsu87.html>)。 | |
| 115 | 115 | ||
| 116 | 116 | 各种处理器架构下的大小限制 | |
| 117 | 117 | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 118 | + | | 处理器架构 | 参数 | 本地变量 | | |
| 119 | + | |---|---|---| | |
| 120 | + | | i386 | 64 KiB | 没有限制 | | |
| 121 | + | | AMD64/x86-64 | 64 KiB | 没有限制 | | |
| 122 | + | | Motorola 68000 | 32 KiB | 32 kiB | | |
| 123 | + | | Motorola 68020 | 32 KiB | 没有限制 | | |
| 124 | + | | PPC | 没有限制 | 没有限制 | | |
| 125 | + | | ARM | 没有限制 | 没有限制 | | |
| 126 | + | | SPARC | 没有限制 | 没有限制 | | |
| 127 | + | ||
| 128 | 128 | ### 大型机 | |
| 129 | 129 | ||
| 130 | 130 | 关于 IBM 370 Z 系列,更多内容请参阅 [[ZSeries]]。 | |
| 131 | 131 | ||
| 132 | 132 | ### x86 | |
| 133 | 133 | ||
| 134 | 134 | 在 x86 处理器上,通常所有参数都通过堆栈传递。但在 Free Pascal 中,首选采用 FastCall 或 Register 约定([[Target Darwin|Darwin]] 和 [[Portal:Mac|macOS]] 除外)。与 [[Delphi]] 兼容的 Register 约定规定了前三个值或单地址参数通过寄存器 EAX、EDX 和 ECX 传递。 | |
| 135 | 135 | ||
| 136 | 136 | ### ARM | |
| 137 | 137 | ||
| 138 | 138 | ### PPC | |
| 139 | 139 | ||
| 140 | 140 | 由于 PowerPC 架构拥有大量寄存器,因此大多数函数的单级调用可用寄存器传递所有参数。必要时会用栈传递其他参数。如果是多级调用且必须保存寄存器值,则一定会在栈上为基于寄存器的参数额外分配空间。 | |
| 141 | 141 | ||
| 142 | 142 | PPC 处理器采用标准的 AIX 或 SysV 调用约定。详情请参阅 [[PPC Calling conventions]]。 | |
| 143 | 143 | ||
| 144 | 144 | ### 68K | |
| 145 | 145 | ||
| 146 | 146 | 68K 处理器支持 CDecl 和 Pascal 约定。68K 栈帧模型是具有帧指针(FP)和堆栈指针(SP)的传统向下增长栈。68K 硬件对栈中的参数强制执行 16 位对齐。详情请参阅[68K 与 PowerPC 的对比](<http://physinfo-mac0.ulb.ac.be/divers_html/powerpc_programming_info/intro_to_ppc/ppc4_runtime2.html>)。 | |
| 147 | 147 | ||
| 148 | 148 | 大多数 Macintosh Toolbox 函数(经典 Mac OS 的高层函数)都推荐 Pascal 式的调用,尽管一些较新的管理器遵循 C 约定。Macintosh 操作系统函数(经典 Mac OS 的底层函数)会将调用结果放入寄存器中,也从寄存器中读取结果。 | |
| 149 | 149 | ||
| 150 | 150 | Pascal 函数刚被调用时,返回地址位于栈顶。随后函数会用 LINK 指令创建一个栈帧,并将临时寄存器中的值保存其中,因此在函数刚开始执行时的栈将如下所示: | |
| 151 | 151 | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 152 | + | | | 返回值 | | |
| 153 | + | |---|---| | |
| 154 | + | | | 第一个实参 | | |
| 155 | + | | | ... | | |
| 156 | + | | | 最后一个实参 | | |
| 157 | + | | | 指向 opt 的静态链接 | | |
| 158 | + | | | 返回地址 | | |
| 159 | + | | A6 | 前一个 A6 寄存器 | | |
| 160 | + | | | 本地变量 | | |
| 161 | + | | SP | 保存的寄存器值 | | |
| 162 | + | ||
| 163 | 163 | #### 参考 | |
| 164 | 164 | ||
| 165 | 165 | * [[THINK Pascal]] 用户手册,Symantec Corporation, Cupertino, 1988 | |
| 166 | 166 | * Physinfo, Université libre de Bruxelles: [PowerPC 函数调用约定, 68K vs. PowerPC](<http://physinfo-mac0.ulb.ac.be/divers_html/powerpc_programming_info/intro_to_ppc/ppc4_runtime2.html>) (改编自 Apple Developer University) | |
| 167 | 167 | ||
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | 170 | **navigation bar: data types** | |
| 171 | 171 | ||
| 172 | 172 | _[[simple type|simple data types]]:_ [[Boolean/zh CN|`boolean`]] [[Byte/zh CN|`byte`]] [[Cardinal/zh CN|`cardinal`]] [[Char/zh CN|`char`]] [[Currency/zh CN|`currency`]] [[Double/zh CN|`double`]] [[DWord/zh CN|`dword`]] [[Extended/zh CN|`extended`]] [[Int8/zh CN|`int8`]] [[Int16/zh CN|`int16`]] [[Int32/zh CN|`int32`]] [[Int64/zh CN|`int64`]] [[Integer/zh CN|`integer`]] [[Longint/zh CN|`longint`]] [[Real/zh CN|`real`]] [[Shortint/zh CN|`shortint`]] [[Single/zh CN|`single`]] [[Smallint/zh CN|`smallint`]] [[Pointer/zh CN|`pointer`]] [[QWord/zh CN|`qword`]] [[Word/zh CN|`word`]] | |
| 173 | 173 | ||
| 174 | 174 | _complex data types:_ [[Array/zh CN|`array`]] [[Class/zh CN|`class`]] [[Object/zh CN|`object`]] [[Record/zh CN|`record`]] [[Set/zh CN|`set`]] [[String/zh CN|`string`]] [[Shortstring/zh CN|`shortstring`]] | |
| 175 | 175 | ||
| 176 | 176 | ||
| 177 | 177 | ||
| 178 | 178 | ||
| 179 | 179 | ||
| 180 | 180 | ||
| 181 | 181 | ## 参阅 | |
| 182 | 182 | ||
| 183 | 183 | * [[Multiplatform Programming Guide]|跨平台编程指南] | |
| 184 | - | ||
| 185 | 184 | --- | |
| 186 | 185 | *From the Free Pascal wiki, page "Writing portable code regarding the processor architecture/zh CN", as the Internet Archive kept it on 2025-02-18, last changed on 2024-10-01. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |