cgobj.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the basic code generator object
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. {# @abstract(Abstract code generator unit)
  20. Abstreact code generator unit. This contains the base class
  21. to implement for all new supported processors.
  22. }
  23. unit cgobj;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cclasses,aasmbase,aasmtai,aasmcpu,symtable,
  28. cpubase,cpuinfo,cpupara,
  29. cginfo,
  30. symconst,symbase,symtype,node;
  31. type
  32. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  33. {# @abstract(Abstract code generator)
  34. This class implements an abstract instruction generator. Some of
  35. the methods of this class are generic, while others must
  36. be overriden for all new processors which will be supported
  37. by Free Pascal. For 32-bit processors, the base class
  38. sould be @link(tcg64f32) and not @var(tcg).
  39. }
  40. tcg = class
  41. scratch_register_array_pointer : aword;
  42. {# List of currently unused scratch registers }
  43. unusedscratchregisters : tregisterset;
  44. alignment : talignment;
  45. {************************************************}
  46. { basic routines }
  47. constructor create;
  48. { returns the tcgsize corresponding with the size of reg }
  49. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  50. {# Emit a label to the instruction stream. }
  51. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  52. {# Allocates register r by inserting a pai_realloc record }
  53. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  54. {# Deallocates register r by inserting a pa_regdealloc record}
  55. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  56. {# @abstract(Returns an int register for use as scratch register)
  57. This routine returns a register which can be used by
  58. the code generator as a general purpose scratch register.
  59. Since scratch_registers are scarce resources, the register
  60. should be freed by calling @link(free_scratch_reg) as
  61. soon as it is no longer required.
  62. }
  63. function get_scratch_reg_int(list : taasmoutput) : tregister;virtual;
  64. {# @abstract(Returns an address register for use as scratch register)
  65. This routine returns a register which can be used by
  66. the code generator as a pointer scratch register.
  67. Since scratch_registers are scarce resources, the register
  68. should be freed by calling @link(free_scratch_reg) as
  69. soon as it is no longer required.
  70. }
  71. function get_scratch_reg_address(list : taasmoutput) : tregister;virtual;
  72. {# @abstract(Releases a scratch register)
  73. Releases a scratch register.
  74. This routine is used to free a register which
  75. was previously allocated using @link(get_scratch_reg).
  76. }
  77. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  78. { passing parameters, per default the parameter is pushed }
  79. { nr gives the number of the parameter (enumerated from }
  80. { left to right), this allows to move the parameter to }
  81. { register, if the cpu supports register calling }
  82. { conventions }
  83. {# Pass a parameter, which is located in a register, to a routine.
  84. This routine should push/send the parameter to the routine, as
  85. required by the specific processor ABI. This must be overriden for
  86. each CPU target.
  87. @param(size size of the operand in the register)
  88. @param(r register source of the operand)
  89. @param(nr parameter number (starting from one) of routine (from left to right))
  90. }
  91. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  92. {# Pass a parameter, which is a constant, to a routine.
  93. A generic version is provided.
  94. @param(size size of the operand in constant)
  95. @param(a value of constant to send)
  96. @param(nr parameter number (starting from one) of routine (from left to right))
  97. }
  98. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  99. {# Pass the value of a parameter, which is located in memory, to a routine.
  100. A generic version is provided.
  101. @param(size size of the operand in constant)
  102. @param(r Memory reference of value to send)
  103. @param(nr parameter number (starting from one) of routine (from left to right))
  104. }
  105. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  106. {# Pass the value of a parameter, which can be located either in a register or memory location,
  107. to a routine.
  108. A generic version is provided.
  109. @param(l location of the operand to send)
  110. @param(nr parameter number (starting from one) of routine (from left to right))
  111. }
  112. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  113. {# Pass the address of a reference to a routine.
  114. A generic version is provided.
  115. @param(r reference to get address from)
  116. @param(nr parameter number (starting from one) of routine (from left to right))
  117. }
  118. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  119. {**********************************}
  120. { these methods must be overriden: }
  121. { Remarks:
  122. * If a method specifies a size you have only to take care
  123. of that number of bits, i.e. load_const_reg with OP_8 must
  124. only load the lower 8 bit of the specified register
  125. the rest of the register can be undefined
  126. if necessary the compiler will call a method
  127. to zero or sign extend the register
  128. * The a_load_XX_XX with OP_64 needn't to be
  129. implemented for 32 bit
  130. processors, the code generator takes care of that
  131. * the addr size is for work with the natural pointer
  132. size
  133. * the procedures without fpu/mm are only for integer usage
  134. * normally the first location is the source and the
  135. second the destination
  136. }
  137. {# Emits instruction to call the method specified by symbol name.
  138. This routine must be overriden for each new target cpu.
  139. }
  140. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  141. procedure a_call_ref(list : taasmoutput;const ref : treference);virtual;abstract;
  142. { move instructions }
  143. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  144. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  145. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  146. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  147. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  148. procedure a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  149. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  150. procedure a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);virtual;
  151. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  152. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  153. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);virtual; abstract;
  154. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  155. { fpu move instructions }
  156. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  157. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  158. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  159. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  160. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  161. { vector register move instructions }
  162. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  163. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  164. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  165. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  166. { basic arithmetic operations }
  167. { note: for operators which require only one argument (not, neg), use }
  168. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  169. { that in this case the *second* operand is used as both source and }
  170. { destination (JM) }
  171. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  172. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  173. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  174. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  175. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  176. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  177. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  178. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  179. { trinary operations for processors that support them, 'emulated' }
  180. { on others. None with "ref" arguments since I don't think there }
  181. { are any processors that support it (JM) }
  182. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  183. size: tcgsize; a: aword; src, dst: tregister); virtual;
  184. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  185. size: tcgsize; src1, src2, dst: tregister); virtual;
  186. { comparison operations }
  187. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  188. l : tasmlabel);virtual; abstract;
  189. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  190. l : tasmlabel); virtual;
  191. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  192. l : tasmlabel);
  193. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  194. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  195. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  196. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  197. l : tasmlabel);
  198. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  199. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  200. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  201. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  202. { some processors like the PPC doesn't allow to change the stack in }
  203. { a procedure, so we need to maintain an extra stack for the }
  204. { result values of setjmp in exception code }
  205. { this two procedures are for pushing an exception value, }
  206. { they can use the scratch registers }
  207. procedure g_push_exception(list : taasmoutput;const exceptbuf:treference;l:AWord; exceptlabel:TAsmLabel);virtual;abstract;
  208. procedure g_pop_exception(list : taasmoutput;endexceptlabel:tasmlabel);virtual;abstract;
  209. procedure g_maybe_loadself(list : taasmoutput);virtual;
  210. {# This should emit the opcode to copy len bytes from the source
  211. to destination, if loadref is true, it assumes that it first must load
  212. the source address from the memory location where
  213. source points to.
  214. It must be overriden for each new target processor.
  215. @param(source Source reference of copy)
  216. @param(dest Destination reference of copy)
  217. @param(delsource Indicates if the source reference's resources should be freed)
  218. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  219. }
  220. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  221. {# This should emit the opcode to a shortrstring from the source
  222. to destination, if loadref is true, it assumes that it first must load
  223. the source address from the memory location where
  224. source points to.
  225. @param(source Source reference of copy)
  226. @param(dest Destination reference of copy)
  227. @param(delsource Indicates if the source reference's resources should be freed)
  228. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  229. }
  230. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  231. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  232. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  233. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  234. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  235. {# Emits the call to the stack checking routine of
  236. the runtime library. The default behavior
  237. does not need to be modified, as it is generic
  238. for all platforms.
  239. }
  240. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  241. {# Generates range checking code. It is to note
  242. that this routine does not need to be overriden,
  243. as it takes care of everything.
  244. @param(p Node which contains the value to check)
  245. @param(todef Type definition of node to range check)
  246. }
  247. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  248. const todef: tdef); virtual;
  249. { generates overflow checking code for a node }
  250. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  251. {**********************************}
  252. { entry/exit code helpers }
  253. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer); virtual; abstract;
  254. {# Emits instructions which should be emitted when entering
  255. a routine declared as @var(interrupt). The default
  256. behavior does nothing, should be overriden as required.
  257. }
  258. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  259. {# Emits instructions which should be emitted when exiting
  260. a routine declared as @var(interrupt). The default
  261. behavior does nothing, should be overriden as required.
  262. }
  263. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;
  264. {# Emits instructions when compilation is done in profile
  265. mode (this is set as a command line option). The default
  266. behavior does nothing, should be overriden as required.
  267. }
  268. procedure g_profilecode(list : taasmoutput);virtual;
  269. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  270. { restores the frame pointer at procedure exit }
  271. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  272. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  273. procedure g_call_constructor_helper(list : taasmoutput);virtual;
  274. procedure g_call_destructor_helper(list : taasmoutput);virtual;
  275. procedure g_call_fail_helper(list : taasmoutput);virtual;
  276. procedure g_save_standard_registers(list : taasmoutput);virtual;abstract;
  277. procedure g_restore_standard_registers(list : taasmoutput);virtual;abstract;
  278. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  279. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;abstract;
  280. end;
  281. {# @abstract(Abstract code generator for 64 Bit operations)
  282. This class implements an abstract code generator class
  283. for 64 Bit operations.
  284. }
  285. tcg64 = class
  286. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  287. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  288. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  289. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  290. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  291. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  292. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  293. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  294. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  295. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  296. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  297. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  298. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  299. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  300. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  301. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  302. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  303. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  304. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  305. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  306. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  307. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  308. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  309. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  310. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  311. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  312. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  313. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  314. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  315. { override to catch 64bit rangechecks }
  316. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  317. const todef: tdef);virtual;abstract;
  318. end;
  319. var
  320. {# Main code generator class }
  321. cg : tcg;
  322. {# Code generator class for all operations working with 64-Bit operands }
  323. cg64 : tcg64;
  324. implementation
  325. uses
  326. globals,globtype,options,systems,cgbase,
  327. verbose,defbase,tgobj,symdef,paramgr,
  328. rgobj;
  329. const
  330. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  331. {*****************************************************************************
  332. basic functionallity
  333. ******************************************************************************}
  334. constructor tcg.create;
  335. var
  336. i : longint;
  337. begin
  338. scratch_register_array_pointer:=1;
  339. for i:=low(scratch_regs) to high(scratch_regs) do
  340. include(unusedscratchregisters,scratch_regs[i]);
  341. end;
  342. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  343. begin
  344. list.concat(tai_regalloc.alloc(r));
  345. end;
  346. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  347. begin
  348. list.concat(tai_regalloc.dealloc(r));
  349. end;
  350. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  351. begin
  352. list.concat(tai_label.create(l));
  353. end;
  354. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  355. var
  356. r : tregister;
  357. i : longint;
  358. begin
  359. if unusedscratchregisters=[] then
  360. internalerror(68996);
  361. for i:=scratch_register_array_pointer to
  362. (scratch_register_array_pointer+max_scratch_regs-1) do
  363. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  364. begin
  365. r:=scratch_regs[(i mod max_scratch_regs)+1];
  366. break;
  367. end;
  368. exclude(unusedscratchregisters,r);
  369. inc(scratch_register_array_pointer);
  370. if scratch_register_array_pointer>max_scratch_regs then
  371. scratch_register_array_pointer:=1;
  372. a_reg_alloc(list,r);
  373. get_scratch_reg_int:=r;
  374. end;
  375. { the default behavior simply returns a general purpose register }
  376. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  377. begin
  378. get_scratch_reg_address := get_scratch_reg_int(list);
  379. end;
  380. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  381. begin
  382. include(unusedscratchregisters,rg.makeregsize(r,OS_INT));
  383. a_reg_dealloc(list,r);
  384. end;
  385. {*****************************************************************************
  386. for better code generation these methods should be overridden
  387. ******************************************************************************}
  388. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  389. var
  390. ref : treference;
  391. begin
  392. case locpara.loc of
  393. LOC_REGISTER:
  394. a_load_reg_reg(list,size,r,locpara.register);
  395. LOC_REFERENCE:
  396. begin
  397. reference_reset(ref);
  398. ref.base:=locpara.reference.index;
  399. ref.offset:=locpara.reference.offset;
  400. a_load_reg_ref(list,size,r,ref);
  401. {!!!! FIX ME!, take sp_fixup into account }
  402. internalerror(2002071005);
  403. end
  404. else
  405. internalerror(2002071004);
  406. end;
  407. end;
  408. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  409. var
  410. hr : tregister;
  411. begin
  412. hr:=get_scratch_reg_int(list);
  413. a_load_const_reg(list,size,a,hr);
  414. a_param_reg(list,size,hr,locpara);
  415. free_scratch_reg(list,hr);
  416. end;
  417. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  418. var
  419. hr : tregister;
  420. begin
  421. hr:=get_scratch_reg_int(list);
  422. a_load_ref_reg(list,size,r,hr);
  423. a_param_reg(list,size,hr,locpara);
  424. free_scratch_reg(list,hr);
  425. end;
  426. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  427. begin
  428. case l.loc of
  429. LOC_REGISTER,
  430. LOC_CREGISTER :
  431. a_param_reg(list,l.size,l.register,locpara);
  432. LOC_CONSTANT :
  433. a_param_const(list,l.size,l.value,locpara);
  434. LOC_CREFERENCE,
  435. LOC_REFERENCE :
  436. a_param_ref(list,l.size,l.reference,locpara);
  437. else
  438. internalerror(2002032211);
  439. end;
  440. end;
  441. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  442. var
  443. hr : tregister;
  444. begin
  445. hr:=get_scratch_reg_address(list);
  446. a_loadaddr_ref_reg(list,r,hr);
  447. a_param_reg(list,OS_ADDR,hr,locpara);
  448. free_scratch_reg(list,hr);
  449. end;
  450. {****************************************************************************
  451. some generic implementations
  452. ****************************************************************************}
  453. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  454. var
  455. tmpreg: tregister;
  456. {$ifdef i386}
  457. pushed_reg: tregister;
  458. {$endif i386}
  459. begin
  460. {$ifdef i386}
  461. { the following is done with defines to avoid a speed penalty, }
  462. { since all this is only necessary for the 80x86 (because EDI }
  463. { doesn't have an 8bit component which is directly addressable) }
  464. pushed_reg := R_NO;
  465. if size in [OS_8,OS_S8] then
  466. if (rg.countunusedregsint = 0) then
  467. begin
  468. if (dref.base <> R_EBX) and
  469. (dref.index <> R_EBX) then
  470. pushed_reg := R_EBX
  471. else if (dref.base <> R_EAX) and
  472. (dref.index <> R_EAX) then
  473. pushed_reg := R_EAX
  474. else pushed_reg := R_ECX;
  475. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  476. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  477. end
  478. else
  479. tmpreg := rg.getregisterint(exprasmlist)
  480. else
  481. {$endif i386}
  482. tmpreg := get_scratch_reg_int(list);
  483. tmpreg:=rg.makeregsize(tmpreg,size);
  484. a_load_ref_reg(list,size,sref,tmpreg);
  485. a_load_reg_ref(list,size,tmpreg,dref);
  486. {$ifdef i386}
  487. if size in [OS_8,OS_S8] then
  488. begin
  489. if (pushed_reg <> R_NO) then
  490. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  491. else
  492. rg.ungetregister(exprasmlist,tmpreg)
  493. end
  494. else
  495. {$endif i386}
  496. free_scratch_reg(list,tmpreg);
  497. end;
  498. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  499. var
  500. tmpreg: tregister;
  501. begin
  502. tmpreg := get_scratch_reg_int(list);
  503. a_load_const_reg(list,size,a,tmpreg);
  504. a_load_reg_ref(list,size,tmpreg,ref);
  505. free_scratch_reg(list,tmpreg);
  506. end;
  507. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  508. begin
  509. case loc.loc of
  510. LOC_REFERENCE,LOC_CREFERENCE:
  511. a_load_const_ref(list,loc.size,a,loc.reference);
  512. LOC_REGISTER,LOC_CREGISTER:
  513. a_load_const_reg(list,loc.size,a,loc.register);
  514. else
  515. internalerror(200203272);
  516. end;
  517. end;
  518. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  519. begin
  520. case loc.loc of
  521. LOC_REFERENCE,LOC_CREFERENCE:
  522. a_load_reg_ref(list,size,reg,loc.reference);
  523. LOC_REGISTER,LOC_CREGISTER:
  524. a_load_reg_reg(list,size,reg,loc.register);
  525. else
  526. internalerror(200203271);
  527. end;
  528. end;
  529. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  530. begin
  531. case loc.loc of
  532. LOC_REFERENCE,LOC_CREFERENCE:
  533. a_load_ref_reg(list,loc.size,loc.reference,reg);
  534. LOC_REGISTER,LOC_CREGISTER:
  535. a_load_reg_reg(list,loc.size,loc.register,reg);
  536. LOC_CONSTANT:
  537. a_load_const_reg(list,loc.size,loc.value,reg);
  538. else
  539. begin
  540. writeln(ord(loc.loc));
  541. runerror(216);
  542. internalerror(200109092);
  543. end;
  544. end;
  545. end;
  546. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  547. begin
  548. case loc.loc of
  549. LOC_REFERENCE,LOC_CREFERENCE:
  550. a_load_ref_ref(list,loc.size,loc.reference,ref);
  551. LOC_REGISTER,LOC_CREGISTER:
  552. a_load_reg_ref(list,loc.size,loc.register,ref);
  553. LOC_CONSTANT:
  554. a_load_const_ref(list,loc.size,loc.value,ref);
  555. else
  556. internalerror(200109302);
  557. end;
  558. end;
  559. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  560. begin
  561. case loc.loc of
  562. LOC_REFERENCE, LOC_CREFERENCE:
  563. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  564. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  565. a_loadfpu_reg_reg(list,loc.register,reg);
  566. else
  567. internalerror(200203301);
  568. end;
  569. end;
  570. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  571. begin
  572. case loc.loc of
  573. LOC_REFERENCE, LOC_CREFERENCE:
  574. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  575. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  576. a_loadfpu_reg_reg(list,reg,loc.register);
  577. else
  578. internalerror(48991);
  579. end;
  580. end;
  581. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  582. var
  583. tmpreg: tregister;
  584. begin
  585. tmpreg := get_scratch_reg_int(list);
  586. a_load_ref_reg(list,size,ref,tmpreg);
  587. a_op_const_reg(list,op,a,tmpreg);
  588. a_load_reg_ref(list,size,tmpreg,ref);
  589. free_scratch_reg(list,tmpreg);
  590. end;
  591. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  592. begin
  593. case loc.loc of
  594. LOC_REGISTER, LOC_CREGISTER:
  595. a_op_const_reg(list,op,a,loc.register);
  596. LOC_REFERENCE, LOC_CREFERENCE:
  597. a_op_const_ref(list,op,loc.size,a,loc.reference);
  598. else
  599. internalerror(200109061);
  600. end;
  601. end;
  602. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  603. var
  604. tmpreg: tregister;
  605. begin
  606. tmpreg := get_scratch_reg_int(list);
  607. a_load_ref_reg(list,size,ref,tmpreg);
  608. a_op_reg_reg(list,op,size,reg,tmpreg);
  609. a_load_reg_ref(list,size,tmpreg,ref);
  610. free_scratch_reg(list,tmpreg);
  611. end;
  612. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  613. var
  614. tmpreg: tregister;
  615. begin
  616. case op of
  617. OP_NOT,OP_NEG:
  618. { handle it as "load ref,reg; op reg" }
  619. begin
  620. a_load_ref_reg(list,size,ref,reg);
  621. a_op_reg_reg(list,op,size,reg,reg);
  622. end;
  623. else
  624. begin
  625. tmpreg := get_scratch_reg_int(list);
  626. a_load_ref_reg(list,size,ref,tmpreg);
  627. a_op_reg_reg(list,op,size,tmpreg,reg);
  628. free_scratch_reg(list,tmpreg);
  629. end;
  630. end;
  631. end;
  632. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  633. begin
  634. case loc.loc of
  635. LOC_REGISTER, LOC_CREGISTER:
  636. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  637. LOC_REFERENCE, LOC_CREFERENCE:
  638. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  639. else
  640. internalerror(200109061);
  641. end;
  642. end;
  643. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  644. var
  645. tmpreg: tregister;
  646. begin
  647. case loc.loc of
  648. LOC_REGISTER,LOC_CREGISTER:
  649. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  650. LOC_REFERENCE,LOC_CREFERENCE:
  651. begin
  652. tmpreg := get_scratch_reg_int(list);
  653. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  654. a_load_ref_reg(list,loc.size,ref,tmpreg);
  655. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  656. free_scratch_reg(list,tmpreg);
  657. end;
  658. else
  659. internalerror(200109061);
  660. end;
  661. end;
  662. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  663. size: tcgsize; a: aword; src, dst: tregister);
  664. begin
  665. a_load_reg_reg(list,size,src,dst);
  666. a_op_const_reg(list,op,a,dst);
  667. end;
  668. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  669. size: tcgsize; src1, src2, dst: tregister);
  670. begin
  671. a_load_reg_reg(list,size,src2,dst);
  672. a_op_reg_reg(list,op,size,src1,dst);
  673. end;
  674. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  675. l : tasmlabel);
  676. var
  677. tmpreg: tregister;
  678. begin
  679. tmpreg := get_scratch_reg_int(list);
  680. a_load_ref_reg(list,size,ref,tmpreg);
  681. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  682. free_scratch_reg(list,tmpreg);
  683. end;
  684. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  685. l : tasmlabel);
  686. begin
  687. case loc.loc of
  688. LOC_REGISTER,LOC_CREGISTER:
  689. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  690. LOC_REFERENCE,LOC_CREFERENCE:
  691. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  692. else
  693. internalerror(200109061);
  694. end;
  695. end;
  696. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  697. var
  698. tmpreg: tregister;
  699. begin
  700. tmpreg := get_scratch_reg_int(list);
  701. a_load_ref_reg(list,size,ref,tmpreg);
  702. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  703. free_scratch_reg(list,tmpreg);
  704. end;
  705. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  706. begin
  707. case loc.loc of
  708. LOC_REGISTER,
  709. LOC_CREGISTER:
  710. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  711. LOC_REFERENCE,
  712. LOC_CREFERENCE :
  713. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  714. LOC_CONSTANT:
  715. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  716. else
  717. internalerror(200203231);
  718. end;
  719. end;
  720. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  721. l : tasmlabel);
  722. var
  723. tmpreg: tregister;
  724. begin
  725. case loc.loc of
  726. LOC_REGISTER,LOC_CREGISTER:
  727. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  728. LOC_REFERENCE,LOC_CREFERENCE:
  729. begin
  730. {$ifdef i386}
  731. { the following is done with defines to avoid a speed penalty, }
  732. { since all this is only necessary for the 80x86 (because EDI }
  733. { doesn't have an 8bit component which is directly addressable) }
  734. if size in [OS_8,OS_S8] then
  735. tmpreg := rg.getregisterint(exprasmlist)
  736. else
  737. {$endif i386}
  738. tmpreg := get_scratch_reg_int(list);
  739. tmpreg := rg.makeregsize(tmpreg,size);
  740. a_load_ref_reg(list,size,loc.reference,tmpreg);
  741. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  742. {$ifdef i386}
  743. if size in [OS_8,OS_S8] then
  744. rg.ungetregister(exprasmlist,tmpreg)
  745. else
  746. {$endif i386}
  747. free_scratch_reg(list,tmpreg);
  748. end
  749. else
  750. internalerror(200109061);
  751. end;
  752. end;
  753. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  754. begin
  755. reg_cgsize := OS_INT;
  756. end;
  757. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  758. begin
  759. {$warning FIX ME!}
  760. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  761. if loadref then
  762. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  763. else
  764. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  765. if delsource then
  766. reference_release(list,source);
  767. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  768. a_call_name(list,'FPC_SHORTSTR_COPY');
  769. g_maybe_loadself(list);
  770. end;
  771. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  772. var
  773. href : treference;
  774. incrfunc : string;
  775. begin
  776. { These functions should not change the registers (they use
  777. the saveregister proc directive }
  778. if is_interfacecom(t) then
  779. incrfunc:='FPC_INTF_INCR_REF'
  780. else if is_ansistring(t) then
  781. incrfunc:='FPC_ANSISTR_INCR_REF'
  782. else if is_widestring(t) then
  783. incrfunc:='FPC_WIDESTR_INCR_REF'
  784. else if is_dynamic_array(t) then
  785. incrfunc:='FPC_DYNARRAY_INCR_REF'
  786. else
  787. incrfunc:='';
  788. { call the special incr function or the generic addref }
  789. if incrfunc<>'' then
  790. begin
  791. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  792. a_call_name(list,incrfunc);
  793. end
  794. else
  795. begin
  796. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  797. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  798. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  799. a_call_name(list,'FPC_ADDREF');
  800. end;
  801. end;
  802. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  803. var
  804. href : treference;
  805. decrfunc : string;
  806. begin
  807. if is_interfacecom(t) then
  808. decrfunc:='FPC_INTF_DECR_REF'
  809. else if is_ansistring(t) then
  810. decrfunc:='FPC_ANSISTR_DECR_REF'
  811. else if is_widestring(t) then
  812. decrfunc:='FPC_WIDESTR_DECR_REF'
  813. else if is_dynamic_array(t) then
  814. decrfunc:='FPC_DYNARRAY_INCR_REF'
  815. else
  816. decrfunc:='';
  817. { call the special decr function or the generic decref }
  818. if decrfunc<>'' then
  819. begin
  820. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  821. a_call_name(list,decrfunc);
  822. end
  823. else
  824. begin
  825. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  826. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  827. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  828. a_call_name(list,'FPC_DECREF');
  829. end;
  830. end;
  831. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  832. var
  833. href : treference;
  834. begin
  835. if is_ansistring(t) or
  836. is_widestring(t) or
  837. is_interfacecom(t) then
  838. a_load_const_ref(list,OS_ADDR,0,ref)
  839. else
  840. begin
  841. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  842. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  843. if loadref then
  844. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  845. else
  846. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  847. a_call_name(list,'FPC_INITIALIZE');
  848. end;
  849. end;
  850. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  851. var
  852. href : treference;
  853. begin
  854. if is_ansistring(t) or
  855. is_widestring(t) or
  856. is_interfacecom(t) then
  857. g_decrrefcount(list,t,ref)
  858. else
  859. begin
  860. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  861. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  862. if loadref then
  863. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  864. else
  865. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  866. a_call_name(list,'FPC_FINALIZE');
  867. end;
  868. end;
  869. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  870. { generate range checking code for the value at location p. The type }
  871. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  872. { is the original type used at that location. When both defs are equal }
  873. { the check is also insert (needed for succ,pref,inc,dec) }
  874. const
  875. {$ifdef ver1_0}
  876. awordsignedmax=high(longint);
  877. {$else}
  878. awordsignedmax=high(aword) div 2;
  879. {$endif}
  880. var
  881. neglabel : tasmlabel;
  882. hreg : tregister;
  883. fromdef : tdef;
  884. lto,hto,
  885. lfrom,hfrom : TConstExprInt;
  886. from_signed: boolean;
  887. begin
  888. { range checking on and range checkable value? }
  889. if not(cs_check_range in aktlocalswitches) or
  890. not(todef.deftype in [orddef,enumdef,arraydef]) then
  891. exit;
  892. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  893. begin
  894. cg64.g_rangecheck64(list,p,todef);
  895. exit;
  896. end;
  897. { only check when assigning to scalar, subranges are different, }
  898. { when todef=fromdef then the check is always generated }
  899. fromdef:=p.resulttype.def;
  900. getrange(p.resulttype.def,lfrom,hfrom);
  901. getrange(todef,lto,hto);
  902. { no range check if from and to are equal and are both longint/dword }
  903. { (if we have a 32bit processor) or int64/qword, since such }
  904. { operations can at most cause overflows (JM) }
  905. { Note that these checks are mostly processor independent, they only }
  906. { have to be changed once we introduce 64bit subrange types }
  907. if (fromdef = todef) and
  908. (fromdef.deftype=orddef) and
  909. (((sizeof(aword) = 4) and
  910. (((torddef(fromdef).typ = s32bit) and
  911. (lfrom = low(longint)) and
  912. (hfrom = high(longint))) or
  913. ((torddef(fromdef).typ = u32bit) and
  914. (lfrom = low(cardinal)) and
  915. (hfrom = high(cardinal)))))) then
  916. exit;
  917. if todef<>fromdef then
  918. begin
  919. { if the from-range falls completely in the to-range, no check }
  920. { is necessary }
  921. if (lto<=lfrom) and (hto>=hfrom) then
  922. exit;
  923. end;
  924. { generate the rangecheck code for the def where we are going to }
  925. { store the result }
  926. { use the trick that }
  927. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  928. { To be able to do that, we have to make sure however that either }
  929. { fromdef and todef are both signed or unsigned, or that we leave }
  930. { the parts < 0 and > maxlongint out }
  931. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  932. from_signed := is_signed(fromdef);
  933. if from_signed xor is_signed(todef) then
  934. if from_signed then
  935. { from is signed, to is unsigned }
  936. begin
  937. { if high(from) < 0 -> always range error }
  938. if (hfrom < 0) or
  939. { if low(to) > maxlongint also range error }
  940. (lto > awordsignedmax) then
  941. begin
  942. a_call_name(list,'FPC_RANGEERROR');
  943. exit
  944. end;
  945. { from is signed and to is unsigned -> when looking at from }
  946. { as an unsigned value, it must be < maxlongint (otherwise }
  947. { it's negative, which is invalid since "to" is unsigned) }
  948. if hto > awordsignedmax then
  949. hto := awordsignedmax;
  950. end
  951. else
  952. { from is unsigned, to is signed }
  953. begin
  954. if (lfrom > awordsignedmax) or
  955. (hto < 0) then
  956. begin
  957. a_call_name(list,'FPC_RANGEERROR');
  958. exit
  959. end;
  960. { from is unsigned and to is signed -> when looking at to }
  961. { as an unsigned value, it must be >= 0 (since negative }
  962. { values are the same as values > maxlongint) }
  963. if lto < 0 then
  964. lto := 0;
  965. end;
  966. hreg := get_scratch_reg_int(list);
  967. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  968. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  969. aword(lto),p.location.register,hreg)
  970. else
  971. begin
  972. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  973. p.location.reference,hreg);
  974. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  975. end;
  976. getlabel(neglabel);
  977. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  978. { !!! should happen right after the compare (JM) }
  979. free_scratch_reg(list,hreg);
  980. a_call_name(list,'FPC_RANGEERROR');
  981. a_label(list,neglabel);
  982. end;
  983. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  984. begin
  985. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  986. a_call_name(list,'FPC_STACKCHECK');
  987. end;
  988. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  989. var
  990. tmpreg : tregister;
  991. begin
  992. tmpreg := get_scratch_reg_int(list);
  993. g_flags2reg(list,size,f,tmpreg);
  994. a_load_reg_ref(list,size,tmpreg,ref);
  995. free_scratch_reg(list,tmpreg);
  996. end;
  997. procedure tcg.g_maybe_loadself(list : taasmoutput);
  998. var
  999. hp : treference;
  1000. p : pprocinfo;
  1001. i : longint;
  1002. begin
  1003. if assigned(procinfo^._class) then
  1004. begin
  1005. list.concat(tai_regalloc.Alloc(SELF_POINTER_REG));
  1006. if lexlevel>normal_function_level then
  1007. begin
  1008. reference_reset_base(hp,procinfo^.framepointer,procinfo^.framepointer_offset);
  1009. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1010. p:=procinfo^.parent;
  1011. for i:=3 to lexlevel-1 do
  1012. begin
  1013. reference_reset_base(hp,SELF_POINTER_REG,p^.framepointer_offset);
  1014. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1015. p:=p^.parent;
  1016. end;
  1017. reference_reset_base(hp,SELF_POINTER_REG,p^.selfpointer_offset);
  1018. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1019. end
  1020. else
  1021. begin
  1022. reference_reset_base(hp,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1023. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1024. end;
  1025. end;
  1026. end;
  1027. {*****************************************************************************
  1028. Entry/Exit Code Functions
  1029. *****************************************************************************}
  1030. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1031. var
  1032. href : treference;
  1033. hregister : tregister;
  1034. begin
  1035. if is_class(procinfo^._class) then
  1036. begin
  1037. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1038. { parameter 2 : self pointer / flag }
  1039. {!! this is a terrible hack, normally the helper should get three params : }
  1040. { one with self register, one with flag and one with VMT pointer }
  1041. {reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);}
  1042. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, paramanager.getintparaloc(2));
  1043. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1044. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1045. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1046. a_call_name(list,'FPC_NEW_CLASS');
  1047. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1048. { save the self pointer result }
  1049. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1050. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1051. end
  1052. else if is_object(procinfo^._class) then
  1053. begin
  1054. { parameter 3 :vmt_offset }
  1055. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1056. { parameter 2 : address of pointer to vmt }
  1057. { this is the first(?) parameter which was pushed to the constructor }
  1058. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1059. hregister:=get_scratch_reg_address(list);
  1060. a_loadaddr_ref_reg(list, href, hregister);
  1061. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1062. free_scratch_reg(list, hregister);
  1063. { parameter 1 : address of self pointer }
  1064. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1065. hregister:=get_scratch_reg_address(list);
  1066. a_loadaddr_ref_reg(list, href, hregister);
  1067. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1068. free_scratch_reg(list, hregister);
  1069. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1070. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1071. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1072. end
  1073. else
  1074. internalerror(200006161);
  1075. end;
  1076. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1077. var
  1078. nofinal : tasmlabel;
  1079. href : treference;
  1080. hregister : tregister;
  1081. begin
  1082. if is_class(procinfo^._class) then
  1083. begin
  1084. { 2nd parameter : flag }
  1085. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);
  1086. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1087. { 1st parameter to destructor : self }
  1088. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1089. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1090. a_call_name(list,'FPC_DISPOSE_CLASS')
  1091. end
  1092. else if is_object(procinfo^._class) then
  1093. begin
  1094. { must the object be finalized ? }
  1095. if procinfo^._class.needs_inittable then
  1096. begin
  1097. getlabel(nofinal);
  1098. reference_reset_base(href,procinfo^.framepointer,target_info.first_parm_offset);
  1099. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1100. reference_reset_base(href,SELF_POINTER_REG,0);
  1101. g_finalize(list,procinfo^._class,href,false);
  1102. a_label(list,nofinal);
  1103. end;
  1104. { actually call destructor }
  1105. { parameter 3 :vmt_offset }
  1106. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1107. { parameter 2 : pointer to vmt }
  1108. { this is the first parameter which was pushed to the destructor }
  1109. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1110. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1111. { parameter 1 : address of self pointer }
  1112. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1113. hregister:=get_scratch_reg_address(list);
  1114. a_loadaddr_ref_reg(list, href, hregister);
  1115. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1116. free_scratch_reg(list, hregister);
  1117. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1118. end
  1119. else
  1120. internalerror(200006162);
  1121. end;
  1122. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1123. var
  1124. href : treference;
  1125. hregister : tregister;
  1126. begin
  1127. if is_class(procinfo^._class) then
  1128. begin
  1129. {
  1130. Dispose of the class then set self_pointer to nil
  1131. both in stack and in self register.
  1132. }
  1133. { 2nd parameter : flag }
  1134. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1135. { 1st parameter to destructor : self }
  1136. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1137. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1138. a_call_name(list,'FPC_DISPOSE_CLASS');
  1139. { SET SELF TO NIL }
  1140. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1141. { set the self pointer in the stack to nil }
  1142. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1143. end
  1144. else if is_object(procinfo^._class) then
  1145. begin
  1146. { parameter 3 :vmt_offset }
  1147. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1148. { parameter 2 : address of pointer to vmt }
  1149. { this is the first(?) parameter which was pushed to the constructor }
  1150. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1151. hregister:=get_scratch_reg_address(list);
  1152. a_loadaddr_ref_reg(list, href, hregister);
  1153. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1154. free_scratch_reg(list, hregister);
  1155. { parameter 1 : address of self pointer }
  1156. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1157. hregister:=get_scratch_reg_address(list);
  1158. a_loadaddr_ref_reg(list, href, hregister);
  1159. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1160. free_scratch_reg(list, hregister);
  1161. a_call_name(list,'FPC_HELP_FAIL');
  1162. { SET SELF TO NIL }
  1163. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1164. end
  1165. else
  1166. internalerror(200006163);
  1167. end;
  1168. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1169. begin
  1170. end;
  1171. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1172. begin
  1173. end;
  1174. procedure tcg.g_profilecode(list : taasmoutput);
  1175. begin
  1176. end;
  1177. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1178. begin
  1179. a_load64_reg_reg(list,regsrc,regdst);
  1180. a_op64_const_reg(list,op,value,regdst);
  1181. end;
  1182. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1183. begin
  1184. a_load64_reg_reg(list,regsrc2,regdst);
  1185. a_op64_reg_reg(list,op,regsrc1,regdst);
  1186. end;
  1187. finalization
  1188. cg.free;
  1189. cg64.free;
  1190. end.
  1191. {
  1192. $Log$
  1193. Revision 1.41 2002-07-30 20:50:43 florian
  1194. * the code generator knows now if parameters are in registers
  1195. Revision 1.40 2002/07/29 21:16:02 florian
  1196. * some more ppc fixes
  1197. Revision 1.39 2002/07/28 15:56:00 jonas
  1198. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1199. generic implementation
  1200. Revision 1.38 2002/07/27 19:53:51 jonas
  1201. + generic implementation of tcg.g_flags2ref()
  1202. * tcg.flags2xxx() now also needs a size parameter
  1203. Revision 1.37 2002/07/20 11:57:53 florian
  1204. * types.pas renamed to defbase.pas because D6 contains a types
  1205. unit so this would conflicts if D6 programms are compiled
  1206. + Willamette/SSE2 instructions to assembler added
  1207. Revision 1.36 2002/07/11 14:41:27 florian
  1208. * start of the new generic parameter handling
  1209. Revision 1.35 2002/07/07 10:16:29 florian
  1210. * problems with last commit fixed
  1211. Revision 1.33 2002/07/07 09:52:32 florian
  1212. * powerpc target fixed, very simple units can be compiled
  1213. * some basic stuff for better callparanode handling, far from being finished
  1214. Revision 1.32 2002/07/06 20:09:10 carl
  1215. * updated generic constructor / destructor calling
  1216. Revision 1.31 2002/07/02 11:40:00 jonas
  1217. * fixed cg64 memory leak
  1218. Revision 1.30 2002/07/01 18:46:22 peter
  1219. * internal linker
  1220. * reorganized aasm layer
  1221. Revision 1.29 2002/07/01 16:23:52 peter
  1222. * cg64 patch
  1223. * basics for currency
  1224. * asnode updates for class and interface (not finished)
  1225. Revision 1.28 2002/06/06 18:53:17 jonas
  1226. * fixed internalerror(10) with -Or for i386 (a_load_ref_ref now saves
  1227. a general purpose register if it needs one but none are available)
  1228. Revision 1.27 2002/05/22 19:02:16 carl
  1229. + generic FPC_HELP_FAIL
  1230. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  1231. + generic FPC_DISPOSE_CLASS
  1232. + TEST_GENERIC define
  1233. Revision 1.26 2002/05/20 13:30:40 carl
  1234. * bugfix of hdisponen (base must be set, not index)
  1235. * more portability fixes
  1236. Revision 1.25 2002/05/18 13:34:05 peter
  1237. * readded missing revisions
  1238. Revision 1.24 2002/05/16 19:46:35 carl
  1239. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1240. + try to fix temp allocation (still in ifdef)
  1241. + generic constructor calls
  1242. + start of tassembler / tmodulebase class cleanup
  1243. Revision 1.23 2002/05/14 19:34:40 peter
  1244. * removed old logs and updated copyright year
  1245. Revision 1.22 2002/05/13 19:54:36 peter
  1246. * removed n386ld and n386util units
  1247. * maybe_save/maybe_restore added instead of the old maybe_push
  1248. Revision 1.21 2002/05/12 19:57:16 carl
  1249. * maybe_loadself portable
  1250. Revision 1.20 2002/05/12 16:53:04 peter
  1251. * moved entry and exitcode to ncgutil and cgobj
  1252. * foreach gets extra argument for passing local data to the
  1253. iterator function
  1254. * -CR checks also class typecasts at runtime by changing them
  1255. into as
  1256. * fixed compiler to cycle with the -CR option
  1257. * fixed stabs with elf writer, finally the global variables can
  1258. be watched
  1259. * removed a lot of routines from cga unit and replaced them by
  1260. calls to cgobj
  1261. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1262. u32bit then the other is typecasted also to u32bit without giving
  1263. a rangecheck warning/error.
  1264. * fixed pascal calling method with reversing also the high tree in
  1265. the parast, detected by tcalcst3 test
  1266. Revision 1.19 2002/04/26 15:19:04 peter
  1267. * use saveregisters for incr routines, saves also problems with
  1268. the optimizer
  1269. Revision 1.18 2002/04/25 20:16:38 peter
  1270. * moved more routines from cga/n386util
  1271. Revision 1.17 2002/04/22 16:30:05 peter
  1272. * fixed @methodpointer
  1273. Revision 1.16 2002/04/21 15:25:30 carl
  1274. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1275. + changeregsize -> rg.makeregsize
  1276. Revision 1.15 2002/04/20 21:32:23 carl
  1277. + generic FPC_CHECKPOINTER
  1278. + first parameter offset in stack now portable
  1279. * rename some constants
  1280. + move some cpu stuff to other units
  1281. - remove unused constents
  1282. * fix stacksize for some targets
  1283. * fix generic size problems which depend now on EXTEND_SIZE constant
  1284. Revision 1.14 2002/04/15 19:44:18 peter
  1285. * fixed stackcheck that would be called recursively when a stack
  1286. error was found
  1287. * generic changeregsize(reg,size) for i386 register resizing
  1288. * removed some more routines from cga unit
  1289. * fixed returnvalue handling
  1290. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1291. Revision 1.13 2002/04/07 13:22:11 carl
  1292. + more documentation
  1293. Revision 1.12 2002/04/07 09:12:46 carl
  1294. + documentation
  1295. Revision 1.11 2002/04/06 18:10:42 jonas
  1296. * several powerpc-related additions and fixes
  1297. Revision 1.10 2002/04/04 19:05:54 peter
  1298. * removed unused units
  1299. * use tlocation.size in cg.a_*loc*() routines
  1300. Revision 1.9 2002/04/02 17:11:27 peter
  1301. * tlocation,treference update
  1302. * LOC_CONSTANT added for better constant handling
  1303. * secondadd splitted in multiple routines
  1304. * location_force_reg added for loading a location to a register
  1305. of a specified size
  1306. * secondassignment parses now first the right and then the left node
  1307. (this is compatible with Kylix). This saves a lot of push/pop especially
  1308. with string operations
  1309. * adapted some routines to use the new cg methods
  1310. Revision 1.8 2002/03/31 20:26:33 jonas
  1311. + a_loadfpu_* and a_loadmm_* methods in tcg
  1312. * register allocation is now handled by a class and is mostly processor
  1313. independent (+rgobj.pas and i386/rgcpu.pas)
  1314. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1315. * some small improvements and fixes to the optimizer
  1316. * some register allocation fixes
  1317. * some fpuvaroffset fixes in the unary minus node
  1318. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1319. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1320. also better optimizable)
  1321. * fixed and optimized register saving/restoring for new/dispose nodes
  1322. * LOC_FPU locations now also require their "register" field to be set to
  1323. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1324. - list field removed of the tnode class because it's not used currently
  1325. and can cause hard-to-find bugs
  1326. Revision 1.7 2002/03/04 19:10:11 peter
  1327. * removed compiler warnings
  1328. }