cgobj.pas 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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. WARNING: None of the routines implemented in these modules,
  23. or their descendants, should use the temp. allocator, as
  24. these routines may be called inside genentrycode, and the
  25. stack frame is already setup!
  26. }
  27. unit cgobj;
  28. {$i fpcdefs.inc}
  29. interface
  30. uses
  31. cclasses,aasmbase,aasmtai,aasmcpu,symtable,
  32. cpubase,cpuinfo,cpupara,
  33. cginfo,
  34. symconst,symbase,symtype,symdef,node
  35. {$ifdef delphi}
  36. ,dmisc
  37. {$endif}
  38. ;
  39. type
  40. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  41. {# @abstract(Abstract code generator)
  42. This class implements an abstract instruction generator. Some of
  43. the methods of this class are generic, while others must
  44. be overriden for all new processors which will be supported
  45. by Free Pascal. For 32-bit processors, the base class
  46. sould be @link(tcg64f32) and not @var(tcg).
  47. }
  48. tcg = class
  49. scratch_register_array_pointer : aword;
  50. {# List of currently unused scratch registers }
  51. unusedscratchregisters:Tsupregset;
  52. alignment : talignment;
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. { returns the tcgsize corresponding with the size of reg }
  57. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  58. {# Emit a label to the instruction stream. }
  59. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  60. {# Allocates register r by inserting a pai_realloc record }
  61. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  62. {# Deallocates register r by inserting a pa_regdealloc record}
  63. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  64. {# @abstract(Returns an int register for use as scratch register)
  65. This routine returns a register which can be used by
  66. the code generator as a general purpose 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_int(list : taasmoutput;size:Tcgsize) : tregister;virtual;
  72. {# @abstract(Returns an address register for use as scratch register)
  73. This routine returns a register which can be used by
  74. the code generator as a pointer scratch register.
  75. Since scratch_registers are scarce resources, the register
  76. should be freed by calling @link(free_scratch_reg) as
  77. soon as it is no longer required.
  78. }
  79. function get_scratch_reg_address(list : taasmoutput) : tregister;virtual;
  80. {# @abstract(Releases a scratch register)
  81. Releases a scratch register.
  82. This routine is used to free a register which
  83. was previously allocated using @link(get_scratch_reg).
  84. }
  85. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  86. {# Pass a parameter, which is located in a register, to a routine.
  87. This routine should push/send the parameter to the routine, as
  88. required by the specific processor ABI and routine modifiers.
  89. This must be overriden for each CPU target.
  90. @param(size size of the operand in the register)
  91. @param(r register source of the operand)
  92. @param(locpara where the parameter will be stored)
  93. }
  94. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  95. {# Pass a parameter, which is a constant, to a routine.
  96. A generic version is provided. This routine should
  97. be overriden for optimization purposes if the cpu
  98. permits directly sending this type of parameter.
  99. @param(size size of the operand in constant)
  100. @param(a value of constant to send)
  101. @param(locpara where the parameter will be stored)
  102. }
  103. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  104. {# Pass the value of a parameter, which is located in memory, to a routine.
  105. A generic version is provided. This routine should
  106. be overriden for optimization purposes if the cpu
  107. permits directly sending this type of parameter.
  108. @param(size size of the operand in constant)
  109. @param(r Memory reference of value to send)
  110. @param(locpara where the parameter will be stored)
  111. }
  112. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  113. {# Pass the value of a parameter, which can be located either in a register or memory location,
  114. to a routine.
  115. A generic version is provided.
  116. @param(l location of the operand to send)
  117. @param(nr parameter number (starting from one) of routine (from left to right))
  118. @param(locpara where the parameter will be stored)
  119. }
  120. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  121. {# Pass the address of a reference to a routine. This routine
  122. will calculate the address of the reference, and pass this
  123. calculated address as a parameter.
  124. A generic version is provided. This routine should
  125. be overriden for optimization purposes if the cpu
  126. permits directly sending this type of parameter.
  127. @param(r reference to get address from)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. }
  130. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  131. { Copies a whole memory block to the stack, the locpara must be a memory location }
  132. procedure a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  133. { Remarks:
  134. * If a method specifies a size you have only to take care
  135. of that number of bits, i.e. load_const_reg with OP_8 must
  136. only load the lower 8 bit of the specified register
  137. the rest of the register can be undefined
  138. if necessary the compiler will call a method
  139. to zero or sign extend the register
  140. * The a_load_XX_XX with OP_64 needn't to be
  141. implemented for 32 bit
  142. processors, the code generator takes care of that
  143. * the addr size is for work with the natural pointer
  144. size
  145. * the procedures without fpu/mm are only for integer usage
  146. * normally the first location is the source and the
  147. second the destination
  148. }
  149. {# Emits instruction to call the method specified by symbol name.
  150. This routine must be overriden for each new target cpu.
  151. }
  152. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  153. procedure a_call_ref(list : taasmoutput;const ref : treference);virtual;abstract;
  154. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  155. procedure a_call_loc(list : taasmoutput;const loc:tlocation);
  156. { move instructions }
  157. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  158. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  159. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  160. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  161. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  162. procedure a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  163. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  164. procedure a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);virtual;
  165. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  166. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  167. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  168. { fpu move instructions }
  169. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  170. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  171. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  172. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  173. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  174. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);virtual;
  175. { vector register move instructions }
  176. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  177. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  178. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  179. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  180. { basic arithmetic operations }
  181. { note: for operators which require only one argument (not, neg), use }
  182. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  183. { that in this case the *second* operand is used as both source and }
  184. { destination (JM) }
  185. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  186. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  187. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  188. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  189. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  190. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  191. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  192. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  193. { trinary operations for processors that support them, 'emulated' }
  194. { on others. None with "ref" arguments since I don't think there }
  195. { are any processors that support it (JM) }
  196. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  197. size: tcgsize; a: aword; src, dst: tregister); virtual;
  198. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  199. size: tcgsize; src1, src2, dst: tregister); virtual;
  200. { comparison operations }
  201. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  202. l : tasmlabel);virtual; abstract;
  203. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  204. l : tasmlabel); virtual;
  205. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  206. l : tasmlabel);
  207. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  208. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  209. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  210. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  211. l : tasmlabel);
  212. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  213. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  214. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  215. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  216. }
  217. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  218. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  219. {
  220. This routine tries to optimize the const_reg opcode, and should be
  221. called at the start of a_op_const_reg. It returns the actual opcode
  222. to emit, and the constant value to emit. If this routine returns
  223. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  224. @param(op The opcode to emit, returns the opcode which must be emitted)
  225. @param(a The constant which should be emitted, returns the constant which must
  226. be emitted)
  227. @param(reg The register to emit the opcode with, returns the register with
  228. which the opcode will be emitted)
  229. }
  230. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg: tregister): boolean;virtual;
  231. {#
  232. This routine is used in exception management nodes. It should
  233. save the exception reason currently in the accumulator. The
  234. save should be done either to a temp (pointed to by href).
  235. or on the stack (pushing the value on the stack).
  236. The size of the value to save is OS_S32. The default version
  237. saves the exception reason to a temp. memory area.
  238. }
  239. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  240. {#
  241. This routine is used in exception management nodes. It should
  242. save the exception reason constant. The
  243. save should be done either to a temp (pointed to by href).
  244. or on the stack (pushing the value on the stack).
  245. The size of the value to save is OS_S32. The default version
  246. saves the exception reason to a temp. memory area.
  247. }
  248. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);virtual;
  249. {#
  250. This routine is used in exception management nodes. It should
  251. load the exception reason to the accumulator. The saved value
  252. should either be in the temp. area (pointed to by href , href should
  253. *NOT* be freed) or on the stack (the value should be popped).
  254. The size of the value to save is OS_S32. The default version
  255. saves the exception reason to a temp. memory area.
  256. }
  257. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  258. procedure g_maybe_loadself(list : taasmoutput);virtual;
  259. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  260. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  261. {# This should emit the opcode to copy len bytes from the source
  262. to destination, if loadref is true, it assumes that it first must load
  263. the source address from the memory location where
  264. source points to.
  265. It must be overriden for each new target processor.
  266. @param(source Source reference of copy)
  267. @param(dest Destination reference of copy)
  268. @param(delsource Indicates if the source reference's resources should be freed)
  269. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  270. }
  271. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  272. {# This should emit the opcode to a shortrstring from the source
  273. to destination, if loadref is true, it assumes that it first must load
  274. the source address from the memory location where
  275. source points to.
  276. @param(source Source reference of copy)
  277. @param(dest Destination reference of copy)
  278. @param(delsource Indicates if the source reference's resources should be freed)
  279. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  280. }
  281. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  282. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  283. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  284. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  285. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  286. {# Emits the call to the stack checking routine of
  287. the runtime library. The default behavior
  288. does not need to be modified, as it is generic
  289. for all platforms.
  290. @param(stackframesize Number of bytes which will be allocated on the stack)
  291. }
  292. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  293. {# Generates range checking code. It is to note
  294. that this routine does not need to be overriden,
  295. as it takes care of everything.
  296. @param(p Node which contains the value to check)
  297. @param(todef Type definition of node to range check)
  298. }
  299. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  300. const todef: tdef); virtual;
  301. {# Generates overflow checking code for a node }
  302. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  303. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  304. procedure g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  305. {# Emits instructions which should be emitted when entering
  306. a routine declared as @var(interrupt). The default
  307. behavior does nothing, should be overriden as required.
  308. }
  309. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  310. {# Emits instructions which should be emitted when exiting
  311. a routine declared as @var(interrupt). The default
  312. behavior does nothing, should be overriden as required.
  313. }
  314. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;
  315. {# Emits instructions when compilation is done in profile
  316. mode (this is set as a command line option). The default
  317. behavior does nothing, should be overriden as required.
  318. }
  319. procedure g_profilecode(list : taasmoutput);virtual;
  320. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  321. @param(size Number of bytes to allocate)
  322. }
  323. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  324. {# Emits instruction for allocating the locals in entry
  325. code of a routine. This is one of the first
  326. routine called in @var(genentrycode).
  327. @param(localsize Number of bytes to allocate as locals)
  328. }
  329. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  330. {# Emits instructiona for restoring the frame pointer
  331. at routine exit. For some processors, this routine
  332. may do nothing at all.
  333. }
  334. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  335. {# Emits instructions for returning from a subroutine.
  336. Should also restore the stack.
  337. @param(parasize Number of bytes of parameters to deallocate from stack)
  338. }
  339. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  340. procedure g_call_constructor_helper(list : taasmoutput);virtual;
  341. procedure g_call_destructor_helper(list : taasmoutput);virtual;
  342. procedure g_call_fail_helper(list : taasmoutput);virtual;
  343. {# This routine is called when generating the code for the entry point
  344. of a routine. It should save all registers which are not used in this
  345. routine, and which should be declared as saved in the std_saved_registers
  346. set.
  347. This routine is mainly used when linking to code which is generated
  348. by ABI-compliant compilers (like GCC), to make sure that the reserved
  349. registers of that ABI are not clobbered.
  350. @param(usedinproc Registers which are used in the code of this routine)
  351. }
  352. procedure g_save_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);virtual;abstract;
  353. {# This routine is called when generating the code for the exit point
  354. of a routine. It should restore all registers which were previously
  355. saved in @var(g_save_standard_registers).
  356. @param(usedinproc Registers which are used in the code of this routine)
  357. }
  358. procedure g_restore_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);virtual;abstract;
  359. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  360. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;abstract;
  361. end;
  362. {# @abstract(Abstract code generator for 64 Bit operations)
  363. This class implements an abstract code generator class
  364. for 64 Bit operations.
  365. }
  366. tcg64 = class
  367. { Allocates 64 Bit register r by inserting a pai_realloc record }
  368. procedure a_reg_alloc(list : taasmoutput;r : tregister64);virtual;abstract;
  369. { Deallocates 64 Bit register r by inserting a pa_regdealloc record}
  370. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);virtual;abstract;
  371. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  372. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  373. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  374. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  375. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  376. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  377. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  378. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  379. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  380. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  381. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  382. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  383. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  384. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  385. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  386. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  387. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  388. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  389. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  390. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  391. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  392. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  393. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  394. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  395. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  396. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  397. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  398. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  399. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  400. {
  401. This routine tries to optimize the const_reg opcode, and should be
  402. called at the start of a_op64_const_reg. It returns the actual opcode
  403. to emit, and the constant value to emit. If this routine returns
  404. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  405. @param(op The opcode to emit, returns the opcode which must be emitted)
  406. @param(a The constant which should be emitted, returns the constant which must
  407. be emitted)
  408. @param(reg The register to emit the opcode with, returns the register with
  409. which the opcode will be emitted)
  410. }
  411. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;virtual;abstract;
  412. { override to catch 64bit rangechecks }
  413. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  414. const todef: tdef);virtual;abstract;
  415. end;
  416. var
  417. {# Main code generator class }
  418. cg : tcg;
  419. {# Code generator class for all operations working with 64-Bit operands }
  420. cg64 : tcg64;
  421. implementation
  422. uses
  423. globals,globtype,options,systems,cgbase,
  424. verbose,defutil,tgobj,paramgr,
  425. rgobj,cutils;
  426. const
  427. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  428. {*****************************************************************************
  429. basic functionallity
  430. ******************************************************************************}
  431. constructor tcg.create;
  432. var
  433. i : longint;
  434. begin
  435. scratch_register_array_pointer:=1;
  436. for i:=low(scratch_regs) to high(scratch_regs) do
  437. include(unusedscratchregisters,scratch_regs[i]);
  438. end;
  439. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  440. begin
  441. list.concat(tai_regalloc.alloc(r));
  442. end;
  443. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  444. begin
  445. list.concat(tai_regalloc.dealloc(r));
  446. end;
  447. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  448. begin
  449. list.concat(tai_label.create(l));
  450. end;
  451. function tcg.get_scratch_reg_int(list:taasmoutput;size:Tcgsize):tregister;
  452. var
  453. r : tregister;
  454. i : longint;
  455. begin
  456. if unusedscratchregisters=[] then
  457. internalerror(68996);
  458. for i:=scratch_register_array_pointer to
  459. (scratch_register_array_pointer+max_scratch_regs-1) do
  460. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  461. begin
  462. r.enum:=R_INTREGISTER;
  463. r.number:=scratch_regs[(i mod max_scratch_regs)+1] shl 8 or cgsize2subreg(size);
  464. break;
  465. end;
  466. exclude(unusedscratchregisters,r.number shr 8);
  467. {$ifndef i386}
  468. include(rg.usedintbyproc,r.number shr 8);
  469. {$endif i386}
  470. inc(scratch_register_array_pointer);
  471. if scratch_register_array_pointer>max_scratch_regs then
  472. scratch_register_array_pointer:=1;
  473. a_reg_alloc(list,r);
  474. get_scratch_reg_int:=r;
  475. end;
  476. { the default behavior simply returns a general purpose register }
  477. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  478. begin
  479. get_scratch_reg_address := get_scratch_reg_int(list,OS_ADDR);
  480. end;
  481. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  482. begin
  483. if r.enum<>R_INTREGISTER then
  484. internalerror(200302058);
  485. include(unusedscratchregisters,r.number shr 8);
  486. a_reg_dealloc(list,r);
  487. end;
  488. {*****************************************************************************
  489. for better code generation these methods should be overridden
  490. ******************************************************************************}
  491. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  492. var
  493. ref : treference;
  494. t : Tregister;
  495. begin
  496. case locpara.loc of
  497. LOC_REGISTER,LOC_CREGISTER:
  498. a_load_reg_reg(list,size,locpara.size,r,locpara.register);
  499. LOC_REFERENCE,LOC_CREFERENCE:
  500. begin
  501. if locpara.sp_fixup<>0 then
  502. begin
  503. t.enum:=R_INTREGISTER;;
  504. t.number:=NR_STACK_POINTER_REG;
  505. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  506. end;
  507. reference_reset(ref);
  508. ref.base:=locpara.reference.index;
  509. ref.offset:=locpara.reference.offset;
  510. a_load_reg_ref(list,size,r,ref);
  511. end
  512. else
  513. internalerror(2002071004);
  514. end;
  515. end;
  516. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  517. var
  518. hr : tregister;
  519. begin
  520. hr:=get_scratch_reg_int(list,size);
  521. a_load_const_reg(list,size,a,hr);
  522. a_param_reg(list,size,hr,locpara);
  523. free_scratch_reg(list,hr);
  524. end;
  525. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  526. var
  527. hr : tregister;
  528. begin
  529. hr:=get_scratch_reg_int(list,size);
  530. a_load_ref_reg(list,size,r,hr);
  531. a_param_reg(list,size,hr,locpara);
  532. free_scratch_reg(list,hr);
  533. end;
  534. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  535. begin
  536. case l.loc of
  537. LOC_REGISTER,
  538. LOC_CREGISTER :
  539. a_param_reg(list,l.size,l.register,locpara);
  540. LOC_CONSTANT :
  541. a_param_const(list,l.size,l.value,locpara);
  542. LOC_CREFERENCE,
  543. LOC_REFERENCE :
  544. a_param_ref(list,l.size,l.reference,locpara);
  545. else
  546. internalerror(2002032211);
  547. end;
  548. end;
  549. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  550. var
  551. hr : tregister;
  552. begin
  553. hr:=get_scratch_reg_address(list);
  554. a_loadaddr_ref_reg(list,r,hr);
  555. a_param_reg(list,OS_ADDR,hr,locpara);
  556. free_scratch_reg(list,hr);
  557. end;
  558. procedure tcg.a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  559. var
  560. ref : treference;
  561. hr : tregister;
  562. begin
  563. if not(locpara.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  564. internalerror(2003010901);
  565. if locpara.sp_fixup<>0 then
  566. cg.g_stackpointer_alloc(list,locpara.sp_fixup);
  567. reference_reset(ref);
  568. ref.base:=locpara.reference.index;
  569. ref.offset:=locpara.reference.offset;
  570. cg.g_concatcopy(list,r,ref,size,false,false);
  571. end;
  572. {****************************************************************************
  573. some generic implementations
  574. ****************************************************************************}
  575. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  576. var
  577. tmpreg: tregister;
  578. {$ifdef i386}
  579. pushed_reg: tregister;
  580. {$endif i386}
  581. begin
  582. { verify if we have the same reference }
  583. if references_equal(sref,dref) then
  584. exit;
  585. {$ifdef i386}
  586. { the following is done with defines to avoid a speed penalty, }
  587. { since all this is only necessary for the 80x86 (because EDI }
  588. { doesn't have an 8bit component which is directly addressable) }
  589. pushed_reg.enum:=R_INTREGISTER;
  590. pushed_reg.number:=NR_NO;
  591. if size in [OS_8,OS_S8] then
  592. if (rg.countunusedregsint = 0) then
  593. begin
  594. if (dref.base.enum<>R_NO) and (dref.base.enum<>R_INTREGISTER) then
  595. internalerror(200302037);
  596. if (dref.index.enum<>R_NO) and (dref.index.enum<>R_INTREGISTER) then
  597. internalerror(200302037);
  598. if (dref.base.number shr 8<>RS_EBX) and
  599. (dref.index.number shr 8<>RS_EBX) then
  600. pushed_reg.number:=NR_EBX
  601. else if (dref.base.number<>RS_EAX) and
  602. (dref.index.number<>RS_EAX) then
  603. pushed_reg.number:=NR_EAX
  604. else
  605. pushed_reg.number:=NR_ECX;
  606. tmpreg.enum:=R_INTREGISTER;
  607. tmpreg.number:=(pushed_reg.number and not $ff) or R_SUBL;
  608. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  609. end
  610. else
  611. tmpreg := rg.getregisterint(list,size)
  612. else
  613. {$endif i386}
  614. tmpreg := get_scratch_reg_int(list,size);
  615. a_load_ref_reg(list,size,sref,tmpreg);
  616. a_load_reg_ref(list,size,tmpreg,dref);
  617. {$ifdef i386}
  618. if size in [OS_8,OS_S8] then
  619. begin
  620. if (pushed_reg.number<>NR_NO) then
  621. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  622. else
  623. rg.ungetregisterint(list,tmpreg)
  624. end
  625. else
  626. {$endif i386}
  627. free_scratch_reg(list,tmpreg);
  628. end;
  629. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  630. var
  631. tmpreg: tregister;
  632. begin
  633. tmpreg := get_scratch_reg_int(list,size);
  634. a_load_const_reg(list,size,a,tmpreg);
  635. a_load_reg_ref(list,size,tmpreg,ref);
  636. free_scratch_reg(list,tmpreg);
  637. end;
  638. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  639. begin
  640. case loc.loc of
  641. LOC_REFERENCE,LOC_CREFERENCE:
  642. a_load_const_ref(list,loc.size,a,loc.reference);
  643. LOC_REGISTER,LOC_CREGISTER:
  644. a_load_const_reg(list,loc.size,a,loc.register);
  645. else
  646. internalerror(200203272);
  647. end;
  648. end;
  649. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  650. begin
  651. case loc.loc of
  652. LOC_REFERENCE,LOC_CREFERENCE:
  653. a_load_reg_ref(list,size,reg,loc.reference);
  654. LOC_REGISTER,LOC_CREGISTER:
  655. a_load_reg_reg(list,size,loc.size,reg,loc.register);
  656. else
  657. internalerror(200203271);
  658. end;
  659. end;
  660. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  661. var r:Tregister;
  662. begin
  663. case loc.loc of
  664. LOC_REFERENCE,LOC_CREFERENCE:
  665. a_load_ref_reg(list,loc.size,loc.reference,reg);
  666. LOC_REGISTER,LOC_CREGISTER:
  667. a_load_reg_reg(list,loc.size,loc.size,loc.register,reg);
  668. LOC_CONSTANT:
  669. a_load_const_reg(list,loc.size,loc.value,reg);
  670. else
  671. internalerror(200109092);
  672. end;
  673. end;
  674. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  675. begin
  676. case loc.loc of
  677. LOC_REFERENCE,LOC_CREFERENCE:
  678. a_load_ref_ref(list,loc.size,loc.reference,ref);
  679. LOC_REGISTER,LOC_CREGISTER:
  680. a_load_reg_ref(list,loc.size,loc.register,ref);
  681. LOC_CONSTANT:
  682. a_load_const_ref(list,loc.size,loc.value,ref);
  683. else
  684. internalerror(200109302);
  685. end;
  686. end;
  687. procedure tcg.a_call_loc(list : taasmoutput;const loc:tlocation);
  688. begin
  689. case loc.loc of
  690. LOC_REGISTER,LOC_CREGISTER:
  691. cg.a_call_reg(list,loc.register);
  692. LOC_REFERENCE,LOC_CREFERENCE :
  693. cg.a_call_ref(list,loc.reference);
  694. else
  695. internalerror(200203311);
  696. end;
  697. end;
  698. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  699. var
  700. powerval : longint;
  701. begin
  702. optimize_op_const_reg := false;
  703. case op of
  704. { or with zero returns same result }
  705. OP_OR : if a = 0 then optimize_op_const_reg := true;
  706. { and with max returns same result }
  707. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  708. { division by 1 returns result }
  709. OP_DIV :
  710. begin
  711. if a = 1 then
  712. optimize_op_const_reg := true
  713. else if ispowerof2(int64(a), powerval) then
  714. begin
  715. a := powerval;
  716. op:= OP_SHR;
  717. end;
  718. exit;
  719. end;
  720. OP_IDIV:
  721. begin
  722. if a = 1 then
  723. optimize_op_const_reg := true
  724. else if ispowerof2(int64(a), powerval) then
  725. begin
  726. a := powerval;
  727. op:= OP_SAR;
  728. end;
  729. exit;
  730. end;
  731. OP_MUL,OP_IMUL:
  732. begin
  733. if a = 1 then
  734. optimize_op_const_reg := true
  735. else if ispowerof2(int64(a), powerval) then
  736. begin
  737. a := powerval;
  738. op:= OP_SHL;
  739. end;
  740. exit;
  741. end;
  742. OP_SAR,OP_SHL,OP_SHR:
  743. begin
  744. if a = 0 then
  745. optimize_op_const_reg := true;
  746. exit;
  747. end;
  748. end;
  749. end;
  750. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  751. begin
  752. case loc.loc of
  753. LOC_REFERENCE, LOC_CREFERENCE:
  754. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  755. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  756. a_loadfpu_reg_reg(list,loc.register,reg);
  757. else
  758. internalerror(200203301);
  759. end;
  760. end;
  761. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  762. begin
  763. case loc.loc of
  764. LOC_REFERENCE, LOC_CREFERENCE:
  765. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  766. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  767. a_loadfpu_reg_reg(list,reg,loc.register);
  768. else
  769. internalerror(48991);
  770. end;
  771. end;
  772. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  773. var
  774. ref : treference;
  775. t : Tregister;
  776. begin
  777. case locpara.loc of
  778. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  779. a_loadfpu_reg_reg(list,r,locpara.register);
  780. LOC_REFERENCE,LOC_CREFERENCE:
  781. begin
  782. if locpara.sp_fixup<>0 then
  783. begin
  784. t.enum:=R_INTREGISTER;
  785. t.number:=NR_STACK_POINTER_REG;
  786. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  787. end;
  788. reference_reset(ref);
  789. ref.base:=locpara.reference.index;
  790. ref.offset:=locpara.reference.offset;
  791. a_loadfpu_reg_ref(list,size,r,ref);
  792. end
  793. else
  794. internalerror(2002071004);
  795. end;
  796. end;
  797. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  798. var
  799. tmpreg: tregister;
  800. begin
  801. tmpreg := get_scratch_reg_int(list,size);
  802. a_load_ref_reg(list,size,ref,tmpreg);
  803. a_op_const_reg(list,op,a,tmpreg);
  804. a_load_reg_ref(list,size,tmpreg,ref);
  805. free_scratch_reg(list,tmpreg);
  806. end;
  807. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  808. begin
  809. case loc.loc of
  810. LOC_REGISTER, LOC_CREGISTER:
  811. a_op_const_reg(list,op,a,loc.register);
  812. LOC_REFERENCE, LOC_CREFERENCE:
  813. a_op_const_ref(list,op,loc.size,a,loc.reference);
  814. else
  815. internalerror(200109061);
  816. end;
  817. end;
  818. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  819. var
  820. tmpreg: tregister;
  821. begin
  822. tmpreg := get_scratch_reg_int(list,size);
  823. a_load_ref_reg(list,size,ref,tmpreg);
  824. a_op_reg_reg(list,op,size,reg,tmpreg);
  825. a_load_reg_ref(list,size,tmpreg,ref);
  826. free_scratch_reg(list,tmpreg);
  827. end;
  828. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  829. var
  830. tmpreg: tregister;
  831. begin
  832. case op of
  833. OP_NOT,OP_NEG:
  834. { handle it as "load ref,reg; op reg" }
  835. begin
  836. a_load_ref_reg(list,size,ref,reg);
  837. a_op_reg_reg(list,op,size,reg,reg);
  838. end;
  839. else
  840. begin
  841. tmpreg := get_scratch_reg_int(list,size);
  842. a_load_ref_reg(list,size,ref,tmpreg);
  843. a_op_reg_reg(list,op,size,tmpreg,reg);
  844. free_scratch_reg(list,tmpreg);
  845. end;
  846. end;
  847. end;
  848. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  849. begin
  850. case loc.loc of
  851. LOC_REGISTER, LOC_CREGISTER:
  852. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  853. LOC_REFERENCE, LOC_CREFERENCE:
  854. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  855. else
  856. internalerror(200109061);
  857. end;
  858. end;
  859. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  860. var
  861. tmpreg: tregister;
  862. begin
  863. case loc.loc of
  864. LOC_REGISTER,LOC_CREGISTER:
  865. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  866. LOC_REFERENCE,LOC_CREFERENCE:
  867. begin
  868. tmpreg := get_scratch_reg_int(list,loc.size);
  869. a_load_ref_reg(list,loc.size,ref,tmpreg);
  870. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  871. free_scratch_reg(list,tmpreg);
  872. end;
  873. else
  874. internalerror(200109061);
  875. end;
  876. end;
  877. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  878. size: tcgsize; a: aword; src, dst: tregister);
  879. begin
  880. a_load_reg_reg(list,size,size,src,dst);
  881. a_op_const_reg(list,op,a,dst);
  882. end;
  883. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  884. size: tcgsize; src1, src2, dst: tregister);
  885. begin
  886. a_load_reg_reg(list,size,size,src2,dst);
  887. a_op_reg_reg(list,op,size,src1,dst);
  888. end;
  889. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  890. l : tasmlabel);
  891. var
  892. tmpreg: tregister;
  893. begin
  894. tmpreg := get_scratch_reg_int(list,size);
  895. a_load_ref_reg(list,size,ref,tmpreg);
  896. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  897. free_scratch_reg(list,tmpreg);
  898. end;
  899. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  900. l : tasmlabel);
  901. begin
  902. case loc.loc of
  903. LOC_REGISTER,LOC_CREGISTER:
  904. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  905. LOC_REFERENCE,LOC_CREFERENCE:
  906. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  907. else
  908. internalerror(200109061);
  909. end;
  910. end;
  911. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  912. var
  913. tmpreg: tregister;
  914. begin
  915. tmpreg := get_scratch_reg_int(list,size);
  916. a_load_ref_reg(list,size,ref,tmpreg);
  917. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  918. free_scratch_reg(list,tmpreg);
  919. end;
  920. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  921. begin
  922. case loc.loc of
  923. LOC_REGISTER,
  924. LOC_CREGISTER:
  925. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  926. LOC_REFERENCE,
  927. LOC_CREFERENCE :
  928. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  929. LOC_CONSTANT:
  930. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  931. else
  932. internalerror(200203231);
  933. end;
  934. end;
  935. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  936. l : tasmlabel);
  937. var
  938. tmpreg: tregister;
  939. begin
  940. case loc.loc of
  941. LOC_REGISTER,LOC_CREGISTER:
  942. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  943. LOC_REFERENCE,LOC_CREFERENCE:
  944. begin
  945. {$ifdef i386}
  946. { the following is done with defines to avoid a speed penalty, }
  947. { since all this is only necessary for the 80x86 (because EDI }
  948. { doesn't have an 8bit component which is directly addressable) }
  949. if size in [OS_8,OS_S8] then
  950. tmpreg := rg.getregisterint(list,size)
  951. else
  952. {$endif i386}
  953. tmpreg := get_scratch_reg_int(list,size);
  954. {tmpreg := rg.makeregsize(tmpreg,size);}
  955. a_load_ref_reg(list,size,loc.reference,tmpreg);
  956. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  957. {$ifdef i386}
  958. if size in [OS_8,OS_S8] then
  959. rg.ungetregisterint(list,tmpreg)
  960. else
  961. {$endif i386}
  962. free_scratch_reg(list,tmpreg);
  963. end
  964. else
  965. internalerror(200109061);
  966. end;
  967. end;
  968. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  969. begin
  970. reg_cgsize := OS_INT;
  971. end;
  972. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  973. begin
  974. {$ifdef FPC}
  975. {$warning FIX ME!}
  976. {$endif}
  977. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  978. if loadref then
  979. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  980. else
  981. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  982. if delsource then
  983. reference_release(list,source);
  984. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  985. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  986. g_maybe_loadself(list);
  987. end;
  988. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  989. var
  990. href : treference;
  991. incrfunc : string;
  992. begin
  993. { These functions should not change the registers (they use
  994. the saveregister proc directive }
  995. if is_interfacecom(t) then
  996. incrfunc:='FPC_INTF_INCR_REF'
  997. else if is_ansistring(t) then
  998. incrfunc:='FPC_ANSISTR_INCR_REF'
  999. else if is_widestring(t) then
  1000. incrfunc:='FPC_WIDESTR_INCR_REF'
  1001. else if is_dynamic_array(t) then
  1002. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1003. else
  1004. incrfunc:='';
  1005. { call the special incr function or the generic addref }
  1006. if incrfunc<>'' then
  1007. begin
  1008. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  1009. a_call_name(list,incrfunc);
  1010. end
  1011. else
  1012. begin
  1013. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1014. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1015. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1016. a_call_name(list,'FPC_ADDREF');
  1017. end;
  1018. end;
  1019. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1020. var
  1021. href : treference;
  1022. decrfunc : string;
  1023. needrtti : boolean;
  1024. begin
  1025. needrtti:=false;
  1026. if is_interfacecom(t) then
  1027. decrfunc:='FPC_INTF_DECR_REF'
  1028. else if is_ansistring(t) then
  1029. decrfunc:='FPC_ANSISTR_DECR_REF'
  1030. else if is_widestring(t) then
  1031. decrfunc:='FPC_WIDESTR_DECR_REF'
  1032. else if is_dynamic_array(t) then
  1033. begin
  1034. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1035. needrtti:=true;
  1036. end
  1037. else
  1038. decrfunc:='';
  1039. { call the special decr function or the generic decref }
  1040. if decrfunc<>'' then
  1041. begin
  1042. if needrtti then
  1043. begin
  1044. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1045. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1046. end;
  1047. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1048. a_call_name(list,decrfunc);
  1049. end
  1050. else
  1051. begin
  1052. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1053. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1054. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1055. a_call_name(list,'FPC_DECREF');
  1056. end;
  1057. end;
  1058. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1059. var
  1060. href : treference;
  1061. begin
  1062. if is_ansistring(t) or
  1063. is_widestring(t) or
  1064. is_interfacecom(t) then
  1065. a_load_const_ref(list,OS_ADDR,0,ref)
  1066. else
  1067. begin
  1068. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1069. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1070. if loadref then
  1071. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1072. else
  1073. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1074. a_call_name(list,'FPC_INITIALIZE');
  1075. end;
  1076. end;
  1077. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1078. var
  1079. href : treference;
  1080. begin
  1081. if is_ansistring(t) or
  1082. is_widestring(t) or
  1083. is_interfacecom(t) then
  1084. g_decrrefcount(list,t,ref)
  1085. else
  1086. begin
  1087. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1088. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1089. if loadref then
  1090. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1091. else
  1092. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1093. a_call_name(list,'FPC_FINALIZE');
  1094. end;
  1095. end;
  1096. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1097. { generate range checking code for the value at location p. The type }
  1098. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1099. { is the original type used at that location. When both defs are equal }
  1100. { the check is also insert (needed for succ,pref,inc,dec) }
  1101. const
  1102. {$ifdef ver1_0}
  1103. awordsignedmax=high(longint);
  1104. {$else}
  1105. awordsignedmax=high(aword) div 2;
  1106. {$endif}
  1107. var
  1108. neglabel : tasmlabel;
  1109. hreg : tregister;
  1110. fromdef : tdef;
  1111. lto,hto,
  1112. lfrom,hfrom : TConstExprInt;
  1113. from_signed: boolean;
  1114. begin
  1115. { range checking on and range checkable value? }
  1116. if not(cs_check_range in aktlocalswitches) or
  1117. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1118. exit;
  1119. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  1120. begin
  1121. cg64.g_rangecheck64(list,p,todef);
  1122. exit;
  1123. end;
  1124. { only check when assigning to scalar, subranges are different, }
  1125. { when todef=fromdef then the check is always generated }
  1126. fromdef:=p.resulttype.def;
  1127. getrange(p.resulttype.def,lfrom,hfrom);
  1128. getrange(todef,lto,hto);
  1129. { no range check if from and to are equal and are both longint/dword }
  1130. { (if we have a 32bit processor) or int64/qword, since such }
  1131. { operations can at most cause overflows (JM) }
  1132. { Note that these checks are mostly processor independent, they only }
  1133. { have to be changed once we introduce 64bit subrange types }
  1134. if (fromdef = todef) and
  1135. (fromdef.deftype=orddef) and
  1136. (((sizeof(aword) = 4) and
  1137. (((torddef(fromdef).typ = s32bit) and
  1138. (lfrom = low(longint)) and
  1139. (hfrom = high(longint))) or
  1140. ((torddef(fromdef).typ = u32bit) and
  1141. (lfrom = low(cardinal)) and
  1142. (hfrom = high(cardinal)))))) then
  1143. exit;
  1144. if todef<>fromdef then
  1145. begin
  1146. { if the from-range falls completely in the to-range, no check }
  1147. { is necessary }
  1148. if (lto<=lfrom) and (hto>=hfrom) then
  1149. exit;
  1150. end;
  1151. { generate the rangecheck code for the def where we are going to }
  1152. { store the result }
  1153. { use the trick that }
  1154. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1155. { To be able to do that, we have to make sure however that either }
  1156. { fromdef and todef are both signed or unsigned, or that we leave }
  1157. { the parts < 0 and > maxlongint out }
  1158. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1159. from_signed := is_signed(fromdef);
  1160. if from_signed xor is_signed(todef) then
  1161. if from_signed then
  1162. { from is signed, to is unsigned }
  1163. begin
  1164. { if high(from) < 0 -> always range error }
  1165. if (hfrom < 0) or
  1166. { if low(to) > maxlongint also range error }
  1167. (lto > awordsignedmax) then
  1168. begin
  1169. a_call_name(list,'FPC_RANGEERROR');
  1170. exit
  1171. end;
  1172. { from is signed and to is unsigned -> when looking at from }
  1173. { as an unsigned value, it must be < maxlongint (otherwise }
  1174. { it's negative, which is invalid since "to" is unsigned) }
  1175. if hto > awordsignedmax then
  1176. hto := awordsignedmax;
  1177. end
  1178. else
  1179. { from is unsigned, to is signed }
  1180. begin
  1181. if (lfrom > awordsignedmax) or
  1182. (hto < 0) then
  1183. begin
  1184. a_call_name(list,'FPC_RANGEERROR');
  1185. exit
  1186. end;
  1187. { from is unsigned and to is signed -> when looking at to }
  1188. { as an unsigned value, it must be >= 0 (since negative }
  1189. { values are the same as values > maxlongint) }
  1190. if lto < 0 then
  1191. lto := 0;
  1192. end;
  1193. hreg := get_scratch_reg_int(list,OS_INT);
  1194. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1195. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1196. aword(lto),p.location.register,hreg)
  1197. else
  1198. begin
  1199. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1200. p.location.reference,hreg);
  1201. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1202. end;
  1203. objectlibrary.getlabel(neglabel);
  1204. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1205. { !!! should happen right after the compare (JM) }
  1206. free_scratch_reg(list,hreg);
  1207. a_call_name(list,'FPC_RANGEERROR');
  1208. a_label(list,neglabel);
  1209. end;
  1210. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1211. begin
  1212. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1213. a_call_name(list,'FPC_STACKCHECK');
  1214. end;
  1215. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1216. var
  1217. tmpreg : tregister;
  1218. begin
  1219. tmpreg := get_scratch_reg_int(list,size);
  1220. g_flags2reg(list,size,f,tmpreg);
  1221. a_load_reg_ref(list,size,tmpreg,ref);
  1222. free_scratch_reg(list,tmpreg);
  1223. end;
  1224. procedure tcg.g_maybe_loadself(list : taasmoutput);
  1225. var
  1226. hp : treference;
  1227. p : tprocinfo;
  1228. i : longint;
  1229. spr : Tregister;
  1230. begin
  1231. spr.enum:=R_INTREGISTER;
  1232. spr.number:=NR_SELF_POINTER_REG;
  1233. if assigned(procinfo._class) then
  1234. begin
  1235. list.concat(tai_regalloc.Alloc(spr));
  1236. if lexlevel>normal_function_level then
  1237. begin
  1238. reference_reset_base(hp,procinfo.framepointer,procinfo.framepointer_offset);
  1239. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1240. p:=procinfo.parent;
  1241. for i:=3 to lexlevel-1 do
  1242. begin
  1243. reference_reset_base(hp,spr,p.framepointer_offset);
  1244. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1245. p:=p.parent;
  1246. end;
  1247. reference_reset_base(hp,spr,p.selfpointer_offset);
  1248. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1249. end
  1250. else
  1251. begin
  1252. reference_reset_base(hp,procinfo.framepointer,procinfo.selfpointer_offset);
  1253. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1254. end;
  1255. end;
  1256. end;
  1257. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1258. var
  1259. OKLabel : tasmlabel;
  1260. dummyloc : tparalocation;
  1261. begin
  1262. if (cs_check_object in aktlocalswitches) or
  1263. (cs_check_range in aktlocalswitches) then
  1264. begin
  1265. objectlibrary.getlabel(oklabel);
  1266. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1267. a_param_const(list,OS_INT,210,dummyloc);
  1268. a_call_name(list,'FPC_HANDLEERROR');
  1269. a_label(list,oklabel);
  1270. end;
  1271. end;
  1272. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1273. var
  1274. hrefvmt : treference;
  1275. begin
  1276. if (cs_check_object in aktlocalswitches) then
  1277. begin
  1278. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(objdef.vmt_mangledname),0);
  1279. a_paramaddr_ref(exprasmlist,hrefvmt,paramanager.getintparaloc(2));
  1280. a_param_reg(exprasmlist,OS_ADDR,reg,paramanager.getintparaloc(1));
  1281. a_call_name(exprasmlist,'FPC_CHECK_OBJECT_EXT');
  1282. end
  1283. else
  1284. if (cs_check_range in aktlocalswitches) then
  1285. begin
  1286. a_param_reg(exprasmlist,OS_ADDR,reg,paramanager.getintparaloc(1));
  1287. a_call_name(exprasmlist,'FPC_CHECK_OBJECT');
  1288. end;
  1289. end;
  1290. {*****************************************************************************
  1291. Entry/Exit Code Functions
  1292. *****************************************************************************}
  1293. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1294. var
  1295. href : treference;
  1296. hregister : tregister;
  1297. spr,acc : Tregister;
  1298. begin
  1299. acc.enum:=R_INTREGISTER;
  1300. acc.number:=NR_ACCUMULATOR;
  1301. spr.enum:=R_INTREGISTER;
  1302. spr.number:=NR_SELF_POINTER_REG;
  1303. if is_class(procinfo._class) then
  1304. begin
  1305. if (cs_implicit_exceptions in aktmoduleswitches) then
  1306. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1307. { parameter 2 : self pointer / flag }
  1308. {!! this is a terrible hack, normally the helper should get three params : }
  1309. { one with self register, one with flag and one with VMT pointer }
  1310. {reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);}
  1311. a_param_reg(list, OS_ADDR, spr, paramanager.getintparaloc(2));
  1312. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1313. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1314. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1315. a_call_name(list,'FPC_NEW_CLASS');
  1316. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1317. { save the self pointer result }
  1318. a_load_reg_ref(list,OS_ADDR,spr,href);
  1319. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1320. end
  1321. else if is_object(procinfo._class) then
  1322. begin
  1323. { parameter 3 :vmt_offset }
  1324. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1325. { parameter 2 : address of pointer to vmt }
  1326. { this is the first(?) parameter which was pushed to the constructor }
  1327. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1328. hregister:=get_scratch_reg_address(list);
  1329. a_loadaddr_ref_reg(list, href, hregister);
  1330. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1331. free_scratch_reg(list, hregister);
  1332. { parameter 1 : address of self pointer }
  1333. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1334. hregister:=get_scratch_reg_address(list);
  1335. a_loadaddr_ref_reg(list, href, hregister);
  1336. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1337. free_scratch_reg(list, hregister);
  1338. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1339. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1340. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1341. end
  1342. else
  1343. internalerror(200006161);
  1344. end;
  1345. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1346. var
  1347. nofinal : tasmlabel;
  1348. href : treference;
  1349. hregister : tregister;
  1350. spr : Tregister;
  1351. begin
  1352. spr.enum:=R_INTREGISTER;
  1353. spr.number:=NR_SELF_POINTER_REG;
  1354. if is_class(procinfo._class) then
  1355. begin
  1356. { 2nd parameter : flag }
  1357. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);
  1358. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1359. { 1st parameter to destructor : self }
  1360. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1361. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1362. a_call_name(list,'FPC_DISPOSE_CLASS')
  1363. end
  1364. else if is_object(procinfo._class) then
  1365. begin
  1366. { must the object be finalized ? }
  1367. if procinfo._class.needs_inittable then
  1368. begin
  1369. objectlibrary.getlabel(nofinal);
  1370. reference_reset_base(href,procinfo.framepointer,target_info.first_parm_offset);
  1371. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1372. reference_reset_base(href,spr,0);
  1373. g_finalize(list,procinfo._class,href,false);
  1374. a_label(list,nofinal);
  1375. end;
  1376. { actually call destructor }
  1377. { parameter 3 :vmt_offset }
  1378. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1379. { parameter 2 : pointer to vmt }
  1380. { this is the first parameter which was pushed to the destructor }
  1381. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1382. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1383. { parameter 1 : address of self pointer }
  1384. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1385. hregister:=get_scratch_reg_address(list);
  1386. a_loadaddr_ref_reg(list, href, hregister);
  1387. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1388. free_scratch_reg(list, hregister);
  1389. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1390. end
  1391. else
  1392. internalerror(200006162);
  1393. end;
  1394. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1395. var
  1396. href : treference;
  1397. hregister : tregister;
  1398. spr : Tregister;
  1399. begin
  1400. spr.enum:=R_INTREGISTER;
  1401. spr.number:=NR_SELF_POINTER_REG;
  1402. if is_class(procinfo._class) then
  1403. begin
  1404. {
  1405. Dispose of the class then set self_pointer to nil
  1406. both in stack and in self register.
  1407. }
  1408. { 2nd parameter : flag }
  1409. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1410. { 1st parameter to destructor : self }
  1411. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1412. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1413. a_call_name(list,'FPC_DISPOSE_CLASS');
  1414. { SET SELF TO NIL }
  1415. a_load_const_reg(list,OS_ADDR,0,spr);
  1416. { set the self pointer in the stack to nil }
  1417. a_load_reg_ref(list,OS_ADDR,spr,href);
  1418. end
  1419. else if is_object(procinfo._class) then
  1420. begin
  1421. { parameter 3 :vmt_offset }
  1422. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1423. { parameter 2 : address of pointer to vmt }
  1424. { this is the first(?) parameter which was pushed to the constructor }
  1425. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1426. hregister:=get_scratch_reg_address(list);
  1427. a_loadaddr_ref_reg(list, href, hregister);
  1428. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1429. free_scratch_reg(list, hregister);
  1430. { parameter 1 : address of self pointer }
  1431. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1432. hregister:=get_scratch_reg_address(list);
  1433. a_loadaddr_ref_reg(list, href, hregister);
  1434. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1435. free_scratch_reg(list, hregister);
  1436. a_call_name(list,'FPC_HELP_FAIL');
  1437. { SET SELF TO NIL }
  1438. a_load_const_reg(list,OS_ADDR,0,spr);
  1439. end
  1440. else
  1441. internalerror(200006163);
  1442. end;
  1443. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1444. begin
  1445. end;
  1446. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1447. begin
  1448. end;
  1449. procedure tcg.g_profilecode(list : taasmoutput);
  1450. begin
  1451. end;
  1452. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1453. var r:Tregister;
  1454. begin
  1455. r.enum:=R_INTREGISTER;;
  1456. r.number:=NR_ACCUMULATOR;
  1457. a_load_reg_ref(list, OS_S32, r, href);
  1458. end;
  1459. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1460. begin
  1461. a_load_const_ref(list, OS_S32, a, href);
  1462. end;
  1463. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1464. var r:Tregister;
  1465. begin
  1466. r.enum:=R_INTREGISTER;;
  1467. r.number:=NR_ACCUMULATOR;
  1468. a_load_ref_reg(list, OS_S32, href, r);
  1469. end;
  1470. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1471. regsrc,regdst : tregister64);
  1472. begin
  1473. a_load64_reg_reg(list,regsrc,regdst);
  1474. a_op64_const_reg(list,op,value,regdst);
  1475. end;
  1476. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1477. begin
  1478. a_load64_reg_reg(list,regsrc2,regdst);
  1479. a_op64_reg_reg(list,op,regsrc1,regdst);
  1480. end;
  1481. initialization
  1482. ;
  1483. finalization
  1484. cg.free;
  1485. cg64.free;
  1486. end.
  1487. {
  1488. $Log$
  1489. Revision 1.79 2003-03-22 18:07:18 jonas
  1490. + add used scratch registers to usedintbyproc for non-i386
  1491. Revision 1.78 2003/03/11 21:46:24 jonas
  1492. * lots of new regallocator fixes, both in generic and ppc-specific code
  1493. (ppc compiler still can't compile the linux system unit though)
  1494. Revision 1.77 2003/02/19 22:00:14 daniel
  1495. * Code generator converted to new register notation
  1496. - Horribily outdated todo.txt removed
  1497. Revision 1.76 2003/01/31 22:47:48 peter
  1498. * maybe_testself now really uses the passed register
  1499. Revision 1.75 2003/01/30 21:46:35 peter
  1500. * maybe_testvmt added
  1501. Revision 1.74 2003/01/17 12:45:40 daniel
  1502. * Fixed internalerror 200301081 problem
  1503. Revision 1.73 2003/01/13 14:54:34 daniel
  1504. * Further work to convert codegenerator register convention;
  1505. internalerror bug fixed.
  1506. Revision 1.72 2003/01/09 22:00:53 florian
  1507. * fixed some PowerPC issues
  1508. Revision 1.71 2003/01/09 20:41:10 florian
  1509. * fixed broken PowerPC compiler
  1510. Revision 1.70 2003/01/08 18:43:56 daniel
  1511. * Tregister changed into a record
  1512. Revision 1.69 2002/12/24 15:56:50 peter
  1513. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1514. this for the pageprotection
  1515. Revision 1.68 2002/12/20 18:14:04 peter
  1516. * removed some runerror and writeln
  1517. Revision 1.67 2002/12/14 15:02:03 carl
  1518. * maxoperands -> max_operands (for portability in rautils.pas)
  1519. * fix some range-check errors with loadconst
  1520. + add ncgadd unit to m68k
  1521. * some bugfix of a_param_reg with LOC_CREFERENCE
  1522. Revision 1.66 2002/11/25 17:43:16 peter
  1523. * splitted defbase in defutil,symutil,defcmp
  1524. * merged isconvertable and is_equal into compare_defs(_ext)
  1525. * made operator search faster by walking the list only once
  1526. Revision 1.65 2002/11/17 16:27:31 carl
  1527. * document flags2reg
  1528. Revision 1.64 2002/11/16 17:06:28 peter
  1529. * return error 210 for failed self test
  1530. Revision 1.63 2002/11/15 01:58:46 peter
  1531. * merged changes from 1.0.7 up to 04-11
  1532. - -V option for generating bug report tracing
  1533. - more tracing for option parsing
  1534. - errors for cdecl and high()
  1535. - win32 import stabs
  1536. - win32 records<=8 are returned in eax:edx (turned off by default)
  1537. - heaptrc update
  1538. - more info for temp management in .s file with EXTDEBUG
  1539. Revision 1.62 2002/10/16 19:01:43 peter
  1540. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1541. implicit exception frames for procedures with initialized variables
  1542. and for constructors. The default is on for compatibility
  1543. Revision 1.61 2002/10/05 12:43:23 carl
  1544. * fixes for Delphi 6 compilation
  1545. (warning : Some features do not work under Delphi)
  1546. Revision 1.60 2002/10/02 18:20:52 peter
  1547. * Copy() is now internal syssym that calls compilerprocs
  1548. Revision 1.59 2002/09/17 18:54:02 jonas
  1549. * a_load_reg_reg() now has two size parameters: source and dest. This
  1550. allows some optimizations on architectures that don't encode the
  1551. register size in the register name.
  1552. Revision 1.58 2002/09/09 19:29:29 peter
  1553. * fixed dynarr_decr_ref call
  1554. Revision 1.57 2002/09/07 15:25:01 peter
  1555. * old logs removed and tabs fixed
  1556. Revision 1.56 2002/09/01 21:04:47 florian
  1557. * several powerpc related stuff fixed
  1558. Revision 1.55 2002/09/01 17:05:43 florian
  1559. + added abstract tcg.g_removevaluepara_openarray
  1560. Revision 1.54 2002/09/01 12:09:27 peter
  1561. + a_call_reg, a_call_loc added
  1562. * removed exprasmlist references
  1563. Revision 1.53 2002/08/19 18:17:48 carl
  1564. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1565. * more fixes to m68k for 64-bit operations
  1566. Revision 1.52 2002/08/17 22:09:43 florian
  1567. * result type handling in tcgcal.pass_2 overhauled
  1568. * better tnode.dowrite
  1569. * some ppc stuff fixed
  1570. Revision 1.51 2002/08/17 09:23:33 florian
  1571. * first part of procinfo rewrite
  1572. Revision 1.50 2002/08/16 14:24:57 carl
  1573. * issameref() to test if two references are the same (then emit no opcodes)
  1574. + ret_in_reg to replace ret_in_acc
  1575. (fix some register allocation bugs at the same time)
  1576. + save_std_register now has an extra parameter which is the
  1577. usedinproc registers
  1578. Revision 1.49 2002/08/15 08:13:54 carl
  1579. - a_load_sym_ofs_reg removed
  1580. * loadvmt now calls loadaddr_ref_reg instead
  1581. Revision 1.48 2002/08/14 19:26:02 carl
  1582. + routine to optimize opcodes with constants
  1583. Revision 1.47 2002/08/11 14:32:26 peter
  1584. * renamed current_library to objectlibrary
  1585. Revision 1.46 2002/08/11 13:24:11 peter
  1586. * saving of asmsymbols in ppu supported
  1587. * asmsymbollist global is removed and moved into a new class
  1588. tasmlibrarydata that will hold the info of a .a file which
  1589. corresponds with a single module. Added librarydata to tmodule
  1590. to keep the library info stored for the module. In the future the
  1591. objectfiles will also be stored to the tasmlibrarydata class
  1592. * all getlabel/newasmsymbol and friends are moved to the new class
  1593. Revision 1.45 2002/08/10 17:15:20 jonas
  1594. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1595. Revision 1.44 2002/08/09 19:10:05 carl
  1596. - moved new_exception and free_exception to ncgutils
  1597. Revision 1.43 2002/08/05 18:27:48 carl
  1598. + more more more documentation
  1599. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1600. Revision 1.42 2002/08/04 19:08:21 carl
  1601. + added generic exception support (still does not work!)
  1602. + more documentation
  1603. Revision 1.41 2002/07/30 20:50:43 florian
  1604. * the code generator knows now if parameters are in registers
  1605. Revision 1.40 2002/07/29 21:16:02 florian
  1606. * some more ppc fixes
  1607. Revision 1.39 2002/07/28 15:56:00 jonas
  1608. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1609. generic implementation
  1610. Revision 1.38 2002/07/27 19:53:51 jonas
  1611. + generic implementation of tcg.g_flags2ref()
  1612. * tcg.flags2xxx() now also needs a size parameter
  1613. Revision 1.37 2002/07/20 11:57:53 florian
  1614. * types.pas renamed to defbase.pas because D6 contains a types
  1615. unit so this would conflicts if D6 programms are compiled
  1616. + Willamette/SSE2 instructions to assembler added
  1617. }