cpubase.pas 19 KB

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