cgobj.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  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. begin
  983. a_load_reg_reg(list,size,size,src2,dst);
  984. a_op_reg_reg(list,op,size,src1,dst);
  985. end;
  986. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  987. l : tasmlabel);
  988. var
  989. tmpreg: tregister;
  990. begin
  991. {$ifdef newra}
  992. tmpreg:=rg.getregisterint(list,size);
  993. {$else}
  994. tmpreg := get_scratch_reg_int(list,size);
  995. {$endif}
  996. a_load_ref_reg(list,size,ref,tmpreg);
  997. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  998. {$ifdef newra}
  999. rg.ungetregisterint(list,tmpreg);
  1000. {$else}
  1001. free_scratch_reg(list,tmpreg);
  1002. {$endif}
  1003. end;
  1004. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  1005. l : tasmlabel);
  1006. begin
  1007. case loc.loc of
  1008. LOC_REGISTER,LOC_CREGISTER:
  1009. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1010. LOC_REFERENCE,LOC_CREFERENCE:
  1011. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1012. else
  1013. internalerror(200109061);
  1014. end;
  1015. end;
  1016. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1017. var
  1018. tmpreg: tregister;
  1019. begin
  1020. {$ifdef newra}
  1021. tmpreg:=rg.getregisterint(list,size);
  1022. {$else}
  1023. tmpreg := get_scratch_reg_int(list,size);
  1024. {$endif}
  1025. a_load_ref_reg(list,size,ref,tmpreg);
  1026. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1027. {$ifdef newra}
  1028. rg.ungetregisterint(list,tmpreg);
  1029. {$else}
  1030. free_scratch_reg(list,tmpreg);
  1031. {$endif}
  1032. end;
  1033. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1034. begin
  1035. case loc.loc of
  1036. LOC_REGISTER,
  1037. LOC_CREGISTER:
  1038. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1039. LOC_REFERENCE,
  1040. LOC_CREFERENCE :
  1041. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1042. LOC_CONSTANT:
  1043. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1044. else
  1045. internalerror(200203231);
  1046. end;
  1047. end;
  1048. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1049. l : tasmlabel);
  1050. var
  1051. tmpreg: tregister;
  1052. begin
  1053. case loc.loc of
  1054. LOC_REGISTER,LOC_CREGISTER:
  1055. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1056. LOC_REFERENCE,LOC_CREFERENCE:
  1057. begin
  1058. {$ifdef newra}
  1059. tmpreg := rg.getregisterint(list,size);
  1060. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1061. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1062. rg.ungetregisterint(list,tmpreg);
  1063. {$else newra}
  1064. {$ifdef i386}
  1065. { the following is done with defines to avoid a speed penalty, }
  1066. { since all this is only necessary for the 80x86 (because EDI }
  1067. { doesn't have an 8bit component which is directly addressable) }
  1068. if size in [OS_8,OS_S8] then
  1069. tmpreg := rg.getregisterint(list,size)
  1070. else
  1071. {$endif i386}
  1072. tmpreg := get_scratch_reg_int(list,size);
  1073. {tmpreg := rg.makeregsize(tmpreg,size);}
  1074. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1075. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1076. {$ifdef i386}
  1077. if size in [OS_8,OS_S8] then
  1078. rg.ungetregisterint(list,tmpreg)
  1079. else
  1080. {$endif i386}
  1081. free_scratch_reg(list,tmpreg);
  1082. {$endif newra}
  1083. end
  1084. else
  1085. internalerror(200109061);
  1086. end;
  1087. end;
  1088. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  1089. begin
  1090. reg_cgsize := OS_INT;
  1091. end;
  1092. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  1093. begin
  1094. {$ifdef FPC}
  1095. {$warning FIX ME!}
  1096. {$endif}
  1097. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  1098. if loadref then
  1099. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  1100. else
  1101. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  1102. if delsource then
  1103. reference_release(list,source);
  1104. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  1105. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1106. end;
  1107. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  1108. var
  1109. href : treference;
  1110. incrfunc : string;
  1111. begin
  1112. { These functions should not change the registers (they use
  1113. the saveregister proc directive }
  1114. if is_interfacecom(t) then
  1115. incrfunc:='FPC_INTF_INCR_REF'
  1116. else if is_ansistring(t) then
  1117. incrfunc:='FPC_ANSISTR_INCR_REF'
  1118. else if is_widestring(t) then
  1119. incrfunc:='FPC_WIDESTR_INCR_REF'
  1120. else if is_dynamic_array(t) then
  1121. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1122. else
  1123. incrfunc:='';
  1124. { call the special incr function or the generic addref }
  1125. if incrfunc<>'' then
  1126. begin
  1127. if loadref then
  1128. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1129. else
  1130. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1131. a_call_name(list,incrfunc);
  1132. end
  1133. else
  1134. begin
  1135. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1136. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1137. if loadref then
  1138. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1139. else
  1140. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1141. a_call_name(list,'FPC_ADDREF');
  1142. end;
  1143. end;
  1144. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference; loadref:boolean);
  1145. var
  1146. href : treference;
  1147. decrfunc : string;
  1148. needrtti : boolean;
  1149. begin
  1150. needrtti:=false;
  1151. if is_interfacecom(t) then
  1152. decrfunc:='FPC_INTF_DECR_REF'
  1153. else if is_ansistring(t) then
  1154. decrfunc:='FPC_ANSISTR_DECR_REF'
  1155. else if is_widestring(t) then
  1156. decrfunc:='FPC_WIDESTR_DECR_REF'
  1157. else if is_dynamic_array(t) then
  1158. begin
  1159. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1160. needrtti:=true;
  1161. end
  1162. else
  1163. decrfunc:='';
  1164. { call the special decr function or the generic decref }
  1165. if decrfunc<>'' then
  1166. begin
  1167. if needrtti then
  1168. begin
  1169. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1170. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1171. end;
  1172. if loadref then
  1173. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1174. else
  1175. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1176. a_call_name(list,decrfunc);
  1177. end
  1178. else
  1179. begin
  1180. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1181. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1182. if loadref then
  1183. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1184. else
  1185. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1186. a_call_name(list,'FPC_DECREF');
  1187. end;
  1188. end;
  1189. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1190. var
  1191. href : treference;
  1192. begin
  1193. if is_ansistring(t) or
  1194. is_widestring(t) or
  1195. is_interfacecom(t) then
  1196. a_load_const_ref(list,OS_ADDR,0,ref)
  1197. else
  1198. begin
  1199. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1200. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1201. if loadref then
  1202. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1203. else
  1204. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1205. a_call_name(list,'FPC_INITIALIZE');
  1206. end;
  1207. end;
  1208. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1209. var
  1210. href : treference;
  1211. begin
  1212. if is_ansistring(t) or
  1213. is_widestring(t) or
  1214. is_interfacecom(t) then
  1215. g_decrrefcount(list,t,ref,loadref)
  1216. else
  1217. begin
  1218. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1219. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1220. if loadref then
  1221. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1222. else
  1223. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1224. a_call_name(list,'FPC_FINALIZE');
  1225. end;
  1226. end;
  1227. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1228. { generate range checking code for the value at location p. The type }
  1229. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1230. { is the original type used at that location. When both defs are equal }
  1231. { the check is also insert (needed for succ,pref,inc,dec) }
  1232. const
  1233. {$ifdef ver1_0}
  1234. awordsignedmax=high(longint);
  1235. {$else}
  1236. awordsignedmax=high(aword) div 2;
  1237. {$endif}
  1238. var
  1239. neglabel : tasmlabel;
  1240. hreg : tregister;
  1241. fromdef : tdef;
  1242. lto,hto,
  1243. lfrom,hfrom : TConstExprInt;
  1244. from_signed: boolean;
  1245. begin
  1246. { range checking on and range checkable value? }
  1247. if not(cs_check_range in aktlocalswitches) or
  1248. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1249. exit;
  1250. if is_64bit(p.resulttype.def) or is_64bit(todef) then
  1251. begin
  1252. cg64.g_rangecheck64(list,p,todef);
  1253. exit;
  1254. end;
  1255. { only check when assigning to scalar, subranges are different, }
  1256. { when todef=fromdef then the check is always generated }
  1257. fromdef:=p.resulttype.def;
  1258. getrange(p.resulttype.def,lfrom,hfrom);
  1259. getrange(todef,lto,hto);
  1260. { no range check if from and to are equal and are both longint/dword }
  1261. { (if we have a 32bit processor) or int64/qword, since such }
  1262. { operations can at most cause overflows (JM) }
  1263. { Note that these checks are mostly processor independent, they only }
  1264. { have to be changed once we introduce 64bit subrange types }
  1265. if (fromdef = todef) and
  1266. (fromdef.deftype=orddef) and
  1267. (((sizeof(aword) = 4) and
  1268. (((torddef(fromdef).typ = s32bit) and
  1269. (lfrom = low(longint)) and
  1270. (hfrom = high(longint))) or
  1271. ((torddef(fromdef).typ = u32bit) and
  1272. (lfrom = low(cardinal)) and
  1273. (hfrom = high(cardinal)))))) then
  1274. exit;
  1275. if todef<>fromdef then
  1276. begin
  1277. { if the from-range falls completely in the to-range, no check }
  1278. { is necessary }
  1279. if (lto<=lfrom) and (hto>=hfrom) then
  1280. exit;
  1281. end;
  1282. { generate the rangecheck code for the def where we are going to }
  1283. { store the result }
  1284. { use the trick that }
  1285. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1286. { To be able to do that, we have to make sure however that either }
  1287. { fromdef and todef are both signed or unsigned, or that we leave }
  1288. { the parts < 0 and > maxlongint out }
  1289. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1290. from_signed := is_signed(fromdef);
  1291. if from_signed xor is_signed(todef) then
  1292. if from_signed then
  1293. { from is signed, to is unsigned }
  1294. begin
  1295. { if high(from) < 0 -> always range error }
  1296. if (hfrom < 0) or
  1297. { if low(to) > maxlongint also range error }
  1298. (lto > awordsignedmax) then
  1299. begin
  1300. a_call_name(list,'FPC_RANGEERROR');
  1301. exit
  1302. end;
  1303. { from is signed and to is unsigned -> when looking at from }
  1304. { as an unsigned value, it must be < maxlongint (otherwise }
  1305. { it's negative, which is invalid since "to" is unsigned) }
  1306. if hto > awordsignedmax then
  1307. hto := awordsignedmax;
  1308. end
  1309. else
  1310. { from is unsigned, to is signed }
  1311. begin
  1312. if (lfrom > awordsignedmax) or
  1313. (hto < 0) then
  1314. begin
  1315. a_call_name(list,'FPC_RANGEERROR');
  1316. exit
  1317. end;
  1318. { from is unsigned and to is signed -> when looking at to }
  1319. { as an unsigned value, it must be >= 0 (since negative }
  1320. { values are the same as values > maxlongint) }
  1321. if lto < 0 then
  1322. lto := 0;
  1323. end;
  1324. {$ifdef newra}
  1325. hreg:=rg.getregisterint(list,OS_INT);
  1326. {$else}
  1327. hreg := get_scratch_reg_int(list,OS_INT);
  1328. {$endif}
  1329. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1330. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1331. aword(lto),p.location.register,hreg)
  1332. else
  1333. begin
  1334. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1335. p.location.reference,hreg);
  1336. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1337. end;
  1338. objectlibrary.getlabel(neglabel);
  1339. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1340. { !!! should happen right after the compare (JM) }
  1341. {$ifdef newra}
  1342. rg.ungetregisterint(list,hreg);
  1343. {$else}
  1344. free_scratch_reg(list,hreg);
  1345. {$endif}
  1346. a_call_name(list,'FPC_RANGEERROR');
  1347. a_label(list,neglabel);
  1348. end;
  1349. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1350. begin
  1351. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1352. a_call_name(list,'FPC_STACKCHECK');
  1353. end;
  1354. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1355. var
  1356. tmpreg : tregister;
  1357. begin
  1358. {$ifdef newra}
  1359. tmpreg:=rg.getregisterint(list,size);
  1360. {$else}
  1361. tmpreg := get_scratch_reg_int(list,size);
  1362. {$endif}
  1363. g_flags2reg(list,size,f,tmpreg);
  1364. a_load_reg_ref(list,size,tmpreg,ref);
  1365. {$ifdef newra}
  1366. rg.ungetregisterint(list,tmpreg);
  1367. {$else}
  1368. free_scratch_reg(list,tmpreg);
  1369. {$endif}
  1370. end;
  1371. function tcg.g_load_self(list : taasmoutput):tregister;
  1372. var
  1373. hp : treference;
  1374. p : tprocinfo;
  1375. self_reg : tregister;
  1376. begin
  1377. if not assigned(current_procdef._class) then
  1378. internalerror(200303211);
  1379. self_reg:=rg.getaddressregister(list);
  1380. if current_procdef.parast.symtablelevel>normal_function_level then
  1381. begin
  1382. reference_reset_base(hp,current_procinfo.framepointer,current_procinfo.framepointer_offset);
  1383. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1384. p:=current_procinfo.parent;
  1385. while (p.procdef.parast.symtablelevel>normal_function_level) do
  1386. begin
  1387. reference_reset_base(hp,self_reg,p.framepointer_offset);
  1388. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1389. p:=p.parent;
  1390. end;
  1391. reference_reset_base(hp,self_reg,p.selfpointer_offset);
  1392. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1393. end
  1394. else
  1395. begin
  1396. reference_reset_base(hp,current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1397. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1398. end;
  1399. g_load_self:=self_reg;
  1400. end;
  1401. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1402. var
  1403. OKLabel : tasmlabel;
  1404. begin
  1405. if (cs_check_object in aktlocalswitches) or
  1406. (cs_check_range in aktlocalswitches) then
  1407. begin
  1408. objectlibrary.getlabel(oklabel);
  1409. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1410. a_param_const(list,OS_INT,210,paramanager.getintparaloc(1));
  1411. a_call_name(list,'FPC_HANDLEERROR');
  1412. a_label(list,oklabel);
  1413. end;
  1414. end;
  1415. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1416. var
  1417. hrefvmt : treference;
  1418. begin
  1419. if (cs_check_object in aktlocalswitches) then
  1420. begin
  1421. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymboldata(objdef.vmt_mangledname),0);
  1422. a_paramaddr_ref(list,hrefvmt,paramanager.getintparaloc(2));
  1423. a_param_reg(list,OS_ADDR,reg,paramanager.getintparaloc(1));
  1424. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1425. end
  1426. else
  1427. if (cs_check_range in aktlocalswitches) then
  1428. begin
  1429. a_param_reg(list,OS_ADDR,reg,paramanager.getintparaloc(1));
  1430. a_call_name(list,'FPC_CHECK_OBJECT');
  1431. end;
  1432. end;
  1433. {*****************************************************************************
  1434. Entry/Exit Code Functions
  1435. *****************************************************************************}
  1436. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1437. var
  1438. href : treference;
  1439. acc : Tregister;
  1440. begin
  1441. if current_procinfo.vmtpointer_offset=0 then
  1442. internalerror(200303251);
  1443. if current_procinfo.selfpointer_offset=0 then
  1444. internalerror(200303252);
  1445. acc.enum:=R_INTREGISTER;
  1446. acc.number:=NR_ACCUMULATOR;
  1447. if is_class(current_procdef._class) then
  1448. begin
  1449. if (cs_implicit_exceptions in aktmoduleswitches) then
  1450. include(current_procinfo.flags,pi_needs_implicit_finally);
  1451. { parameter 2 : vmt pointer, 0 when called by inherited }
  1452. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.vmtpointer_offset);
  1453. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1454. { parameter 1 : self pointer }
  1455. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1456. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1457. a_call_name(list,'FPC_NEW_CLASS');
  1458. { save the self pointer }
  1459. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1460. a_load_reg_ref(list,OS_ADDR,acc,href);
  1461. { fail? }
  1462. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1463. end
  1464. else if is_object(current_procdef._class) then
  1465. begin
  1466. { parameter 3 : vmt_offset }
  1467. a_param_const(list, OS_32, current_procdef._class.vmt_offset, paramanager.getintparaloc(3));
  1468. { parameter 2 : address of pointer to vmt,
  1469. this is required to allow setting the vmt to -1 to indicate
  1470. that memory was allocated }
  1471. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.vmtpointer_offset);
  1472. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1473. { parameter 1 : self pointer }
  1474. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1475. a_param_ref(list,OS_ADDR,href,paramanager.getintparaloc(1));
  1476. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1477. { save the self pointer }
  1478. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1479. a_load_reg_ref(list,OS_ADDR,acc,href);
  1480. { fail? }
  1481. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1482. end
  1483. else
  1484. internalerror(200006161);
  1485. end;
  1486. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1487. var
  1488. nofinal : tasmlabel;
  1489. href : treference;
  1490. reg : tregister;
  1491. begin
  1492. if is_class(current_procdef._class) then
  1493. begin
  1494. if current_procinfo.selfpointer_offset=0 then
  1495. internalerror(200303253);
  1496. { parameter 2 : flag }
  1497. if current_procinfo.inheritedflag_offset=0 then
  1498. internalerror(200303251);
  1499. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.inheritedflag_offset);
  1500. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1501. { parameter 1 : self }
  1502. if current_procinfo.selfpointer_offset=0 then
  1503. internalerror(200303252);
  1504. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1505. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1506. a_call_name(list,'FPC_DISPOSE_CLASS')
  1507. end
  1508. else if is_object(current_procdef._class) then
  1509. begin
  1510. if current_procinfo.selfpointer_offset=0 then
  1511. internalerror(200303254);
  1512. if current_procinfo.vmtpointer_offset=0 then
  1513. internalerror(200303255);
  1514. { must the object be finalized ? }
  1515. if current_procdef._class.needs_inittable then
  1516. begin
  1517. objectlibrary.getlabel(nofinal);
  1518. reference_reset_base(href,current_procinfo.framepointer,target_info.first_parm_offset);
  1519. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1520. reg:=g_load_self(list);
  1521. reference_reset_base(href,reg,0);
  1522. g_finalize(list,current_procdef._class,href,false);
  1523. reference_release(list,href);
  1524. a_label(list,nofinal);
  1525. end;
  1526. { actually call destructor }
  1527. { parameter 3 : vmt_offset }
  1528. a_param_const(list, OS_32, current_procdef._class.vmt_offset, paramanager.getintparaloc(3));
  1529. { parameter 2 : pointer to vmt }
  1530. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.vmtpointer_offset);
  1531. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1532. { parameter 1 : address of self pointer }
  1533. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1534. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(1));
  1535. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1536. end
  1537. else
  1538. internalerror(200006162);
  1539. end;
  1540. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1541. var
  1542. href : treference;
  1543. begin
  1544. if is_class(current_procdef._class) then
  1545. begin
  1546. if current_procinfo.selfpointer_offset=0 then
  1547. internalerror(200303256);
  1548. { parameter 2 : flag, 0 -> inherited call (=no dispose) }
  1549. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1550. { parameter 1 : self }
  1551. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1552. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1553. a_call_name(list,'FPC_DISPOSE_CLASS');
  1554. end
  1555. else if is_object(current_procdef._class) then
  1556. begin
  1557. if current_procinfo.selfpointer_offset=0 then
  1558. internalerror(200303257);
  1559. if current_procinfo.vmtpointer_offset=0 then
  1560. internalerror(200303258);
  1561. { parameter 3 : vmt_offset }
  1562. a_param_const(list, OS_32, current_procdef._class.vmt_offset, paramanager.getintparaloc(3));
  1563. { parameter 2 : pointer to vmt, will be reset to 0 when freed }
  1564. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.vmtpointer_offset);
  1565. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1566. { parameter 1 : self pointer }
  1567. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1568. a_param_ref(list,OS_ADDR,href,paramanager.getintparaloc(1));
  1569. a_call_name(list,'FPC_HELP_FAIL');
  1570. end
  1571. else
  1572. internalerror(200006163);
  1573. { set self to nil }
  1574. reference_reset_base(href, current_procinfo.framepointer,current_procinfo.selfpointer_offset);
  1575. a_load_const_ref(list,OS_ADDR,0,href);
  1576. end;
  1577. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1578. begin
  1579. end;
  1580. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1581. begin
  1582. end;
  1583. procedure tcg.g_profilecode(list : taasmoutput);
  1584. begin
  1585. end;
  1586. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1587. var r:Tregister;
  1588. begin
  1589. r.enum:=R_INTREGISTER;;
  1590. r.number:=NR_ACCUMULATOR;
  1591. a_load_reg_ref(list, OS_S32, r, href);
  1592. end;
  1593. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1594. begin
  1595. a_load_const_ref(list, OS_S32, a, href);
  1596. end;
  1597. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1598. var r:Tregister;
  1599. begin
  1600. r.enum:=R_INTREGISTER;
  1601. r.number:=NR_ACCUMULATOR;
  1602. a_load_ref_reg(list, OS_S32, href, r);
  1603. end;
  1604. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1605. regsrc,regdst : tregister64);
  1606. begin
  1607. a_load64_reg_reg(list,regsrc,regdst);
  1608. a_op64_const_reg(list,op,value,regdst);
  1609. end;
  1610. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1611. begin
  1612. a_load64_reg_reg(list,regsrc2,regdst);
  1613. a_op64_reg_reg(list,op,regsrc1,regdst);
  1614. end;
  1615. initialization
  1616. ;
  1617. finalization
  1618. cg.free;
  1619. cg64.free;
  1620. end.
  1621. {
  1622. $Log$
  1623. Revision 1.93 2003-04-29 07:28:52 michael
  1624. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1625. Revision 1.92 2003/04/27 11:21:32 peter
  1626. * aktprocdef renamed to current_procdef
  1627. * procinfo renamed to current_procinfo
  1628. * procinfo will now be stored in current_module so it can be
  1629. cleaned up properly
  1630. * gen_main_procsym changed to create_main_proc and release_main_proc
  1631. to also generate a tprocinfo structure
  1632. * fixed unit implicit initfinal
  1633. Revision 1.91 2003/04/27 07:29:50 peter
  1634. * current_procdef cleanup, current_procdef is now always nil when parsing
  1635. a new procdef declaration
  1636. * aktprocsym removed
  1637. * lexlevel removed, use symtable.symtablelevel instead
  1638. * implicit init/final code uses the normal genentry/genexit
  1639. * funcret state checking updated for new funcret handling
  1640. Revision 1.90 2003/04/26 20:57:17 florian
  1641. * fixed para locations of fpc_class_new helper call
  1642. Revision 1.89 2003/04/26 17:21:08 florian
  1643. * fixed passing of fpu values by fpu register
  1644. Revision 1.88 2003/04/23 20:16:03 peter
  1645. + added currency support based on int64
  1646. + is_64bit for use in cg units instead of is_64bitint
  1647. * removed cgmessage from n386add, replace with internalerrors
  1648. Revision 1.87 2003/04/23 14:42:07 daniel
  1649. * Further register allocator work. Compiler now smaller with new
  1650. allocator than without.
  1651. * Somebody forgot to adjust ppu version number
  1652. Revision 1.86 2003/04/23 13:20:34 peter
  1653. * fix self passing to fpc_help_fail
  1654. Revision 1.85 2003/04/23 12:35:34 florian
  1655. * fixed several issues with powerpc
  1656. + applied a patch from Jonas for nested function calls (PowerPC only)
  1657. * ...
  1658. Revision 1.84 2003/04/22 14:33:38 peter
  1659. * removed some notes/hints
  1660. Revision 1.83 2003/04/22 13:47:08 peter
  1661. * fixed C style array of const
  1662. * fixed C array passing
  1663. * fixed left to right with high parameters
  1664. Revision 1.82 2003/04/22 10:09:34 daniel
  1665. + Implemented the actual register allocator
  1666. + Scratch registers unavailable when new register allocator used
  1667. + maybe_save/maybe_restore unavailable when new register allocator used
  1668. Revision 1.81 2003/04/06 21:11:23 olle
  1669. * changed newasmsymbol to newasmsymboldata for data symbols
  1670. Revision 1.80 2003/03/28 19:16:56 peter
  1671. * generic constructor working for i386
  1672. * remove fixed self register
  1673. * esi added as address register for i386
  1674. Revision 1.79 2003/03/22 18:07:18 jonas
  1675. + add used scratch registers to usedintbyproc for non-i386
  1676. Revision 1.78 2003/03/11 21:46:24 jonas
  1677. * lots of new regallocator fixes, both in generic and ppc-specific code
  1678. (ppc compiler still can't compile the linux system unit though)
  1679. Revision 1.77 2003/02/19 22:00:14 daniel
  1680. * Code generator converted to new register notation
  1681. - Horribily outdated todo.txt removed
  1682. Revision 1.76 2003/01/31 22:47:48 peter
  1683. * maybe_testself now really uses the passed register
  1684. Revision 1.75 2003/01/30 21:46:35 peter
  1685. * maybe_testvmt added
  1686. Revision 1.74 2003/01/17 12:45:40 daniel
  1687. * Fixed internalerror 200301081 problem
  1688. Revision 1.73 2003/01/13 14:54:34 daniel
  1689. * Further work to convert codegenerator register convention;
  1690. internalerror bug fixed.
  1691. Revision 1.72 2003/01/09 22:00:53 florian
  1692. * fixed some PowerPC issues
  1693. Revision 1.71 2003/01/09 20:41:10 florian
  1694. * fixed broken PowerPC compiler
  1695. Revision 1.70 2003/01/08 18:43:56 daniel
  1696. * Tregister changed into a record
  1697. Revision 1.69 2002/12/24 15:56:50 peter
  1698. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1699. this for the pageprotection
  1700. Revision 1.68 2002/12/20 18:14:04 peter
  1701. * removed some runerror and writeln
  1702. Revision 1.67 2002/12/14 15:02:03 carl
  1703. * maxoperands -> max_operands (for portability in rautils.pas)
  1704. * fix some range-check errors with loadconst
  1705. + add ncgadd unit to m68k
  1706. * some bugfix of a_param_reg with LOC_CREFERENCE
  1707. Revision 1.66 2002/11/25 17:43:16 peter
  1708. * splitted defbase in defutil,symutil,defcmp
  1709. * merged isconvertable and is_equal into compare_defs(_ext)
  1710. * made operator search faster by walking the list only once
  1711. Revision 1.65 2002/11/17 16:27:31 carl
  1712. * document flags2reg
  1713. Revision 1.64 2002/11/16 17:06:28 peter
  1714. * return error 210 for failed self test
  1715. Revision 1.63 2002/11/15 01:58:46 peter
  1716. * merged changes from 1.0.7 up to 04-11
  1717. - -V option for generating bug report tracing
  1718. - more tracing for option parsing
  1719. - errors for cdecl and high()
  1720. - win32 import stabs
  1721. - win32 records<=8 are returned in eax:edx (turned off by default)
  1722. - heaptrc update
  1723. - more info for temp management in .s file with EXTDEBUG
  1724. Revision 1.62 2002/10/16 19:01:43 peter
  1725. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1726. implicit exception frames for procedures with initialized variables
  1727. and for constructors. The default is on for compatibility
  1728. Revision 1.61 2002/10/05 12:43:23 carl
  1729. * fixes for Delphi 6 compilation
  1730. (warning : Some features do not work under Delphi)
  1731. Revision 1.60 2002/10/02 18:20:52 peter
  1732. * Copy() is now internal syssym that calls compilerprocs
  1733. Revision 1.59 2002/09/17 18:54:02 jonas
  1734. * a_load_reg_reg() now has two size parameters: source and dest. This
  1735. allows some optimizations on architectures that don't encode the
  1736. register size in the register name.
  1737. Revision 1.58 2002/09/09 19:29:29 peter
  1738. * fixed dynarr_decr_ref call
  1739. Revision 1.57 2002/09/07 15:25:01 peter
  1740. * old logs removed and tabs fixed
  1741. Revision 1.56 2002/09/01 21:04:47 florian
  1742. * several powerpc related stuff fixed
  1743. Revision 1.55 2002/09/01 17:05:43 florian
  1744. + added abstract tcg.g_removevaluepara_openarray
  1745. Revision 1.54 2002/09/01 12:09:27 peter
  1746. + a_call_reg, a_call_loc added
  1747. * removed exprasmlist references
  1748. Revision 1.53 2002/08/19 18:17:48 carl
  1749. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1750. * more fixes to m68k for 64-bit operations
  1751. Revision 1.52 2002/08/17 22:09:43 florian
  1752. * result type handling in tcgcal.pass_2 overhauled
  1753. * better tnode.dowrite
  1754. * some ppc stuff fixed
  1755. Revision 1.51 2002/08/17 09:23:33 florian
  1756. * first part of procinfo rewrite
  1757. Revision 1.50 2002/08/16 14:24:57 carl
  1758. * issameref() to test if two references are the same (then emit no opcodes)
  1759. + ret_in_reg to replace ret_in_acc
  1760. (fix some register allocation bugs at the same time)
  1761. + save_std_register now has an extra parameter which is the
  1762. usedinproc registers
  1763. Revision 1.49 2002/08/15 08:13:54 carl
  1764. - a_load_sym_ofs_reg removed
  1765. * loadvmt now calls loadaddr_ref_reg instead
  1766. Revision 1.48 2002/08/14 19:26:02 carl
  1767. + routine to optimize opcodes with constants
  1768. Revision 1.47 2002/08/11 14:32:26 peter
  1769. * renamed current_library to objectlibrary
  1770. Revision 1.46 2002/08/11 13:24:11 peter
  1771. * saving of asmsymbols in ppu supported
  1772. * asmsymbollist global is removed and moved into a new class
  1773. tasmlibrarydata that will hold the info of a .a file which
  1774. corresponds with a single module. Added librarydata to tmodule
  1775. to keep the library info stored for the module. In the future the
  1776. objectfiles will also be stored to the tasmlibrarydata class
  1777. * all getlabel/newasmsymbol and friends are moved to the new class
  1778. Revision 1.45 2002/08/10 17:15:20 jonas
  1779. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1780. Revision 1.44 2002/08/09 19:10:05 carl
  1781. - moved new_exception and free_exception to ncgutils
  1782. Revision 1.43 2002/08/05 18:27:48 carl
  1783. + more more more documentation
  1784. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1785. Revision 1.42 2002/08/04 19:08:21 carl
  1786. + added generic exception support (still does not work!)
  1787. + more documentation
  1788. Revision 1.41 2002/07/30 20:50:43 florian
  1789. * the code generator knows now if parameters are in registers
  1790. Revision 1.40 2002/07/29 21:16:02 florian
  1791. * some more ppc fixes
  1792. Revision 1.39 2002/07/28 15:56:00 jonas
  1793. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1794. generic implementation
  1795. Revision 1.38 2002/07/27 19:53:51 jonas
  1796. + generic implementation of tcg.g_flags2ref()
  1797. * tcg.flags2xxx() now also needs a size parameter
  1798. Revision 1.37 2002/07/20 11:57:53 florian
  1799. * types.pas renamed to defbase.pas because D6 contains a types
  1800. unit so this would conflicts if D6 programms are compiled
  1801. + Willamette/SSE2 instructions to assembler added
  1802. }