cgobj.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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,aasm,symtable,
  28. cpuasm,cpubase,cpuinfo,
  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;nr : longint);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;nr : longint);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;nr : longint);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;nr : longint);
  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;nr : longint);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;nr : longint);virtual;abstract;
  310. procedure a_param64_const(list : taasmoutput;value : qword;nr : longint);virtual;abstract;
  311. procedure a_param64_ref(list : taasmoutput;const r : treference;nr : longint);virtual;abstract;
  312. procedure a_param64_loc(list : taasmoutput;const l : tlocation;nr : longint);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,tainst,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(tairegalloc.alloc(r));
  342. end;
  343. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  344. begin
  345. list.concat(tairegalloc.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;nr : longint);
  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,nr);
  392. free_scratch_reg(list,hr);
  393. end;
  394. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  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,nr);
  401. free_scratch_reg(list,hr);
  402. end;
  403. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;nr : longint);
  404. begin
  405. case l.loc of
  406. LOC_REGISTER,
  407. LOC_CREGISTER :
  408. a_param_reg(list,l.size,l.register,nr);
  409. LOC_CONSTANT :
  410. a_param_const(list,l.size,l.value,nr);
  411. LOC_CREFERENCE,
  412. LOC_REFERENCE :
  413. a_param_ref(list,l.size,l.reference,nr);
  414. else
  415. internalerror(2002032211);
  416. end;
  417. end;
  418. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  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,nr);
  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. a_paramaddr_ref(list,dest,3);
  733. if loadref then
  734. a_param_ref(list,OS_ADDR,source,2)
  735. else
  736. a_paramaddr_ref(list,source,2);
  737. if delsource then
  738. reference_release(list,source);
  739. a_param_const(list,OS_INT,len,1);
  740. a_call_name(list,'FPC_SHORTSTR_COPY');
  741. g_maybe_loadself(list);
  742. end;
  743. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  744. var
  745. href : treference;
  746. incrfunc : string;
  747. begin
  748. { These functions should not change the registers (they use
  749. the saveregister proc directive }
  750. if is_interfacecom(t) then
  751. incrfunc:='FPC_INTF_INCR_REF'
  752. else if is_ansistring(t) then
  753. incrfunc:='FPC_ANSISTR_INCR_REF'
  754. else if is_widestring(t) then
  755. incrfunc:='FPC_WIDESTR_INCR_REF'
  756. else if is_dynamic_array(t) then
  757. incrfunc:='FPC_DYNARRAY_INCR_REF'
  758. else
  759. incrfunc:='';
  760. { call the special incr function or the generic addref }
  761. if incrfunc<>'' then
  762. begin
  763. a_param_ref(list,OS_ADDR,ref,1);
  764. a_call_name(list,incrfunc);
  765. end
  766. else
  767. begin
  768. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  769. a_paramaddr_ref(list,href,2);
  770. a_paramaddr_ref(list,ref,1);
  771. a_call_name(list,'FPC_ADDREF');
  772. end;
  773. end;
  774. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  775. var
  776. href : treference;
  777. decrfunc : string;
  778. begin
  779. if is_interfacecom(t) then
  780. decrfunc:='FPC_INTF_DECR_REF'
  781. else if is_ansistring(t) then
  782. decrfunc:='FPC_ANSISTR_DECR_REF'
  783. else if is_widestring(t) then
  784. decrfunc:='FPC_WIDESTR_DECR_REF'
  785. else if is_dynamic_array(t) then
  786. decrfunc:='FPC_DYNARRAY_INCR_REF'
  787. else
  788. decrfunc:='';
  789. { call the special decr function or the generic decref }
  790. if decrfunc<>'' then
  791. begin
  792. a_paramaddr_ref(list,ref,1);
  793. a_call_name(list,decrfunc);
  794. end
  795. else
  796. begin
  797. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  798. a_paramaddr_ref(list,href,2);
  799. a_paramaddr_ref(list,ref,1);
  800. a_call_name(list,'FPC_DECREF');
  801. end;
  802. end;
  803. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  804. var
  805. href : treference;
  806. begin
  807. if is_ansistring(t) or
  808. is_widestring(t) or
  809. is_interfacecom(t) then
  810. a_load_const_ref(list,OS_ADDR,0,ref)
  811. else
  812. begin
  813. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  814. a_paramaddr_ref(list,href,2);
  815. if loadref then
  816. a_param_ref(list,OS_ADDR,ref,1)
  817. else
  818. a_paramaddr_ref(list,ref,1);
  819. a_call_name(list,'FPC_INITIALIZE');
  820. end;
  821. end;
  822. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  823. var
  824. href : treference;
  825. begin
  826. if is_ansistring(t) or
  827. is_widestring(t) or
  828. is_interfacecom(t) then
  829. g_decrrefcount(list,t,ref)
  830. else
  831. begin
  832. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  833. a_paramaddr_ref(list,href,2);
  834. if loadref then
  835. a_param_ref(list,OS_ADDR,ref,1)
  836. else
  837. a_paramaddr_ref(list,ref,1);
  838. a_call_name(list,'FPC_FINALIZE');
  839. end;
  840. end;
  841. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  842. { generate range checking code for the value at location p. The type }
  843. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  844. { is the original type used at that location. When both defs are equal }
  845. { the check is also insert (needed for succ,pref,inc,dec) }
  846. const
  847. {$ifdef ver1_0}
  848. awordsignedmax=high(longint);
  849. {$else}
  850. awordsignedmax=high(aword) div 2;
  851. {$endif}
  852. var
  853. neglabel : tasmlabel;
  854. hreg : tregister;
  855. fromdef : tdef;
  856. lto,hto,
  857. lfrom,hfrom : TConstExprInt;
  858. from_signed: boolean;
  859. begin
  860. { range checking on and range checkable value? }
  861. if not(cs_check_range in aktlocalswitches) or
  862. not(todef.deftype in [orddef,enumdef,arraydef]) then
  863. exit;
  864. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  865. begin
  866. cg64.g_rangecheck64(list,p,todef);
  867. exit;
  868. end;
  869. { only check when assigning to scalar, subranges are different, }
  870. { when todef=fromdef then the check is always generated }
  871. fromdef:=p.resulttype.def;
  872. getrange(p.resulttype.def,lfrom,hfrom);
  873. getrange(todef,lto,hto);
  874. { no range check if from and to are equal and are both longint/dword }
  875. { (if we have a 32bit processor) or int64/qword, since such }
  876. { operations can at most cause overflows (JM) }
  877. { Note that these checks are mostly processor independent, they only }
  878. { have to be changed once we introduce 64bit subrange types }
  879. if (fromdef = todef) and
  880. (fromdef.deftype=orddef) and
  881. (((sizeof(aword) = 4) and
  882. (((torddef(fromdef).typ = s32bit) and
  883. (lfrom = low(longint)) and
  884. (hfrom = high(longint))) or
  885. ((torddef(fromdef).typ = u32bit) and
  886. (lfrom = low(cardinal)) and
  887. (hfrom = high(cardinal))))) or
  888. is_64bitint(fromdef)) then
  889. exit;
  890. if todef<>fromdef then
  891. begin
  892. { if the from-range falls completely in the to-range, no check }
  893. { is necessary }
  894. if (lto<=lfrom) and (hto>=hfrom) then
  895. exit;
  896. end;
  897. { generate the rangecheck code for the def where we are going to }
  898. { store the result }
  899. { use the trick that }
  900. { a <= x <= b <=> 0 <= x-a <= b-a <=> cardinal(x-a) <= cardinal(b-a) }
  901. { To be able to do that, we have to make sure however that either }
  902. { fromdef and todef are both signed or unsigned, or that we leave }
  903. { the parts < 0 and > maxlongint out }
  904. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  905. from_signed := is_signed(fromdef);
  906. if from_signed xor is_signed(todef) then
  907. if from_signed then
  908. { from is signed, to is unsigned }
  909. begin
  910. { if high(from) < 0 -> always range error }
  911. if (hfrom < 0) or
  912. { if low(to) > maxlongint also range error }
  913. (lto > awordsignedmax) then
  914. begin
  915. a_call_name(list,'FPC_RANGEERROR');
  916. exit
  917. end;
  918. { from is signed and to is unsigned -> when looking at from }
  919. { as an unsigned value, it must be < maxlongint (otherwise }
  920. { it's negative, which is invalid since "to" is unsigned) }
  921. if hto > awordsignedmax then
  922. hto := awordsignedmax;
  923. end
  924. else
  925. { from is unsigned, to is signed }
  926. begin
  927. if (lfrom > awordsignedmax) or
  928. (hto < 0) then
  929. begin
  930. a_call_name(list,'FPC_RANGEERROR');
  931. exit
  932. end;
  933. { from is unsigned and to is signed -> when looking at to }
  934. { as an unsigned value, it must be >= 0 (since negative }
  935. { values are the same as values > maxlongint) }
  936. if lto < 0 then
  937. lto := 0;
  938. end;
  939. hreg := get_scratch_reg_int(list);
  940. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  941. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  942. aword(longint(lto and $ffffffff)),p.location.register,hreg)
  943. else
  944. begin
  945. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  946. p.location.reference,hreg);
  947. a_op_const_reg(list,OP_SUB,aword(longint(lto and $ffffffff)),hreg);
  948. end;
  949. getlabel(neglabel);
  950. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(longint((hto-lto) and $ffffffff)),hreg,neglabel);
  951. { !!! should happen right after the compare (JM) }
  952. free_scratch_reg(list,hreg);
  953. a_call_name(list,'FPC_RANGEERROR');
  954. a_label(list,neglabel);
  955. end;
  956. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  957. begin
  958. a_param_const(list,OS_32,stackframesize,1);
  959. a_call_name(list,'FPC_STACKCHECK');
  960. end;
  961. procedure tcg.g_maybe_loadself(list : taasmoutput);
  962. var
  963. hp : treference;
  964. p : pprocinfo;
  965. i : longint;
  966. begin
  967. if assigned(procinfo^._class) then
  968. begin
  969. list.concat(Tairegalloc.Alloc(SELF_POINTER_REG));
  970. if lexlevel>normal_function_level then
  971. begin
  972. reference_reset_base(hp,procinfo^.framepointer,procinfo^.framepointer_offset);
  973. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  974. p:=procinfo^.parent;
  975. for i:=3 to lexlevel-1 do
  976. begin
  977. reference_reset_base(hp,SELF_POINTER_REG,p^.framepointer_offset);
  978. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  979. p:=p^.parent;
  980. end;
  981. reference_reset_base(hp,SELF_POINTER_REG,p^.selfpointer_offset);
  982. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  983. end
  984. else
  985. begin
  986. reference_reset_base(hp,procinfo^.framepointer,procinfo^.selfpointer_offset);
  987. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  988. end;
  989. end;
  990. end;
  991. {*****************************************************************************
  992. Entry/Exit Code Functions
  993. *****************************************************************************}
  994. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  995. var
  996. href : treference;
  997. hregister : tregister;
  998. begin
  999. if is_class(procinfo^._class) then
  1000. begin
  1001. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1002. { parameter 2 : self pointer }
  1003. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, 2);
  1004. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1005. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1006. a_param_ref(list, OS_ADDR,href,1);
  1007. a_call_name(list,'FPC_NEW_CLASS');
  1008. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1009. { save the self pointer result }
  1010. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1011. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1012. end
  1013. else if is_object(procinfo^._class) then
  1014. begin
  1015. { parameter 3 :vmt_offset }
  1016. a_param_const(list, OS_32, procinfo^._class.vmt_offset, 3);
  1017. { parameter 2 : address of pointer to vmt }
  1018. { this is the first(?) parameter which was pushed to the constructor }
  1019. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1020. hregister:=get_scratch_reg_address(list);
  1021. a_loadaddr_ref_reg(list, href, hregister);
  1022. a_param_reg(list, OS_ADDR,hregister,2);
  1023. free_scratch_reg(list, hregister);
  1024. { parameter 1 : address of self pointer }
  1025. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1026. hregister:=get_scratch_reg_address(list);
  1027. a_loadaddr_ref_reg(list, href, hregister);
  1028. a_param_reg(list, OS_ADDR,hregister,1);
  1029. free_scratch_reg(list, hregister);
  1030. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1031. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1032. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1033. end
  1034. else
  1035. internalerror(200006161);
  1036. end;
  1037. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1038. var
  1039. nofinal : tasmlabel;
  1040. href : treference;
  1041. hregister : tregister;
  1042. begin
  1043. if is_class(procinfo^._class) then
  1044. begin
  1045. { 2nd parameter : flag }
  1046. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);
  1047. a_param_ref(list, OS_ADDR,href,2);
  1048. { 1st parameter to destructor : self }
  1049. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1050. a_param_ref(list, OS_ADDR,href,1);
  1051. a_call_name(list,'FPC_DISPOSE_CLASS')
  1052. end
  1053. else if is_object(procinfo^._class) then
  1054. begin
  1055. { must the object be finalized ? }
  1056. if procinfo^._class.needs_inittable then
  1057. begin
  1058. getlabel(nofinal);
  1059. reference_reset_base(href,procinfo^.framepointer,target_info.first_parm_offset);
  1060. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1061. reference_reset_base(href,SELF_POINTER_REG,0);
  1062. g_finalize(list,procinfo^._class,href,false);
  1063. a_label(list,nofinal);
  1064. end;
  1065. { actually call destructor }
  1066. { parameter 3 :vmt_offset }
  1067. a_param_const(list, OS_32, procinfo^._class.vmt_offset, 3);
  1068. { parameter 2 : pointer to vmt }
  1069. { this is the first parameter which was pushed to the destructor }
  1070. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1071. a_param_ref(list, OS_ADDR, href ,2);
  1072. { parameter 1 : address of self pointer }
  1073. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1074. hregister:=get_scratch_reg_address(list);
  1075. a_loadaddr_ref_reg(list, href, hregister);
  1076. a_param_reg(list, OS_ADDR,hregister,1);
  1077. free_scratch_reg(list, hregister);
  1078. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1079. end
  1080. else
  1081. internalerror(200006162);
  1082. end;
  1083. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1084. var
  1085. href : treference;
  1086. hregister : tregister;
  1087. begin
  1088. if is_class(procinfo^._class) then
  1089. begin
  1090. {$warning todo}
  1091. { Should simply casll FPC_DISPOSE_CLASS and then set the
  1092. SELF_POINTER_REGISTER to NIL
  1093. }
  1094. internalerror(20020523);
  1095. { reference_reset_base(href,procinfo^.framepointer,8);
  1096. a_load_ref_reg(list,OS_ADDR,href,R_ESI);
  1097. a_call_name(list,'FPC_HELP_FAIL_CLASS');}
  1098. end
  1099. else if is_object(procinfo^._class) then
  1100. begin
  1101. { parameter 3 :vmt_offset }
  1102. a_param_const(list, OS_32, procinfo^._class.vmt_offset, 3);
  1103. { parameter 2 : address of pointer to vmt }
  1104. { this is the first(?) parameter which was pushed to the constructor }
  1105. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1106. hregister:=get_scratch_reg_address(list);
  1107. a_loadaddr_ref_reg(list, href, hregister);
  1108. a_param_reg(list, OS_ADDR,hregister,2);
  1109. free_scratch_reg(list, hregister);
  1110. { parameter 1 : address of self pointer }
  1111. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1112. hregister:=get_scratch_reg_address(list);
  1113. a_loadaddr_ref_reg(list, href, hregister);
  1114. a_param_reg(list, OS_ADDR,hregister,1);
  1115. free_scratch_reg(list, hregister);
  1116. a_call_name(list,'FPC_HELP_FAIL');
  1117. { SET SELF TO NIL }
  1118. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1119. end
  1120. else
  1121. internalerror(200006163);
  1122. end;
  1123. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1124. begin
  1125. end;
  1126. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1127. begin
  1128. end;
  1129. procedure tcg.g_profilecode(list : taasmoutput);
  1130. begin
  1131. end;
  1132. finalization
  1133. cg.free;
  1134. end.
  1135. {
  1136. $Log$
  1137. Revision 1.29 2002-07-01 16:23:52 peter
  1138. * cg64 patch
  1139. * basics for currency
  1140. * asnode updates for class and interface (not finished)
  1141. Revision 1.28 2002/06/06 18:53:17 jonas
  1142. * fixed internalerror(10) with -Or for i386 (a_load_ref_ref now saves
  1143. a general purpose register if it needs one but none are available)
  1144. Revision 1.27 2002/05/22 19:02:16 carl
  1145. + generic FPC_HELP_FAIL
  1146. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  1147. + generic FPC_DISPOSE_CLASS
  1148. + TEST_GENERIC define
  1149. Revision 1.26 2002/05/20 13:30:40 carl
  1150. * bugfix of hdisponen (base must be set, not index)
  1151. * more portability fixes
  1152. Revision 1.25 2002/05/18 13:34:05 peter
  1153. * readded missing revisions
  1154. Revision 1.24 2002/05/16 19:46:35 carl
  1155. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1156. + try to fix temp allocation (still in ifdef)
  1157. + generic constructor calls
  1158. + start of tassembler / tmodulebase class cleanup
  1159. Revision 1.23 2002/05/14 19:34:40 peter
  1160. * removed old logs and updated copyright year
  1161. Revision 1.22 2002/05/13 19:54:36 peter
  1162. * removed n386ld and n386util units
  1163. * maybe_save/maybe_restore added instead of the old maybe_push
  1164. Revision 1.21 2002/05/12 19:57:16 carl
  1165. * maybe_loadself portable
  1166. Revision 1.20 2002/05/12 16:53:04 peter
  1167. * moved entry and exitcode to ncgutil and cgobj
  1168. * foreach gets extra argument for passing local data to the
  1169. iterator function
  1170. * -CR checks also class typecasts at runtime by changing them
  1171. into as
  1172. * fixed compiler to cycle with the -CR option
  1173. * fixed stabs with elf writer, finally the global variables can
  1174. be watched
  1175. * removed a lot of routines from cga unit and replaced them by
  1176. calls to cgobj
  1177. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1178. u32bit then the other is typecasted also to u32bit without giving
  1179. a rangecheck warning/error.
  1180. * fixed pascal calling method with reversing also the high tree in
  1181. the parast, detected by tcalcst3 test
  1182. Revision 1.19 2002/04/26 15:19:04 peter
  1183. * use saveregisters for incr routines, saves also problems with
  1184. the optimizer
  1185. Revision 1.18 2002/04/25 20:16:38 peter
  1186. * moved more routines from cga/n386util
  1187. Revision 1.17 2002/04/22 16:30:05 peter
  1188. * fixed @methodpointer
  1189. Revision 1.16 2002/04/21 15:25:30 carl
  1190. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1191. + changeregsize -> rg.makeregsize
  1192. Revision 1.15 2002/04/20 21:32:23 carl
  1193. + generic FPC_CHECKPOINTER
  1194. + first parameter offset in stack now portable
  1195. * rename some constants
  1196. + move some cpu stuff to other units
  1197. - remove unused constents
  1198. * fix stacksize for some targets
  1199. * fix generic size problems which depend now on EXTEND_SIZE constant
  1200. Revision 1.14 2002/04/15 19:44:18 peter
  1201. * fixed stackcheck that would be called recursively when a stack
  1202. error was found
  1203. * generic changeregsize(reg,size) for i386 register resizing
  1204. * removed some more routines from cga unit
  1205. * fixed returnvalue handling
  1206. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1207. Revision 1.13 2002/04/07 13:22:11 carl
  1208. + more documentation
  1209. Revision 1.12 2002/04/07 09:12:46 carl
  1210. + documentation
  1211. Revision 1.11 2002/04/06 18:10:42 jonas
  1212. * several powerpc-related additions and fixes
  1213. Revision 1.10 2002/04/04 19:05:54 peter
  1214. * removed unused units
  1215. * use tlocation.size in cg.a_*loc*() routines
  1216. Revision 1.9 2002/04/02 17:11:27 peter
  1217. * tlocation,treference update
  1218. * LOC_CONSTANT added for better constant handling
  1219. * secondadd splitted in multiple routines
  1220. * location_force_reg added for loading a location to a register
  1221. of a specified size
  1222. * secondassignment parses now first the right and then the left node
  1223. (this is compatible with Kylix). This saves a lot of push/pop especially
  1224. with string operations
  1225. * adapted some routines to use the new cg methods
  1226. Revision 1.8 2002/03/31 20:26:33 jonas
  1227. + a_loadfpu_* and a_loadmm_* methods in tcg
  1228. * register allocation is now handled by a class and is mostly processor
  1229. independent (+rgobj.pas and i386/rgcpu.pas)
  1230. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1231. * some small improvements and fixes to the optimizer
  1232. * some register allocation fixes
  1233. * some fpuvaroffset fixes in the unary minus node
  1234. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1235. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1236. also better optimizable)
  1237. * fixed and optimized register saving/restoring for new/dispose nodes
  1238. * LOC_FPU locations now also require their "register" field to be set to
  1239. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1240. - list field removed of the tnode class because it's not used currently
  1241. and can cause hard-to-find bugs
  1242. Revision 1.7 2002/03/04 19:10:11 peter
  1243. * removed compiler warnings
  1244. }