cgobj.pas 83 KB

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