cpubase.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. {
  2. Copyright (c) 1998-2002 by the Free Pascal dev. team
  3. Contains the base types for the virtual instruction set
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This Unit contains the base types for the Virtual Instruction machine
  18. }
  19. unit cpubase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. strings,cutils,cclasses,aasmbase,cpuinfo,cginfo;
  24. {*****************************************************************************
  25. Assembler Opcodes
  26. *****************************************************************************}
  27. type
  28. TAsmOp=(a_none,a_beqs,a_bges,a_bgts,a_bles,a_blts,a_bnes,
  29. a_bras,a_rets,a_bccs,a_bcss,a_bvcs,a_bvss,a_bbss,
  30. a_bass,a_bats,a_bbts,a_beql,a_bgel,a_bgtl,a_blel,
  31. a_bltl,a_bnel,a_bral,a_bsrl,a_bbsl,a_basl,a_batl,
  32. a_bbtl,a_add,a_addc,a_and,a_asr,a_lsl,a_lsr,a_cmp,
  33. a_sub,a_subb,a_divs,a_divu,a_mod,a_move,a_muls,a_mulu,
  34. a_neg,a_not,a_or,a_xor,a_fadd,a_fcmp,a_fdiv,a_fmove,
  35. a_fmul,a_fneg,a_fsub,a_fldd,a_flds,a_lbzx,a_lbsx,a_llsx,
  36. a_llzx,a_lwsx,a_lwzx,a_fstd,a_fsts,a_stb,a_stl,a_stw,
  37. a_syscall,a_nop,a_lims,a_orhi,a_lilo,a_call,a_popl,
  38. a_pushl,
  39. { these are simplified mnemonics }
  40. a_lea,a_limm,a_bxx
  41. );
  42. {# This should define the array of instructions as string }
  43. op2strtable=array[tasmop] of string[8];
  44. Const
  45. {# First value of opcode enumeration }
  46. firstop = low(tasmop);
  47. {# Last value of opcode enumeration }
  48. lastop = high(tasmop);
  49. {*****************************************************************************
  50. Registers
  51. *****************************************************************************}
  52. type
  53. toldregister = (R_NO,R_R0,R_R1,R_R2,R_R3,
  54. R_R4,R_R5,R_R6,R_R7,
  55. R_R8,R_R9,R_R10,R_R11,
  56. R_CCR,R_SP,R_FP,R_PC,
  57. R_FP0,R_FP1,R_FP2,R_FP3,
  58. R_FP4,R_FP5,R_FP6,R_FP7,
  59. R_FP8,R_FP9,R_FP10,R_FP11,
  60. R_FP12,R_FP13,R_FP14,R_FP15,
  61. R_INTREGISTER,R_FPUREGISTER
  62. );
  63. {# Set type definition for registers }
  64. tregisterset = set of Toldregister;
  65. Tnewregister=word;
  66. tregister=record
  67. enum:toldregister;
  68. number:Tnewregister;
  69. end;
  70. { A type to store register locations for 64 Bit values. }
  71. tregister64 = packed record
  72. reglo,reghi : tregister;
  73. end;
  74. Tsuperregister=byte;
  75. Tsubregister=byte;
  76. Tsupregset=set of Tsuperregister;
  77. { alias for compact code }
  78. treg64 = tregister64;
  79. {# Type definition for the array of string of register nnames }
  80. treg2strtable = array[toldregister] of string[5];
  81. Const
  82. {Special registers:}
  83. NR_NO = $0000; {Invalid register}
  84. {Normal registers:}
  85. {General purpose registers:}
  86. NR_R0 = $0100; NR_R1 = $0200; NR_R2 = $0300;
  87. NR_R3 = $0400; NR_R4 = $0500; NR_R5 = $0600;
  88. NR_R6 = $0700; NR_R7 = $0800; NR_R8 = $0900;
  89. NR_R9 = $0A00; NR_R10 = $0B00; NR_R11 = $0C00;
  90. NR_SP = $0D00; NR_FP = $0E00;
  91. {Super registers:}
  92. RS_R0 = $01; RS_R1 = $02; RS_R2 = $03;
  93. RS_R3 = $04; RS_R4 = $05; RS_R5 = $06;
  94. RS_R6 = $07; RS_R7 = $08; RS_R8 = $09;
  95. RS_R9 = $0A; RS_R10 = $0B; RS_R11 = $0C;
  96. RS_SP = $0D; RS_FP = $0E;
  97. {Subregisters:}
  98. R_SUBL = $00;
  99. R_SUBW = $01;
  100. R_SUBD = $02;
  101. {# First register in the tregister enumeration }
  102. firstreg = low(toldregister);
  103. {# Last register in the tregister enumeration }
  104. lastreg = high(toldregister);
  105. first_supreg = $01;
  106. last_supreg = $0c;
  107. std_reg2str : treg2strtable = ('',
  108. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','ccr',
  109. 'sp','fp','pc','fp0','fp1','fp2','fp3','fp4','fp5','fp6','fp7',
  110. 'fp8','fp9','fp10','fp11','fp12','fp13','fp14','fp15','',''
  111. );
  112. {*****************************************************************************
  113. Conditions
  114. *****************************************************************************}
  115. type
  116. TAsmCond=(C_None,
  117. C_EQ, { equal }
  118. C_NE, { not equal }
  119. C_GE, { greater or equal (signed) }
  120. C_GT, { greater than (signed) }
  121. C_LE, { less or equal (signed) }
  122. C_LT, { less than (signed) }
  123. C_LS, { lower or same (unordered) }
  124. C_AS, { above or same (unordered) }
  125. C_AT, { above than (unordered) }
  126. C_BT, { below than (unordered) }
  127. C_CC, { carry clear }
  128. C_CS { carry set }
  129. );
  130. const
  131. cond2str:array[TAsmCond] of string[3]=('',
  132. 'eq','ne','ge','gt','le','lt','ls','as',
  133. 'at','bt','cc','cs');
  134. {*****************************************************************************
  135. Flags
  136. *****************************************************************************}
  137. type
  138. TResFlags = (
  139. F_E, { zero flag = equal }
  140. F_NE, { !zero_flag = not equal }
  141. F_G, { greater (signed) }
  142. F_L, { less (signed) }
  143. F_GE,
  144. F_LE,
  145. F_C, { carry flag }
  146. F_NC, { !carry flag }
  147. F_A, { greater (unsigned) }
  148. F_AE,
  149. F_B, { less (unsigned) }
  150. F_BE
  151. );
  152. {*****************************************************************************
  153. Reference
  154. *****************************************************************************}
  155. type
  156. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  157. { reference record }
  158. preference = ^treference;
  159. treference = packed record
  160. base,
  161. index : tregister;
  162. offset : longint;
  163. symbol : tasmsymbol;
  164. offsetfixup : longint;
  165. options : trefoptions;
  166. alignment : byte;
  167. end;
  168. { reference record }
  169. pparareference = ^tparareference;
  170. tparareference = packed record
  171. index : tregister;
  172. offset : aword;
  173. end;
  174. {*****************************************************************************
  175. Operand
  176. *****************************************************************************}
  177. type
  178. toptype=(top_none,top_reg,top_ref,top_const,top_symbol,top_bool);
  179. toper=record
  180. ot : longint;
  181. case typ : toptype of
  182. top_none : ();
  183. top_reg : (reg:tregister);
  184. top_ref : (ref:^treference);
  185. top_const : (val:aword);
  186. top_symbol : (sym:tasmsymbol;symofs:longint);
  187. top_bool : (b: boolean);
  188. end;
  189. {*****************************************************************************
  190. Operand Sizes
  191. *****************************************************************************}
  192. { S_NO = No Size of operand }
  193. { S_B = 8-bit size operand }
  194. { S_W = 16-bit size operand }
  195. { S_L = 32-bit size operand }
  196. { Floating point types }
  197. { S_FS = single type (32 bit) }
  198. { S_FD = double/64bit integer }
  199. { S_FX = Extended type }
  200. topsize = (S_NO,S_B,S_W,S_L,S_FS,S_FD,S_FX,S_IQ);
  201. {*****************************************************************************
  202. Generic Location
  203. *****************************************************************************}
  204. type
  205. TLoc=(
  206. { added for tracking problems}
  207. LOC_INVALID,
  208. { ordinal constant }
  209. LOC_CONSTANT,
  210. { in a processor register }
  211. LOC_REGISTER,
  212. { Constant register which shouldn't be modified }
  213. LOC_CREGISTER,
  214. { FPU register}
  215. LOC_FPUREGISTER,
  216. { Constant FPU register which shouldn't be modified }
  217. LOC_CFPUREGISTER,
  218. { multimedia register }
  219. LOC_MMREGISTER,
  220. { Constant multimedia reg which shouldn't be modified }
  221. LOC_CMMREGISTER,
  222. { in memory }
  223. LOC_REFERENCE,
  224. { in memory (constant) }
  225. LOC_CREFERENCE,
  226. { boolean results only, jump to false or true label }
  227. LOC_JUMP,
  228. { boolean results only, flags are set }
  229. LOC_FLAGS
  230. );
  231. { tparamlocation describes where a parameter for a procedure is stored.
  232. References are given from the caller's point of view. The usual
  233. TLocation isn't used, because contains a lot of unnessary fields.
  234. }
  235. tparalocation = packed record
  236. size : TCGSize;
  237. { The location type where the parameter is passed, usually
  238. LOC_REFERENCE,LOC_REGISTER or LOC_FPUREGISTER
  239. }
  240. loc : TLoc;
  241. { The stack pointer must be decreased by this value before
  242. the parameter is copied to the given destination.
  243. This allows to "encode" pushes with tparalocation.
  244. On the PowerPC, this field is unsed but it is there
  245. because several generic code accesses it.
  246. }
  247. sp_fixup : longint;
  248. case TLoc of
  249. LOC_REFERENCE : (reference : tparareference);
  250. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  251. LOC_REGISTER,LOC_CREGISTER : (
  252. case longint of
  253. 1 : (register,register64.reghi : tregister);
  254. { overlay a register64.reglo }
  255. 2 : (register64.reglo : tregister);
  256. { overlay a 64 Bit register type }
  257. 3 : (reg64 : tregister64);
  258. 4 : (register64 : tregister64);
  259. );
  260. end;
  261. treglocation = packed record
  262. case longint of
  263. 1 : (register,register64.reghi : tregister);
  264. { overlay a register64.reglo }
  265. 2 : (register64.reglo : tregister);
  266. { overlay a 64 Bit register type }
  267. 3 : (reg64 : tregister64);
  268. 4 : (register64 : tregister64);
  269. end;
  270. tlocation = packed record
  271. size : TCGSize;
  272. loc : tloc;
  273. case tloc of
  274. LOC_CREFERENCE,LOC_REFERENCE : (reference : treference);
  275. LOC_CONSTANT : (
  276. case longint of
  277. 1 : (value : AWord);
  278. { can't do this, this layout depends on the host cpu. Use }
  279. { lo(valueqword)/hi(valueqword) instead (JM) }
  280. { 2 : (valuelow, valuehigh:AWord); }
  281. { overlay a complete 64 Bit value }
  282. 3 : (valueqword : qword);
  283. );
  284. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  285. LOC_REGISTER,LOC_CREGISTER : (
  286. case longint of
  287. 1 : (register64.reglo,register64.reghi : tregister);
  288. 2 : (register : tregister);
  289. { overlay a 64 Bit register type }
  290. 3 : (reg64 : tregister64);
  291. 4 : (register64 : tregister64);
  292. );
  293. LOC_FLAGS : (resflags : tresflags);
  294. end;
  295. {*****************************************************************************
  296. Constants
  297. *****************************************************************************}
  298. const
  299. max_operands = 2;
  300. lvaluelocations = [LOC_REFERENCE, LOC_CREGISTER, LOC_CFPUREGISTER,
  301. LOC_CMMREGISTER];
  302. {# Constant defining possibly all registers which might require saving }
  303. ALL_REGISTERS = [R_FP0..R_FP15];
  304. general_registers = [R_R0..R_R11];
  305. {# low and high of the available maximum width integer general purpose }
  306. { registers }
  307. LoGPReg = R_R0;
  308. HiGPReg = R_R11;
  309. {# low and high of every possible width general purpose register (same as }
  310. { above on most architctures apart from the 80x86) }
  311. LoReg = R_R0;
  312. HiReg = R_R11;
  313. {# Table of registers which can be allocated by the code generator
  314. internally, when generating the code.
  315. }
  316. { legend: }
  317. { xxxregs = set of all possibly used registers of that type in the code }
  318. { generator }
  319. { usableregsxxx = set of all 32bit components of registers that can be }
  320. { possible allocated to a regvar or using getregisterxxx (this }
  321. { excludes registers which can be only used for parameter }
  322. { passing on ABI's that define this) }
  323. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  324. maxintregs = 12;
  325. intregs = [R_R0..R_R11];
  326. usableregsint = [R_R2..R_R11];
  327. c_countusableregsint = 18;
  328. maxfpuregs = 16;
  329. fpuregs = [R_FP0..R_FP15];
  330. usableregsfpu = [R_FP1..R_FP15];
  331. c_countusableregsfpu = 15;
  332. mmregs = [];
  333. usableregsmm = [];
  334. c_countusableregsmm = 0;
  335. { no distinction on this platform }
  336. maxaddrregs = 0;
  337. addrregs = [];
  338. usableregsaddr = [];
  339. c_countusableregsaddr = 0;
  340. firstsaveintreg = R_R2;
  341. lastsaveintreg = R_R11;
  342. firstsavefpureg = R_FP1;
  343. lastsavefpureg = R_FP15;
  344. firstsavemmreg = R_NO;
  345. lastsavemmreg = R_NO;
  346. maxvarregs = 10;
  347. varregs : Array [1..maxvarregs] of toldregister =
  348. (R_R2,R_R3,R_R4,R_R5,R_R6,R_R7,R_R8,R_R9,R_R10,R_R11);
  349. maxfpuvarregs = 15;
  350. fpuvarregs : Array [1..maxfpuvarregs] of toldregister =
  351. (R_FP1,R_FP2,R_FP3,
  352. R_FP4,R_FP5,R_FP6,
  353. R_FP7,R_FP8,R_FP9,
  354. R_FP10,R_FP11,R_FP12,
  355. R_FP13,R_FP14,R_FP15);
  356. max_param_regs_int = 0;
  357. max_param_regs_fpu = 0;
  358. max_param_regs_mm = 0;
  359. {# Registers which are defined as scratch and no need to save across
  360. routine calls or in assembler blocks.
  361. }
  362. max_scratch_regs = 2;
  363. scratch_regs: Array[1..max_scratch_regs] of Tsuperregister = (RS_R0,RS_R1);
  364. {*****************************************************************************
  365. Default generic sizes
  366. *****************************************************************************}
  367. {# Defines the default address size for a processor, }
  368. OS_ADDR = OS_32;
  369. {# the natural int size for a processor, }
  370. OS_INT = OS_32;
  371. {# the maximum float size for a processor, }
  372. OS_FLOAT = OS_F64;
  373. {# the size of a vector register for a processor }
  374. OS_VECTOR = OS_NO;
  375. {*****************************************************************************
  376. GDB Information
  377. *****************************************************************************}
  378. {# Register indexes for stabs information, when some
  379. parameters or variables are stored in registers.
  380. Currently unsupported by abstract machine
  381. }
  382. stab_regindex : array[toldregister] of shortint =
  383. (-1,
  384. { r0..r11 }
  385. -1,-1,-1,-1,-1,-1,
  386. -1,-1,-1,-1,-1,-1,
  387. { sp,fp,ccr,pc }
  388. -1,-1,-1,-1,
  389. { FP0..FP7 }
  390. -1,-1,-1,-1,-1,-1,-1,-1,
  391. { FP8..FP15 }
  392. -1,-1,-1,-1,-1,-1,-1,-1,
  393. { invalid }
  394. -1,-1
  395. );
  396. {*****************************************************************************
  397. Generic Register names
  398. *****************************************************************************}
  399. {# Stack pointer register }
  400. stack_pointer_reg = R_SP;
  401. NR_STACK_POINTER_REG = NR_SP;
  402. RS_STACK_POINTER_REG = RS_SP;
  403. {# Frame pointer register }
  404. frame_pointer_reg = R_FP;
  405. NR_FRAME_POINTER_REG = NR_FP;
  406. RS_FRAME_POINTER_REG = RS_FP;
  407. {# Self pointer register : contains the instance address of an
  408. object or class. }
  409. self_pointer_reg = R_R11;
  410. NR_SELF_POINTER_REG = NR_R11;
  411. RS_SELF_POINTER_REG = RS_R11;
  412. {# Register for addressing absolute data in a position independant way,
  413. such as in PIC code. The exact meaning is ABI specific.
  414. }
  415. pic_offset_reg = R_R10;
  416. {# Results are returned in this register (32-bit values) }
  417. accumulator = R_R0;
  418. NR_ACCUMULATOR = NR_R0;
  419. RS_ACCUMULATOR = RS_R0;
  420. {the return_result_reg, is used inside the called function to store its return
  421. value when that is a scalar value otherwise a pointer to the address of the
  422. result is placed inside it}
  423. return_result_reg = accumulator;
  424. {the function_result_reg contains the function result after a call to a scalar
  425. function othewise it contains a pointer to the returned result}
  426. function_result_reg = accumulator;
  427. {# Hi-Results are returned in this register (64-bit value high register) }
  428. accumulatorhigh = R_R1;
  429. NR_ACCUMULATORHIGH = NR_R1;
  430. RS_ACCUMULATORHIGH = RS_R1;
  431. fpu_result_reg = R_FP0;
  432. mmresultreg = R_NO;
  433. {*****************************************************************************
  434. GCC /ABI linking information
  435. *****************************************************************************}
  436. {# Registers which must be saved when calling a routine declared as
  437. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  438. saved should be the ones as defined in the target ABI and / or GCC.
  439. This value can be deduced from CALLED_USED_REGISTERS array in the
  440. GCC source.
  441. }
  442. std_saved_registers = [RS_R0,RS_R1,RS_R10,RS_R11];
  443. {# Required parameter alignment when calling a routine declared as
  444. stdcall and cdecl. The alignment value should be the one defined
  445. by GCC or the target ABI.
  446. The value of this constant is equal to the constant
  447. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  448. }
  449. std_param_align = 4; { for 32-bit version only }
  450. {*****************************************************************************
  451. Helpers
  452. *****************************************************************************}
  453. function is_calljmp(o:tasmop):boolean;
  454. procedure inverse_flags(var r : TResFlags);
  455. function flags_to_cond(const f: TResFlags) : TAsmCond;
  456. procedure convert_register_to_enum(var r:Tregister);
  457. function cgsize2subreg(s:Tcgsize):Tsubregister;
  458. implementation
  459. uses
  460. verbose;
  461. {*****************************************************************************
  462. Helpers
  463. *****************************************************************************}
  464. function is_calljmp(o:tasmop):boolean;
  465. begin
  466. is_calljmp := false;
  467. if o in [a_bxx,a_call,a_beqs..a_bbtl] then
  468. is_calljmp := true;
  469. end;
  470. procedure inverse_flags(var r: TResFlags);
  471. const flagsinvers : array[F_E..F_BE] of tresflags =
  472. (F_NE,F_E,
  473. F_LE,F_GE,
  474. F_L,F_G,
  475. F_NC,F_C,
  476. F_BE,F_B,
  477. F_AE,F_A);
  478. begin
  479. r:=flagsinvers[r];
  480. end;
  481. function flags_to_cond(const f: TResFlags) : TAsmCond;
  482. const flags2cond : array[tresflags] of tasmcond =
  483. (
  484. {F_E} C_EQ,
  485. {F_NE} C_NE,
  486. {F_G } C_GT,
  487. {F_L } C_LT,
  488. {F_GE} C_GE,
  489. {F_LE} C_LE,
  490. {F_C} C_CS,
  491. {F_NC} C_CC,
  492. {F_A} C_AT,
  493. {F_AE} C_AS,
  494. {F_B} C_BT,
  495. {F_BE} C_LS);
  496. begin
  497. flags_to_cond := flags2cond[f];
  498. end;
  499. procedure convert_register_to_enum(var r:Tregister);
  500. begin
  501. if r.enum = R_INTREGISTER then
  502. case r.number of
  503. NR_NO: r.enum:= R_NO;
  504. NR_R0: r.enum:= R_R0;
  505. NR_R1: r.enum:= R_R1;
  506. NR_R2: r.enum:= R_R2;
  507. NR_R3: r.enum:= R_R3;
  508. NR_R4: r.enum:= R_R4;
  509. NR_R5: r.enum:= R_R5;
  510. NR_R6: r.enum:= R_R6;
  511. NR_R7: r.enum:= R_R7;
  512. NR_R8: r.enum:= R_R8;
  513. NR_R9: r.enum:= R_R9;
  514. NR_R10: r.enum:= R_R10;
  515. NR_R11: r.enum:= R_R11;
  516. else
  517. internalerror(200301082);
  518. end;
  519. end;
  520. function cgsize2subreg(s:Tcgsize):Tsubregister;
  521. begin
  522. case s of
  523. OS_8,OS_S8:
  524. cgsize2subreg:=R_SUBL;
  525. OS_16,OS_S16:
  526. cgsize2subreg:=R_SUBW;
  527. OS_32,OS_S32:
  528. cgsize2subreg:=R_SUBD;
  529. else
  530. internalerror(200301231);
  531. end;
  532. end;
  533. end.