cgobj.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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; abstract;
  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; const f: tresflags; reg: TRegister); virtual; abstract;
  201. procedure g_flags2ref(list: taasmoutput; const f: tresflags; const ref:TReference); virtual; abstract;
  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_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  310. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  311. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  312. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  313. { override to catch 64bit rangechecks }
  314. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  315. const todef: tdef);virtual;abstract;
  316. end;
  317. var
  318. {# Main code generator class }
  319. cg : tcg;
  320. {# Code generator class for all operations working with 64-Bit operands }
  321. cg64 : tcg64;
  322. implementation
  323. uses
  324. globals,globtype,options,systems,cgbase,
  325. verbose,types,tgobj,symdef,rgobj;
  326. const
  327. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  328. {*****************************************************************************
  329. basic functionallity
  330. ******************************************************************************}
  331. constructor tcg.create;
  332. var
  333. i : longint;
  334. begin
  335. scratch_register_array_pointer:=1;
  336. for i:=low(scratch_regs) to high(scratch_regs) do
  337. include(unusedscratchregisters,scratch_regs[i]);
  338. end;
  339. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  340. begin
  341. list.concat(tai_regalloc.alloc(r));
  342. end;
  343. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  344. begin
  345. list.concat(tai_regalloc.dealloc(r));
  346. end;
  347. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  348. begin
  349. list.concat(tai_label.create(l));
  350. end;
  351. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  352. var
  353. r : tregister;
  354. i : longint;
  355. begin
  356. if unusedscratchregisters=[] then
  357. internalerror(68996);
  358. for i:=scratch_register_array_pointer to
  359. (scratch_register_array_pointer+max_scratch_regs-1) do
  360. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  361. begin
  362. r:=scratch_regs[(i mod max_scratch_regs)+1];
  363. break;
  364. end;
  365. exclude(unusedscratchregisters,r);
  366. inc(scratch_register_array_pointer);
  367. if scratch_register_array_pointer>max_scratch_regs then
  368. scratch_register_array_pointer:=1;
  369. a_reg_alloc(list,r);
  370. get_scratch_reg_int:=r;
  371. end;
  372. { the default behavior simply returns a general purpose register }
  373. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  374. begin
  375. get_scratch_reg_address := get_scratch_reg_int(list);
  376. end;
  377. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  378. begin
  379. include(unusedscratchregisters,rg.makeregsize(r,OS_INT));
  380. a_reg_dealloc(list,r);
  381. end;
  382. {*****************************************************************************
  383. for better code generation these methods should be overridden
  384. ******************************************************************************}
  385. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  386. var
  387. hr : tregister;
  388. begin
  389. hr:=get_scratch_reg_int(list);
  390. a_load_const_reg(list,size,a,hr);
  391. a_param_reg(list,size,hr,locpara);
  392. free_scratch_reg(list,hr);
  393. end;
  394. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  395. var
  396. hr : tregister;
  397. begin
  398. hr:=get_scratch_reg_int(list);
  399. a_load_ref_reg(list,size,r,hr);
  400. a_param_reg(list,size,hr,locpara);
  401. free_scratch_reg(list,hr);
  402. end;
  403. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  404. begin
  405. case l.loc of
  406. LOC_REGISTER,
  407. LOC_CREGISTER :
  408. a_param_reg(list,l.size,l.register,locpara);
  409. LOC_CONSTANT :
  410. a_param_const(list,l.size,l.value,locpara);
  411. LOC_CREFERENCE,
  412. LOC_REFERENCE :
  413. a_param_ref(list,l.size,l.reference,locpara);
  414. else
  415. internalerror(2002032211);
  416. end;
  417. end;
  418. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  419. var
  420. hr : tregister;
  421. begin
  422. hr:=get_scratch_reg_address(list);
  423. a_loadaddr_ref_reg(list,r,hr);
  424. a_param_reg(list,OS_ADDR,hr,locpara);
  425. free_scratch_reg(list,hr);
  426. end;
  427. {****************************************************************************
  428. some generic implementations
  429. ****************************************************************************}
  430. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  431. var
  432. tmpreg: tregister;
  433. {$ifdef i386}
  434. pushed_reg: tregister;
  435. {$endif i386}
  436. begin
  437. {$ifdef i386}
  438. { the following is done with defines to avoid a speed penalty, }
  439. { since all this is only necessary for the 80x86 (because EDI }
  440. { doesn't have an 8bit component which is directly addressable) }
  441. pushed_reg := R_NO;
  442. if size in [OS_8,OS_S8] then
  443. if (rg.countunusedregsint = 0) then
  444. begin
  445. if (dref.base <> R_EBX) and
  446. (dref.index <> R_EBX) then
  447. pushed_reg := R_EBX
  448. else if (dref.base <> R_EAX) and
  449. (dref.index <> R_EAX) then
  450. pushed_reg := R_EAX
  451. else pushed_reg := R_ECX;
  452. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  453. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  454. end
  455. else
  456. tmpreg := rg.getregisterint(exprasmlist)
  457. else
  458. {$endif i386}
  459. tmpreg := get_scratch_reg_int(list);
  460. tmpreg:=rg.makeregsize(tmpreg,size);
  461. a_load_ref_reg(list,size,sref,tmpreg);
  462. a_load_reg_ref(list,size,tmpreg,dref);
  463. {$ifdef i386}
  464. if size in [OS_8,OS_S8] then
  465. begin
  466. if (pushed_reg <> R_NO) then
  467. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  468. else
  469. rg.ungetregister(exprasmlist,tmpreg)
  470. end
  471. else
  472. {$endif i386}
  473. free_scratch_reg(list,tmpreg);
  474. end;
  475. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  476. var
  477. tmpreg: tregister;
  478. begin
  479. tmpreg := get_scratch_reg_int(list);
  480. a_load_const_reg(list,size,a,tmpreg);
  481. a_load_reg_ref(list,size,tmpreg,ref);
  482. free_scratch_reg(list,tmpreg);
  483. end;
  484. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  485. begin
  486. case loc.loc of
  487. LOC_REFERENCE,LOC_CREFERENCE:
  488. a_load_const_ref(list,loc.size,a,loc.reference);
  489. LOC_REGISTER,LOC_CREGISTER:
  490. a_load_const_reg(list,loc.size,a,loc.register);
  491. else
  492. internalerror(200203272);
  493. end;
  494. end;
  495. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  496. begin
  497. case loc.loc of
  498. LOC_REFERENCE,LOC_CREFERENCE:
  499. a_load_reg_ref(list,size,reg,loc.reference);
  500. LOC_REGISTER,LOC_CREGISTER:
  501. a_load_reg_reg(list,size,reg,loc.register);
  502. else
  503. internalerror(200203271);
  504. end;
  505. end;
  506. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  507. begin
  508. case loc.loc of
  509. LOC_REFERENCE,LOC_CREFERENCE:
  510. a_load_ref_reg(list,loc.size,loc.reference,reg);
  511. LOC_REGISTER,LOC_CREGISTER:
  512. a_load_reg_reg(list,loc.size,loc.register,reg);
  513. LOC_CONSTANT:
  514. a_load_const_reg(list,loc.size,loc.value,reg);
  515. else
  516. internalerror(200109092);
  517. end;
  518. end;
  519. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  520. begin
  521. case loc.loc of
  522. LOC_REFERENCE,LOC_CREFERENCE:
  523. a_load_ref_ref(list,loc.size,loc.reference,ref);
  524. LOC_REGISTER,LOC_CREGISTER:
  525. a_load_reg_ref(list,loc.size,loc.register,ref);
  526. LOC_CONSTANT:
  527. a_load_const_ref(list,loc.size,loc.value,ref);
  528. else
  529. internalerror(200109302);
  530. end;
  531. end;
  532. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  533. begin
  534. case loc.loc of
  535. LOC_REFERENCE, LOC_CREFERENCE:
  536. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  537. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  538. a_loadfpu_reg_reg(list,loc.register,reg);
  539. else
  540. internalerror(200203301);
  541. end;
  542. end;
  543. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  544. begin
  545. case loc.loc of
  546. LOC_REFERENCE, LOC_CREFERENCE:
  547. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  548. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  549. a_loadfpu_reg_reg(list,reg,loc.register);
  550. else
  551. internalerror(48991);
  552. end;
  553. end;
  554. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  555. var
  556. tmpreg: tregister;
  557. begin
  558. tmpreg := get_scratch_reg_int(list);
  559. a_load_ref_reg(list,size,ref,tmpreg);
  560. a_op_const_reg(list,op,a,tmpreg);
  561. a_load_reg_ref(list,size,tmpreg,ref);
  562. free_scratch_reg(list,tmpreg);
  563. end;
  564. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  565. begin
  566. case loc.loc of
  567. LOC_REGISTER, LOC_CREGISTER:
  568. a_op_const_reg(list,op,a,loc.register);
  569. LOC_REFERENCE, LOC_CREFERENCE:
  570. a_op_const_ref(list,op,loc.size,a,loc.reference);
  571. else
  572. internalerror(200109061);
  573. end;
  574. end;
  575. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  576. var
  577. tmpreg: tregister;
  578. begin
  579. tmpreg := get_scratch_reg_int(list);
  580. a_load_ref_reg(list,size,ref,tmpreg);
  581. a_op_reg_reg(list,op,size,reg,tmpreg);
  582. a_load_reg_ref(list,size,tmpreg,ref);
  583. free_scratch_reg(list,tmpreg);
  584. end;
  585. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  586. var
  587. tmpreg: tregister;
  588. begin
  589. case op of
  590. OP_NOT,OP_NEG:
  591. { handle it as "load ref,reg; op reg" }
  592. begin
  593. a_load_ref_reg(list,size,ref,reg);
  594. a_op_reg_reg(list,op,size,reg,reg);
  595. end;
  596. else
  597. begin
  598. tmpreg := get_scratch_reg_int(list);
  599. a_load_ref_reg(list,size,ref,tmpreg);
  600. a_op_reg_reg(list,op,size,tmpreg,reg);
  601. free_scratch_reg(list,tmpreg);
  602. end;
  603. end;
  604. end;
  605. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  606. begin
  607. case loc.loc of
  608. LOC_REGISTER, LOC_CREGISTER:
  609. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  610. LOC_REFERENCE, LOC_CREFERENCE:
  611. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  612. else
  613. internalerror(200109061);
  614. end;
  615. end;
  616. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  617. var
  618. tmpreg: tregister;
  619. begin
  620. case loc.loc of
  621. LOC_REGISTER,LOC_CREGISTER:
  622. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  623. LOC_REFERENCE,LOC_CREFERENCE:
  624. begin
  625. tmpreg := get_scratch_reg_int(list);
  626. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  627. a_load_ref_reg(list,loc.size,ref,tmpreg);
  628. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  629. free_scratch_reg(list,tmpreg);
  630. end;
  631. else
  632. internalerror(200109061);
  633. end;
  634. end;
  635. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  636. size: tcgsize; a: aword; src, dst: tregister);
  637. begin
  638. a_load_reg_reg(list,size,src,dst);
  639. a_op_const_reg(list,op,a,dst);
  640. end;
  641. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  642. size: tcgsize; src1, src2, dst: tregister);
  643. begin
  644. a_load_reg_reg(list,size,src2,dst);
  645. a_op_reg_reg(list,op,size,src1,dst);
  646. end;
  647. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  648. l : tasmlabel);
  649. var
  650. tmpreg: tregister;
  651. begin
  652. tmpreg := get_scratch_reg_int(list);
  653. a_load_ref_reg(list,size,ref,tmpreg);
  654. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  655. free_scratch_reg(list,tmpreg);
  656. end;
  657. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  658. l : tasmlabel);
  659. begin
  660. case loc.loc of
  661. LOC_REGISTER,LOC_CREGISTER:
  662. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  663. LOC_REFERENCE,LOC_CREFERENCE:
  664. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  665. else
  666. internalerror(200109061);
  667. end;
  668. end;
  669. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  670. var
  671. tmpreg: tregister;
  672. begin
  673. tmpreg := get_scratch_reg_int(list);
  674. a_load_ref_reg(list,size,ref,tmpreg);
  675. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  676. free_scratch_reg(list,tmpreg);
  677. end;
  678. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  679. begin
  680. case loc.loc of
  681. LOC_REGISTER,
  682. LOC_CREGISTER:
  683. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  684. LOC_REFERENCE,
  685. LOC_CREFERENCE :
  686. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  687. LOC_CONSTANT:
  688. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  689. else
  690. internalerror(200203231);
  691. end;
  692. end;
  693. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  694. l : tasmlabel);
  695. var
  696. tmpreg: tregister;
  697. begin
  698. case loc.loc of
  699. LOC_REGISTER,LOC_CREGISTER:
  700. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  701. LOC_REFERENCE,LOC_CREFERENCE:
  702. begin
  703. {$ifdef i386}
  704. { the following is done with defines to avoid a speed penalty, }
  705. { since all this is only necessary for the 80x86 (because EDI }
  706. { doesn't have an 8bit component which is directly addressable) }
  707. if size in [OS_8,OS_S8] then
  708. tmpreg := rg.getregisterint(exprasmlist)
  709. else
  710. {$endif i386}
  711. tmpreg := get_scratch_reg_int(list);
  712. tmpreg := rg.makeregsize(tmpreg,size);
  713. a_load_ref_reg(list,size,loc.reference,tmpreg);
  714. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  715. {$ifdef i386}
  716. if size in [OS_8,OS_S8] then
  717. rg.ungetregister(exprasmlist,tmpreg)
  718. else
  719. {$endif i386}
  720. free_scratch_reg(list,tmpreg);
  721. end
  722. else
  723. internalerror(200109061);
  724. end;
  725. end;
  726. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  727. begin
  728. reg_cgsize := OS_INT;
  729. end;
  730. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  731. begin
  732. {$warning FIX ME!}
  733. a_paramaddr_ref(list,dest,getintparaloc(3));
  734. if loadref then
  735. a_param_ref(list,OS_ADDR,source,getintparaloc(2))
  736. else
  737. a_paramaddr_ref(list,source,getintparaloc(2));
  738. if delsource then
  739. reference_release(list,source);
  740. a_param_const(list,OS_INT,len,getintparaloc(1));
  741. a_call_name(list,'FPC_SHORTSTR_COPY');
  742. g_maybe_loadself(list);
  743. end;
  744. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  745. var
  746. href : treference;
  747. incrfunc : string;
  748. begin
  749. { These functions should not change the registers (they use
  750. the saveregister proc directive }
  751. if is_interfacecom(t) then
  752. incrfunc:='FPC_INTF_INCR_REF'
  753. else if is_ansistring(t) then
  754. incrfunc:='FPC_ANSISTR_INCR_REF'
  755. else if is_widestring(t) then
  756. incrfunc:='FPC_WIDESTR_INCR_REF'
  757. else if is_dynamic_array(t) then
  758. incrfunc:='FPC_DYNARRAY_INCR_REF'
  759. else
  760. incrfunc:='';
  761. { call the special incr function or the generic addref }
  762. if incrfunc<>'' then
  763. begin
  764. a_param_ref(list,OS_ADDR,ref,getintparaloc(1));
  765. a_call_name(list,incrfunc);
  766. end
  767. else
  768. begin
  769. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  770. a_paramaddr_ref(list,href,getintparaloc(2));
  771. a_paramaddr_ref(list,ref,getintparaloc(1));
  772. a_call_name(list,'FPC_ADDREF');
  773. end;
  774. end;
  775. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  776. var
  777. href : treference;
  778. decrfunc : string;
  779. begin
  780. if is_interfacecom(t) then
  781. decrfunc:='FPC_INTF_DECR_REF'
  782. else if is_ansistring(t) then
  783. decrfunc:='FPC_ANSISTR_DECR_REF'
  784. else if is_widestring(t) then
  785. decrfunc:='FPC_WIDESTR_DECR_REF'
  786. else if is_dynamic_array(t) then
  787. decrfunc:='FPC_DYNARRAY_INCR_REF'
  788. else
  789. decrfunc:='';
  790. { call the special decr function or the generic decref }
  791. if decrfunc<>'' then
  792. begin
  793. a_paramaddr_ref(list,ref,getintparaloc(1));
  794. a_call_name(list,decrfunc);
  795. end
  796. else
  797. begin
  798. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  799. a_paramaddr_ref(list,href,getintparaloc(2));
  800. a_paramaddr_ref(list,ref,getintparaloc(1));
  801. a_call_name(list,'FPC_DECREF');
  802. end;
  803. end;
  804. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  805. var
  806. href : treference;
  807. begin
  808. if is_ansistring(t) or
  809. is_widestring(t) or
  810. is_interfacecom(t) then
  811. a_load_const_ref(list,OS_ADDR,0,ref)
  812. else
  813. begin
  814. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  815. a_paramaddr_ref(list,href,getintparaloc(2));
  816. if loadref then
  817. a_param_ref(list,OS_ADDR,ref,getintparaloc(1))
  818. else
  819. a_paramaddr_ref(list,ref,getintparaloc(1));
  820. a_call_name(list,'FPC_INITIALIZE');
  821. end;
  822. end;
  823. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  824. var
  825. href : treference;
  826. begin
  827. if is_ansistring(t) or
  828. is_widestring(t) or
  829. is_interfacecom(t) then
  830. g_decrrefcount(list,t,ref)
  831. else
  832. begin
  833. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  834. a_paramaddr_ref(list,href,getintparaloc(2));
  835. if loadref then
  836. a_param_ref(list,OS_ADDR,ref,getintparaloc(1))
  837. else
  838. a_paramaddr_ref(list,ref,getintparaloc(1));
  839. a_call_name(list,'FPC_FINALIZE');
  840. end;
  841. end;
  842. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  843. { generate range checking code for the value at location p. The type }
  844. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  845. { is the original type used at that location. When both defs are equal }
  846. { the check is also insert (needed for succ,pref,inc,dec) }
  847. const
  848. {$ifdef ver1_0}
  849. awordsignedmax=high(longint);
  850. {$else}
  851. awordsignedmax=high(aword) div 2;
  852. {$endif}
  853. var
  854. neglabel : tasmlabel;
  855. hreg : tregister;
  856. fromdef : tdef;
  857. lto,hto,
  858. lfrom,hfrom : TConstExprInt;
  859. from_signed: boolean;
  860. begin
  861. { range checking on and range checkable value? }
  862. if not(cs_check_range in aktlocalswitches) or
  863. not(todef.deftype in [orddef,enumdef,arraydef]) then
  864. exit;
  865. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  866. begin
  867. cg64.g_rangecheck64(list,p,todef);
  868. exit;
  869. end;
  870. { only check when assigning to scalar, subranges are different, }
  871. { when todef=fromdef then the check is always generated }
  872. fromdef:=p.resulttype.def;
  873. getrange(p.resulttype.def,lfrom,hfrom);
  874. getrange(todef,lto,hto);
  875. { no range check if from and to are equal and are both longint/dword }
  876. { (if we have a 32bit processor) or int64/qword, since such }
  877. { operations can at most cause overflows (JM) }
  878. { Note that these checks are mostly processor independent, they only }
  879. { have to be changed once we introduce 64bit subrange types }
  880. if (fromdef = todef) and
  881. (fromdef.deftype=orddef) and
  882. (((sizeof(aword) = 4) and
  883. (((torddef(fromdef).typ = s32bit) and
  884. (lfrom = low(longint)) and
  885. (hfrom = high(longint))) or
  886. ((torddef(fromdef).typ = u32bit) and
  887. (lfrom = low(cardinal)) and
  888. (hfrom = high(cardinal))))) or
  889. is_64bitint(fromdef)) then
  890. exit;
  891. if todef<>fromdef then
  892. begin
  893. { if the from-range falls completely in the to-range, no check }
  894. { is necessary }
  895. if (lto<=lfrom) and (hto>=hfrom) then
  896. exit;
  897. end;
  898. { generate the rangecheck code for the def where we are going to }
  899. { store the result }
  900. { use the trick that }
  901. { a <= x <= b <=> 0 <= x-a <= b-a <=> cardinal(x-a) <= cardinal(b-a) }
  902. { To be able to do that, we have to make sure however that either }
  903. { fromdef and todef are both signed or unsigned, or that we leave }
  904. { the parts < 0 and > maxlongint out }
  905. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  906. from_signed := is_signed(fromdef);
  907. if from_signed xor is_signed(todef) then
  908. if from_signed then
  909. { from is signed, to is unsigned }
  910. begin
  911. { if high(from) < 0 -> always range error }
  912. if (hfrom < 0) or
  913. { if low(to) > maxlongint also range error }
  914. (lto > awordsignedmax) then
  915. begin
  916. a_call_name(list,'FPC_RANGEERROR');
  917. exit
  918. end;
  919. { from is signed and to is unsigned -> when looking at from }
  920. { as an unsigned value, it must be < maxlongint (otherwise }
  921. { it's negative, which is invalid since "to" is unsigned) }
  922. if hto > awordsignedmax then
  923. hto := awordsignedmax;
  924. end
  925. else
  926. { from is unsigned, to is signed }
  927. begin
  928. if (lfrom > awordsignedmax) or
  929. (hto < 0) then
  930. begin
  931. a_call_name(list,'FPC_RANGEERROR');
  932. exit
  933. end;
  934. { from is unsigned and to is signed -> when looking at to }
  935. { as an unsigned value, it must be >= 0 (since negative }
  936. { values are the same as values > maxlongint) }
  937. if lto < 0 then
  938. lto := 0;
  939. end;
  940. hreg := get_scratch_reg_int(list);
  941. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  942. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  943. aword(longint(lto and $ffffffff)),p.location.register,hreg)
  944. else
  945. begin
  946. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  947. p.location.reference,hreg);
  948. a_op_const_reg(list,OP_SUB,aword(longint(lto and $ffffffff)),hreg);
  949. end;
  950. getlabel(neglabel);
  951. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(longint((hto-lto) and $ffffffff)),hreg,neglabel);
  952. { !!! should happen right after the compare (JM) }
  953. free_scratch_reg(list,hreg);
  954. a_call_name(list,'FPC_RANGEERROR');
  955. a_label(list,neglabel);
  956. end;
  957. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  958. begin
  959. a_param_const(list,OS_32,stackframesize,getintparaloc(1));
  960. a_call_name(list,'FPC_STACKCHECK');
  961. end;
  962. procedure tcg.g_maybe_loadself(list : taasmoutput);
  963. var
  964. hp : treference;
  965. p : pprocinfo;
  966. i : longint;
  967. begin
  968. if assigned(procinfo^._class) then
  969. begin
  970. list.concat(tai_regalloc.Alloc(SELF_POINTER_REG));
  971. if lexlevel>normal_function_level then
  972. begin
  973. reference_reset_base(hp,procinfo^.framepointer,procinfo^.framepointer_offset);
  974. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  975. p:=procinfo^.parent;
  976. for i:=3 to lexlevel-1 do
  977. begin
  978. reference_reset_base(hp,SELF_POINTER_REG,p^.framepointer_offset);
  979. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  980. p:=p^.parent;
  981. end;
  982. reference_reset_base(hp,SELF_POINTER_REG,p^.selfpointer_offset);
  983. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  984. end
  985. else
  986. begin
  987. reference_reset_base(hp,procinfo^.framepointer,procinfo^.selfpointer_offset);
  988. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  989. end;
  990. end;
  991. end;
  992. {*****************************************************************************
  993. Entry/Exit Code Functions
  994. *****************************************************************************}
  995. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  996. var
  997. href : treference;
  998. hregister : tregister;
  999. begin
  1000. if is_class(procinfo^._class) then
  1001. begin
  1002. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1003. { parameter 2 : self pointer / flag }
  1004. {!! this is a terrible hack, normally the helper should get three params : }
  1005. { one with self register, one with flag and one with VMT pointer }
  1006. {reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);}
  1007. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, getintparaloc(2));
  1008. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1009. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1010. a_param_ref(list, OS_ADDR,href,getintparaloc(1));
  1011. a_call_name(list,'FPC_NEW_CLASS');
  1012. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1013. { save the self pointer result }
  1014. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1015. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1016. end
  1017. else if is_object(procinfo^._class) then
  1018. begin
  1019. { parameter 3 :vmt_offset }
  1020. a_param_const(list, OS_32, procinfo^._class.vmt_offset, getintparaloc(3));
  1021. { parameter 2 : address of pointer to vmt }
  1022. { this is the first(?) parameter which was pushed to the constructor }
  1023. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1024. hregister:=get_scratch_reg_address(list);
  1025. a_loadaddr_ref_reg(list, href, hregister);
  1026. a_param_reg(list, OS_ADDR,hregister,getintparaloc(2));
  1027. free_scratch_reg(list, hregister);
  1028. { parameter 1 : address of self pointer }
  1029. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1030. hregister:=get_scratch_reg_address(list);
  1031. a_loadaddr_ref_reg(list, href, hregister);
  1032. a_param_reg(list, OS_ADDR,hregister,getintparaloc(1));
  1033. free_scratch_reg(list, hregister);
  1034. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1035. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1036. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1037. end
  1038. else
  1039. internalerror(200006161);
  1040. end;
  1041. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1042. var
  1043. nofinal : tasmlabel;
  1044. href : treference;
  1045. hregister : tregister;
  1046. begin
  1047. if is_class(procinfo^._class) then
  1048. begin
  1049. { 2nd parameter : flag }
  1050. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);
  1051. a_param_ref(list, OS_ADDR,href,getintparaloc(2));
  1052. { 1st parameter to destructor : self }
  1053. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1054. a_param_ref(list, OS_ADDR,href,getintparaloc(1));
  1055. a_call_name(list,'FPC_DISPOSE_CLASS')
  1056. end
  1057. else if is_object(procinfo^._class) then
  1058. begin
  1059. { must the object be finalized ? }
  1060. if procinfo^._class.needs_inittable then
  1061. begin
  1062. getlabel(nofinal);
  1063. reference_reset_base(href,procinfo^.framepointer,target_info.first_parm_offset);
  1064. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1065. reference_reset_base(href,SELF_POINTER_REG,0);
  1066. g_finalize(list,procinfo^._class,href,false);
  1067. a_label(list,nofinal);
  1068. end;
  1069. { actually call destructor }
  1070. { parameter 3 :vmt_offset }
  1071. a_param_const(list, OS_32, procinfo^._class.vmt_offset, getintparaloc(3));
  1072. { parameter 2 : pointer to vmt }
  1073. { this is the first parameter which was pushed to the destructor }
  1074. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1075. a_param_ref(list, OS_ADDR, href ,getintparaloc(2));
  1076. { parameter 1 : address of self pointer }
  1077. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1078. hregister:=get_scratch_reg_address(list);
  1079. a_loadaddr_ref_reg(list, href, hregister);
  1080. a_param_reg(list, OS_ADDR,hregister,getintparaloc(1));
  1081. free_scratch_reg(list, hregister);
  1082. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1083. end
  1084. else
  1085. internalerror(200006162);
  1086. end;
  1087. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1088. var
  1089. href : treference;
  1090. hregister : tregister;
  1091. begin
  1092. if is_class(procinfo^._class) then
  1093. begin
  1094. {
  1095. Dispose of the class then set self_pointer to nil
  1096. both in stack and in self register.
  1097. }
  1098. { 2nd parameter : flag }
  1099. a_param_const(list,OS_32,1,getintparaloc(2));
  1100. { 1st parameter to destructor : self }
  1101. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1102. a_param_ref(list, OS_ADDR,href,getintparaloc(1));
  1103. a_call_name(list,'FPC_DISPOSE_CLASS');
  1104. { SET SELF TO NIL }
  1105. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1106. { set the self pointer in the stack to nil }
  1107. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1108. end
  1109. else if is_object(procinfo^._class) then
  1110. begin
  1111. { parameter 3 :vmt_offset }
  1112. a_param_const(list, OS_32, procinfo^._class.vmt_offset, getintparaloc(3));
  1113. { parameter 2 : address of pointer to vmt }
  1114. { this is the first(?) parameter which was pushed to the constructor }
  1115. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1116. hregister:=get_scratch_reg_address(list);
  1117. a_loadaddr_ref_reg(list, href, hregister);
  1118. a_param_reg(list, OS_ADDR,hregister,getintparaloc(2));
  1119. free_scratch_reg(list, hregister);
  1120. { parameter 1 : address of self pointer }
  1121. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1122. hregister:=get_scratch_reg_address(list);
  1123. a_loadaddr_ref_reg(list, href, hregister);
  1124. a_param_reg(list, OS_ADDR,hregister,getintparaloc(1));
  1125. free_scratch_reg(list, hregister);
  1126. a_call_name(list,'FPC_HELP_FAIL');
  1127. { SET SELF TO NIL }
  1128. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1129. end
  1130. else
  1131. internalerror(200006163);
  1132. end;
  1133. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1134. begin
  1135. end;
  1136. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1137. begin
  1138. end;
  1139. procedure tcg.g_profilecode(list : taasmoutput);
  1140. begin
  1141. end;
  1142. finalization
  1143. cg.free;
  1144. cg64.free;
  1145. end.
  1146. {
  1147. $Log$
  1148. Revision 1.35 2002-07-07 10:16:29 florian
  1149. * problems with last commit fixed
  1150. Revision 1.33 2002/07/07 09:52:32 florian
  1151. * powerpc target fixed, very simple units can be compiled
  1152. * some basic stuff for better callparanode handling, far from being finished
  1153. Revision 1.32 2002/07/06 20:09:10 carl
  1154. * updated generic constructor / destructor calling
  1155. Revision 1.31 2002/07/02 11:40:00 jonas
  1156. * fixed cg64 memory leak
  1157. Revision 1.30 2002/07/01 18:46:22 peter
  1158. * internal linker
  1159. * reorganized aasm layer
  1160. Revision 1.29 2002/07/01 16:23:52 peter
  1161. * cg64 patch
  1162. * basics for currency
  1163. * asnode updates for class and interface (not finished)
  1164. Revision 1.28 2002/06/06 18:53:17 jonas
  1165. * fixed internalerror(10) with -Or for i386 (a_load_ref_ref now saves
  1166. a general purpose register if it needs one but none are available)
  1167. Revision 1.27 2002/05/22 19:02:16 carl
  1168. + generic FPC_HELP_FAIL
  1169. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  1170. + generic FPC_DISPOSE_CLASS
  1171. + TEST_GENERIC define
  1172. Revision 1.26 2002/05/20 13:30:40 carl
  1173. * bugfix of hdisponen (base must be set, not index)
  1174. * more portability fixes
  1175. Revision 1.25 2002/05/18 13:34:05 peter
  1176. * readded missing revisions
  1177. Revision 1.24 2002/05/16 19:46:35 carl
  1178. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1179. + try to fix temp allocation (still in ifdef)
  1180. + generic constructor calls
  1181. + start of tassembler / tmodulebase class cleanup
  1182. Revision 1.23 2002/05/14 19:34:40 peter
  1183. * removed old logs and updated copyright year
  1184. Revision 1.22 2002/05/13 19:54:36 peter
  1185. * removed n386ld and n386util units
  1186. * maybe_save/maybe_restore added instead of the old maybe_push
  1187. Revision 1.21 2002/05/12 19:57:16 carl
  1188. * maybe_loadself portable
  1189. Revision 1.20 2002/05/12 16:53:04 peter
  1190. * moved entry and exitcode to ncgutil and cgobj
  1191. * foreach gets extra argument for passing local data to the
  1192. iterator function
  1193. * -CR checks also class typecasts at runtime by changing them
  1194. into as
  1195. * fixed compiler to cycle with the -CR option
  1196. * fixed stabs with elf writer, finally the global variables can
  1197. be watched
  1198. * removed a lot of routines from cga unit and replaced them by
  1199. calls to cgobj
  1200. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1201. u32bit then the other is typecasted also to u32bit without giving
  1202. a rangecheck warning/error.
  1203. * fixed pascal calling method with reversing also the high tree in
  1204. the parast, detected by tcalcst3 test
  1205. Revision 1.19 2002/04/26 15:19:04 peter
  1206. * use saveregisters for incr routines, saves also problems with
  1207. the optimizer
  1208. Revision 1.18 2002/04/25 20:16:38 peter
  1209. * moved more routines from cga/n386util
  1210. Revision 1.17 2002/04/22 16:30:05 peter
  1211. * fixed @methodpointer
  1212. Revision 1.16 2002/04/21 15:25:30 carl
  1213. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1214. + changeregsize -> rg.makeregsize
  1215. Revision 1.15 2002/04/20 21:32:23 carl
  1216. + generic FPC_CHECKPOINTER
  1217. + first parameter offset in stack now portable
  1218. * rename some constants
  1219. + move some cpu stuff to other units
  1220. - remove unused constents
  1221. * fix stacksize for some targets
  1222. * fix generic size problems which depend now on EXTEND_SIZE constant
  1223. Revision 1.14 2002/04/15 19:44:18 peter
  1224. * fixed stackcheck that would be called recursively when a stack
  1225. error was found
  1226. * generic changeregsize(reg,size) for i386 register resizing
  1227. * removed some more routines from cga unit
  1228. * fixed returnvalue handling
  1229. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1230. Revision 1.13 2002/04/07 13:22:11 carl
  1231. + more documentation
  1232. Revision 1.12 2002/04/07 09:12:46 carl
  1233. + documentation
  1234. Revision 1.11 2002/04/06 18:10:42 jonas
  1235. * several powerpc-related additions and fixes
  1236. Revision 1.10 2002/04/04 19:05:54 peter
  1237. * removed unused units
  1238. * use tlocation.size in cg.a_*loc*() routines
  1239. Revision 1.9 2002/04/02 17:11:27 peter
  1240. * tlocation,treference update
  1241. * LOC_CONSTANT added for better constant handling
  1242. * secondadd splitted in multiple routines
  1243. * location_force_reg added for loading a location to a register
  1244. of a specified size
  1245. * secondassignment parses now first the right and then the left node
  1246. (this is compatible with Kylix). This saves a lot of push/pop especially
  1247. with string operations
  1248. * adapted some routines to use the new cg methods
  1249. Revision 1.8 2002/03/31 20:26:33 jonas
  1250. + a_loadfpu_* and a_loadmm_* methods in tcg
  1251. * register allocation is now handled by a class and is mostly processor
  1252. independent (+rgobj.pas and i386/rgcpu.pas)
  1253. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1254. * some small improvements and fixes to the optimizer
  1255. * some register allocation fixes
  1256. * some fpuvaroffset fixes in the unary minus node
  1257. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1258. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1259. also better optimizable)
  1260. * fixed and optimized register saving/restoring for new/dispose nodes
  1261. * LOC_FPU locations now also require their "register" field to be set to
  1262. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1263. - list field removed of the tnode class because it's not used currently
  1264. and can cause hard-to-find bugs
  1265. Revision 1.7 2002/03/04 19:10:11 peter
  1266. * removed compiler warnings
  1267. }