cgobj.pas 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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. { vector register move instructions }
  180. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  181. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  182. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  183. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  184. { basic arithmetic operations }
  185. { note: for operators which require only one argument (not, neg), use }
  186. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  187. { that in this case the *second* operand is used as both source and }
  188. { destination (JM) }
  189. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  190. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  191. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  192. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  193. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  194. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  195. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  196. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  197. { trinary operations for processors that support them, 'emulated' }
  198. { on others. None with "ref" arguments since I don't think there }
  199. { are any processors that support it (JM) }
  200. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  201. size: tcgsize; a: aword; src, dst: tregister); virtual;
  202. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  203. size: tcgsize; src1, src2, dst: tregister); virtual;
  204. { comparison operations }
  205. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  206. l : tasmlabel);virtual; abstract;
  207. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  208. l : tasmlabel); virtual;
  209. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  210. l : tasmlabel);
  211. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  212. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  213. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  214. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  215. l : tasmlabel);
  216. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  217. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  218. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  219. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  220. }
  221. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  222. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  223. {
  224. This routine tries to optimize the const_reg opcode, and should be
  225. called at the start of a_op_const_reg. It returns the actual opcode
  226. to emit, and the constant value to emit. If this routine returns
  227. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  228. @param(op The opcode to emit, returns the opcode which must be emitted)
  229. @param(a The constant which should be emitted, returns the constant which must
  230. be emitted)
  231. @param(reg The register to emit the opcode with, returns the register with
  232. which the opcode will be emitted)
  233. }
  234. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg: tregister): boolean;virtual;
  235. {#
  236. This routine is used in exception management nodes. It should
  237. save the exception reason currently in the accumulator. The
  238. save should be done either to a temp (pointed to by href).
  239. or on the stack (pushing the value on the stack).
  240. The size of the value to save is OS_S32. The default version
  241. saves the exception reason to a temp. memory area.
  242. }
  243. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  244. {#
  245. This routine is used in exception management nodes. It should
  246. save the exception reason constant. The
  247. save should be done either to a temp (pointed to by href).
  248. or on the stack (pushing the value on the stack).
  249. The size of the value to save is OS_S32. The default version
  250. saves the exception reason to a temp. memory area.
  251. }
  252. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);virtual;
  253. {#
  254. This routine is used in exception management nodes. It should
  255. load the exception reason to the accumulator. The saved value
  256. should either be in the temp. area (pointed to by href , href should
  257. *NOT* be freed) or on the stack (the value should be popped).
  258. The size of the value to save is OS_S32. The default version
  259. saves the exception reason to a temp. memory area.
  260. }
  261. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  262. function g_load_self(list : taasmoutput):tregister;
  263. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  264. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  265. {# This should emit the opcode to copy len bytes from the source
  266. to destination, if loadref is true, it assumes that it first must load
  267. the source address from the memory location where
  268. source points to.
  269. It must be overriden for each new target processor.
  270. @param(source Source reference of copy)
  271. @param(dest Destination reference of copy)
  272. @param(delsource Indicates if the source reference's resources should be freed)
  273. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  274. }
  275. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  276. {# This should emit the opcode to a shortrstring from the source
  277. to destination, if loadref is true, it assumes that it first must load
  278. the source address from the memory location where
  279. source points to.
  280. @param(source Source reference of copy)
  281. @param(dest Destination reference of copy)
  282. @param(delsource Indicates if the source reference's resources should be freed)
  283. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  284. }
  285. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  286. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  287. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  288. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  289. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  290. {# Emits the call to the stack checking routine of
  291. the runtime library. The default behavior
  292. does not need to be modified, as it is generic
  293. for all platforms.
  294. @param(stackframesize Number of bytes which will be allocated on the stack)
  295. }
  296. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  297. {# Generates range checking code. It is to note
  298. that this routine does not need to be overriden,
  299. as it takes care of everything.
  300. @param(p Node which contains the value to check)
  301. @param(todef Type definition of node to range check)
  302. }
  303. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  304. const todef: tdef); virtual;
  305. {# Generates overflow checking code for a node }
  306. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  307. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  308. procedure g_removevaluepara_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,tgobj,paramgr,
  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. if (rg.countunusedregsint = 0) then
  630. begin
  631. if (dref.base.enum<>R_NO) and (dref.base.enum<>R_INTREGISTER) then
  632. internalerror(200302037);
  633. if (dref.index.enum<>R_NO) and (dref.index.enum<>R_INTREGISTER) then
  634. internalerror(200302037);
  635. if (dref.base.number shr 8<>RS_EBX) and
  636. (dref.index.number shr 8<>RS_EBX) then
  637. pushed_reg.number:=NR_EBX
  638. else if (dref.base.number<>RS_EAX) and
  639. (dref.index.number<>RS_EAX) then
  640. pushed_reg.number:=NR_EAX
  641. else
  642. pushed_reg.number:=NR_ECX;
  643. tmpreg.enum:=R_INTREGISTER;
  644. tmpreg.number:=(pushed_reg.number and not $ff) or R_SUBL;
  645. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  646. end
  647. else
  648. tmpreg := rg.getregisterint(list,size)
  649. else
  650. {$endif i386}
  651. {$ifdef newra}
  652. tmpreg:=rg.getregisterint(list,size);
  653. {$else}
  654. tmpreg := get_scratch_reg_int(list,size);
  655. {$endif}
  656. a_load_ref_reg(list,size,sref,tmpreg);
  657. a_load_reg_ref(list,size,tmpreg,dref);
  658. {$ifdef i386}
  659. if size in [OS_8,OS_S8] then
  660. begin
  661. if (pushed_reg.number<>NR_NO) then
  662. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  663. else
  664. rg.ungetregisterint(list,tmpreg)
  665. end
  666. else
  667. {$endif i386}
  668. {$ifdef newra}
  669. rg.ungetregisterint(list,tmpreg);
  670. {$else}
  671. free_scratch_reg(list,tmpreg);
  672. {$endif}
  673. end;
  674. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  675. var
  676. tmpreg: tregister;
  677. begin
  678. {$ifdef newra}
  679. tmpreg:=rg.getregisterint(list,size);
  680. {$else}
  681. tmpreg := get_scratch_reg_int(list,size);
  682. {$endif}
  683. a_load_const_reg(list,size,a,tmpreg);
  684. a_load_reg_ref(list,size,tmpreg,ref);
  685. {$ifdef newra}
  686. rg.ungetregisterint(list,tmpreg);
  687. {$else}
  688. free_scratch_reg(list,tmpreg);
  689. {$endif}
  690. end;
  691. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  692. begin
  693. case loc.loc of
  694. LOC_REFERENCE,LOC_CREFERENCE:
  695. a_load_const_ref(list,loc.size,a,loc.reference);
  696. LOC_REGISTER,LOC_CREGISTER:
  697. a_load_const_reg(list,loc.size,a,loc.register);
  698. else
  699. internalerror(200203272);
  700. end;
  701. end;
  702. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  703. begin
  704. case loc.loc of
  705. LOC_REFERENCE,LOC_CREFERENCE:
  706. a_load_reg_ref(list,size,reg,loc.reference);
  707. LOC_REGISTER,LOC_CREGISTER:
  708. a_load_reg_reg(list,size,loc.size,reg,loc.register);
  709. else
  710. internalerror(200203271);
  711. end;
  712. end;
  713. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  714. begin
  715. case loc.loc of
  716. LOC_REFERENCE,LOC_CREFERENCE:
  717. a_load_ref_reg(list,loc.size,loc.reference,reg);
  718. LOC_REGISTER,LOC_CREGISTER:
  719. a_load_reg_reg(list,loc.size,loc.size,loc.register,reg);
  720. LOC_CONSTANT:
  721. a_load_const_reg(list,loc.size,loc.value,reg);
  722. else
  723. internalerror(200109092);
  724. end;
  725. end;
  726. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  727. begin
  728. case loc.loc of
  729. LOC_REFERENCE,LOC_CREFERENCE:
  730. a_load_ref_ref(list,loc.size,loc.reference,ref);
  731. LOC_REGISTER,LOC_CREGISTER:
  732. a_load_reg_ref(list,loc.size,loc.register,ref);
  733. LOC_CONSTANT:
  734. a_load_const_ref(list,loc.size,loc.value,ref);
  735. else
  736. internalerror(200109302);
  737. end;
  738. end;
  739. procedure tcg.a_call_loc(list : taasmoutput;const loc:tlocation);
  740. begin
  741. case loc.loc of
  742. LOC_REGISTER,LOC_CREGISTER:
  743. cg.a_call_reg(list,loc.register);
  744. LOC_REFERENCE,LOC_CREFERENCE :
  745. cg.a_call_ref(list,loc.reference);
  746. else
  747. internalerror(200203311);
  748. end;
  749. end;
  750. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  751. var
  752. powerval : longint;
  753. begin
  754. optimize_op_const_reg := false;
  755. case op of
  756. { or with zero returns same result }
  757. OP_OR : if a = 0 then optimize_op_const_reg := true;
  758. { and with max returns same result }
  759. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  760. { division by 1 returns result }
  761. OP_DIV :
  762. begin
  763. if a = 1 then
  764. optimize_op_const_reg := true
  765. else if ispowerof2(int64(a), powerval) then
  766. begin
  767. a := powerval;
  768. op:= OP_SHR;
  769. end;
  770. exit;
  771. end;
  772. OP_IDIV:
  773. begin
  774. if a = 1 then
  775. optimize_op_const_reg := true
  776. else if ispowerof2(int64(a), powerval) then
  777. begin
  778. a := powerval;
  779. op:= OP_SAR;
  780. end;
  781. exit;
  782. end;
  783. OP_MUL,OP_IMUL:
  784. begin
  785. if a = 1 then
  786. optimize_op_const_reg := true
  787. else if ispowerof2(int64(a), powerval) then
  788. begin
  789. a := powerval;
  790. op:= OP_SHL;
  791. end;
  792. exit;
  793. end;
  794. OP_SAR,OP_SHL,OP_SHR:
  795. begin
  796. if a = 0 then
  797. optimize_op_const_reg := true;
  798. exit;
  799. end;
  800. end;
  801. end;
  802. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  803. begin
  804. case loc.loc of
  805. LOC_REFERENCE, LOC_CREFERENCE:
  806. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  807. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  808. a_loadfpu_reg_reg(list,loc.register,reg);
  809. else
  810. internalerror(200203301);
  811. end;
  812. end;
  813. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  814. begin
  815. case loc.loc of
  816. LOC_REFERENCE, LOC_CREFERENCE:
  817. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  818. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  819. a_loadfpu_reg_reg(list,reg,loc.register);
  820. else
  821. internalerror(48991);
  822. end;
  823. end;
  824. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  825. var
  826. ref : treference;
  827. t : Tregister;
  828. begin
  829. case locpara.loc of
  830. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  831. a_loadfpu_reg_reg(list,r,locpara.register);
  832. LOC_REFERENCE,LOC_CREFERENCE:
  833. begin
  834. if locpara.sp_fixup<>0 then
  835. begin
  836. t.enum:=R_INTREGISTER;
  837. t.number:=NR_STACK_POINTER_REG;
  838. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  839. end;
  840. reference_reset(ref);
  841. ref.base:=locpara.reference.index;
  842. ref.offset:=locpara.reference.offset;
  843. a_loadfpu_reg_ref(list,size,r,ref);
  844. end
  845. else
  846. internalerror(2002071004);
  847. end;
  848. end;
  849. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  850. var
  851. tmpreg: tregister;
  852. begin
  853. {$ifdef newra}
  854. tmpreg:=rg.getregisterint(list,size);
  855. {$else}
  856. tmpreg := get_scratch_reg_int(list,size);
  857. {$endif}
  858. a_load_ref_reg(list,size,ref,tmpreg);
  859. a_op_const_reg(list,op,a,tmpreg);
  860. a_load_reg_ref(list,size,tmpreg,ref);
  861. {$ifdef newra}
  862. rg.ungetregisterint(list,tmpreg);
  863. {$else}
  864. free_scratch_reg(list,tmpreg);
  865. {$endif}
  866. end;
  867. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  868. begin
  869. case loc.loc of
  870. LOC_REGISTER, LOC_CREGISTER:
  871. a_op_const_reg(list,op,a,loc.register);
  872. LOC_REFERENCE, LOC_CREFERENCE:
  873. a_op_const_ref(list,op,loc.size,a,loc.reference);
  874. else
  875. internalerror(200109061);
  876. end;
  877. end;
  878. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  879. var
  880. tmpreg: tregister;
  881. begin
  882. {$ifdef newra}
  883. tmpreg:=rg.getregisterint(list,size);
  884. {$else}
  885. tmpreg := get_scratch_reg_int(list,size);
  886. {$endif}
  887. a_load_ref_reg(list,size,ref,tmpreg);
  888. a_op_reg_reg(list,op,size,reg,tmpreg);
  889. a_load_reg_ref(list,size,tmpreg,ref);
  890. {$ifdef newra}
  891. rg.ungetregisterint(list,tmpreg);
  892. {$else}
  893. free_scratch_reg(list,tmpreg);
  894. {$endif}
  895. end;
  896. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  897. var
  898. tmpreg: tregister;
  899. begin
  900. case op of
  901. OP_NOT,OP_NEG:
  902. { handle it as "load ref,reg; op reg" }
  903. begin
  904. a_load_ref_reg(list,size,ref,reg);
  905. a_op_reg_reg(list,op,size,reg,reg);
  906. end;
  907. else
  908. begin
  909. {$ifdef newra}
  910. tmpreg:=rg.getregisterint(list,size);
  911. {$else}
  912. tmpreg := get_scratch_reg_int(list,size);
  913. {$endif}
  914. a_load_ref_reg(list,size,ref,tmpreg);
  915. a_op_reg_reg(list,op,size,tmpreg,reg);
  916. {$ifdef newra}
  917. rg.ungetregisterint(list,tmpreg);
  918. {$else}
  919. free_scratch_reg(list,tmpreg);
  920. {$endif}
  921. end;
  922. end;
  923. end;
  924. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  925. begin
  926. case loc.loc of
  927. LOC_REGISTER, LOC_CREGISTER:
  928. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  929. LOC_REFERENCE, LOC_CREFERENCE:
  930. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  931. else
  932. internalerror(200109061);
  933. end;
  934. end;
  935. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  936. var
  937. tmpreg: tregister;
  938. begin
  939. case loc.loc of
  940. LOC_REGISTER,LOC_CREGISTER:
  941. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  942. LOC_REFERENCE,LOC_CREFERENCE:
  943. begin
  944. {$ifdef newra}
  945. tmpreg:=rg.getregisterint(list,loc.size);
  946. {$else}
  947. tmpreg := get_scratch_reg_int(list,loc.size);
  948. {$endif}
  949. a_load_ref_reg(list,loc.size,ref,tmpreg);
  950. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  951. {$ifdef newra}
  952. rg.ungetregisterint(list,tmpreg);
  953. {$else}
  954. free_scratch_reg(list,tmpreg);
  955. {$endif}
  956. end;
  957. else
  958. internalerror(200109061);
  959. end;
  960. end;
  961. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  962. size: tcgsize; a: aword; src, dst: tregister);
  963. begin
  964. a_load_reg_reg(list,size,size,src,dst);
  965. a_op_const_reg(list,op,a,dst);
  966. end;
  967. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  968. size: tcgsize; src1, src2, dst: tregister);
  969. begin
  970. a_load_reg_reg(list,size,size,src2,dst);
  971. a_op_reg_reg(list,op,size,src1,dst);
  972. end;
  973. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  974. l : tasmlabel);
  975. var
  976. tmpreg: tregister;
  977. begin
  978. {$ifdef newra}
  979. tmpreg:=rg.getregisterint(list,size);
  980. {$else}
  981. tmpreg := get_scratch_reg_int(list,size);
  982. {$endif}
  983. a_load_ref_reg(list,size,ref,tmpreg);
  984. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  985. {$ifdef newra}
  986. rg.ungetregisterint(list,tmpreg);
  987. {$else}
  988. free_scratch_reg(list,tmpreg);
  989. {$endif}
  990. end;
  991. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  992. l : tasmlabel);
  993. begin
  994. case loc.loc of
  995. LOC_REGISTER,LOC_CREGISTER:
  996. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  997. LOC_REFERENCE,LOC_CREFERENCE:
  998. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  999. else
  1000. internalerror(200109061);
  1001. end;
  1002. end;
  1003. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1004. var
  1005. tmpreg: tregister;
  1006. begin
  1007. {$ifdef newra}
  1008. tmpreg:=rg.getregisterint(list,size);
  1009. {$else}
  1010. tmpreg := get_scratch_reg_int(list,size);
  1011. {$endif}
  1012. a_load_ref_reg(list,size,ref,tmpreg);
  1013. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1014. {$ifdef newra}
  1015. rg.ungetregisterint(list,tmpreg);
  1016. {$else}
  1017. free_scratch_reg(list,tmpreg);
  1018. {$endif}
  1019. end;
  1020. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1021. begin
  1022. case loc.loc of
  1023. LOC_REGISTER,
  1024. LOC_CREGISTER:
  1025. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1026. LOC_REFERENCE,
  1027. LOC_CREFERENCE :
  1028. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1029. LOC_CONSTANT:
  1030. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1031. else
  1032. internalerror(200203231);
  1033. end;
  1034. end;
  1035. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1036. l : tasmlabel);
  1037. var
  1038. tmpreg: tregister;
  1039. begin
  1040. case loc.loc of
  1041. LOC_REGISTER,LOC_CREGISTER:
  1042. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1043. LOC_REFERENCE,LOC_CREFERENCE:
  1044. begin
  1045. {$ifdef newra}
  1046. tmpreg := rg.getregisterint(list,size);
  1047. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1048. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1049. rg.ungetregisterint(list,tmpreg);
  1050. {$else newra}
  1051. {$ifdef i386}
  1052. { the following is done with defines to avoid a speed penalty, }
  1053. { since all this is only necessary for the 80x86 (because EDI }
  1054. { doesn't have an 8bit component which is directly addressable) }
  1055. if size in [OS_8,OS_S8] then
  1056. tmpreg := rg.getregisterint(list,size)
  1057. else
  1058. {$endif i386}
  1059. tmpreg := get_scratch_reg_int(list,size);
  1060. {tmpreg := rg.makeregsize(tmpreg,size);}
  1061. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1062. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1063. {$ifdef i386}
  1064. if size in [OS_8,OS_S8] then
  1065. rg.ungetregisterint(list,tmpreg)
  1066. else
  1067. {$endif i386}
  1068. free_scratch_reg(list,tmpreg);
  1069. {$endif newra}
  1070. end
  1071. else
  1072. internalerror(200109061);
  1073. end;
  1074. end;
  1075. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  1076. begin
  1077. reg_cgsize := OS_INT;
  1078. end;
  1079. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  1080. begin
  1081. {$ifdef FPC}
  1082. {$warning FIX ME!}
  1083. {$endif}
  1084. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  1085. if loadref then
  1086. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  1087. else
  1088. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  1089. if delsource then
  1090. reference_release(list,source);
  1091. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  1092. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1093. end;
  1094. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1095. var
  1096. href : treference;
  1097. incrfunc : string;
  1098. begin
  1099. { These functions should not change the registers (they use
  1100. the saveregister proc directive }
  1101. if is_interfacecom(t) then
  1102. incrfunc:='FPC_INTF_INCR_REF'
  1103. else if is_ansistring(t) then
  1104. incrfunc:='FPC_ANSISTR_INCR_REF'
  1105. else if is_widestring(t) then
  1106. incrfunc:='FPC_WIDESTR_INCR_REF'
  1107. else if is_dynamic_array(t) then
  1108. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1109. else
  1110. incrfunc:='';
  1111. { call the special incr function or the generic addref }
  1112. if incrfunc<>'' then
  1113. begin
  1114. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  1115. a_call_name(list,incrfunc);
  1116. end
  1117. else
  1118. begin
  1119. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1120. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1121. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1122. a_call_name(list,'FPC_ADDREF');
  1123. end;
  1124. end;
  1125. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1126. var
  1127. href : treference;
  1128. decrfunc : string;
  1129. needrtti : boolean;
  1130. begin
  1131. needrtti:=false;
  1132. if is_interfacecom(t) then
  1133. decrfunc:='FPC_INTF_DECR_REF'
  1134. else if is_ansistring(t) then
  1135. decrfunc:='FPC_ANSISTR_DECR_REF'
  1136. else if is_widestring(t) then
  1137. decrfunc:='FPC_WIDESTR_DECR_REF'
  1138. else if is_dynamic_array(t) then
  1139. begin
  1140. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1141. needrtti:=true;
  1142. end
  1143. else
  1144. decrfunc:='';
  1145. { call the special decr function or the generic decref }
  1146. if decrfunc<>'' then
  1147. begin
  1148. if needrtti then
  1149. begin
  1150. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1151. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1152. end;
  1153. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1154. a_call_name(list,decrfunc);
  1155. end
  1156. else
  1157. begin
  1158. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1159. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1160. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1161. a_call_name(list,'FPC_DECREF');
  1162. end;
  1163. end;
  1164. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1165. var
  1166. href : treference;
  1167. begin
  1168. if is_ansistring(t) or
  1169. is_widestring(t) or
  1170. is_interfacecom(t) then
  1171. a_load_const_ref(list,OS_ADDR,0,ref)
  1172. else
  1173. begin
  1174. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1175. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1176. if loadref then
  1177. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1178. else
  1179. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1180. a_call_name(list,'FPC_INITIALIZE');
  1181. end;
  1182. end;
  1183. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1184. var
  1185. href : treference;
  1186. begin
  1187. if is_ansistring(t) or
  1188. is_widestring(t) or
  1189. is_interfacecom(t) then
  1190. g_decrrefcount(list,t,ref)
  1191. else
  1192. begin
  1193. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1194. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1195. if loadref then
  1196. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1197. else
  1198. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1199. a_call_name(list,'FPC_FINALIZE');
  1200. end;
  1201. end;
  1202. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1203. { generate range checking code for the value at location p. The type }
  1204. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1205. { is the original type used at that location. When both defs are equal }
  1206. { the check is also insert (needed for succ,pref,inc,dec) }
  1207. const
  1208. {$ifdef ver1_0}
  1209. awordsignedmax=high(longint);
  1210. {$else}
  1211. awordsignedmax=high(aword) div 2;
  1212. {$endif}
  1213. var
  1214. neglabel : tasmlabel;
  1215. hreg : tregister;
  1216. fromdef : tdef;
  1217. lto,hto,
  1218. lfrom,hfrom : TConstExprInt;
  1219. from_signed: boolean;
  1220. begin
  1221. { range checking on and range checkable value? }
  1222. if not(cs_check_range in aktlocalswitches) or
  1223. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1224. exit;
  1225. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  1226. begin
  1227. cg64.g_rangecheck64(list,p,todef);
  1228. exit;
  1229. end;
  1230. { only check when assigning to scalar, subranges are different, }
  1231. { when todef=fromdef then the check is always generated }
  1232. fromdef:=p.resulttype.def;
  1233. getrange(p.resulttype.def,lfrom,hfrom);
  1234. getrange(todef,lto,hto);
  1235. { no range check if from and to are equal and are both longint/dword }
  1236. { (if we have a 32bit processor) or int64/qword, since such }
  1237. { operations can at most cause overflows (JM) }
  1238. { Note that these checks are mostly processor independent, they only }
  1239. { have to be changed once we introduce 64bit subrange types }
  1240. if (fromdef = todef) and
  1241. (fromdef.deftype=orddef) and
  1242. (((sizeof(aword) = 4) and
  1243. (((torddef(fromdef).typ = s32bit) and
  1244. (lfrom = low(longint)) and
  1245. (hfrom = high(longint))) or
  1246. ((torddef(fromdef).typ = u32bit) and
  1247. (lfrom = low(cardinal)) and
  1248. (hfrom = high(cardinal)))))) then
  1249. exit;
  1250. if todef<>fromdef then
  1251. begin
  1252. { if the from-range falls completely in the to-range, no check }
  1253. { is necessary }
  1254. if (lto<=lfrom) and (hto>=hfrom) then
  1255. exit;
  1256. end;
  1257. { generate the rangecheck code for the def where we are going to }
  1258. { store the result }
  1259. { use the trick that }
  1260. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1261. { To be able to do that, we have to make sure however that either }
  1262. { fromdef and todef are both signed or unsigned, or that we leave }
  1263. { the parts < 0 and > maxlongint out }
  1264. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1265. from_signed := is_signed(fromdef);
  1266. if from_signed xor is_signed(todef) then
  1267. if from_signed then
  1268. { from is signed, to is unsigned }
  1269. begin
  1270. { if high(from) < 0 -> always range error }
  1271. if (hfrom < 0) or
  1272. { if low(to) > maxlongint also range error }
  1273. (lto > awordsignedmax) then
  1274. begin
  1275. a_call_name(list,'FPC_RANGEERROR');
  1276. exit
  1277. end;
  1278. { from is signed and to is unsigned -> when looking at from }
  1279. { as an unsigned value, it must be < maxlongint (otherwise }
  1280. { it's negative, which is invalid since "to" is unsigned) }
  1281. if hto > awordsignedmax then
  1282. hto := awordsignedmax;
  1283. end
  1284. else
  1285. { from is unsigned, to is signed }
  1286. begin
  1287. if (lfrom > awordsignedmax) or
  1288. (hto < 0) then
  1289. begin
  1290. a_call_name(list,'FPC_RANGEERROR');
  1291. exit
  1292. end;
  1293. { from is unsigned and to is signed -> when looking at to }
  1294. { as an unsigned value, it must be >= 0 (since negative }
  1295. { values are the same as values > maxlongint) }
  1296. if lto < 0 then
  1297. lto := 0;
  1298. end;
  1299. {$ifdef newra}
  1300. hreg:=rg.getregisterint(list,OS_INT);
  1301. {$else}
  1302. hreg := get_scratch_reg_int(list,OS_INT);
  1303. {$endif}
  1304. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1305. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1306. aword(lto),p.location.register,hreg)
  1307. else
  1308. begin
  1309. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1310. p.location.reference,hreg);
  1311. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1312. end;
  1313. objectlibrary.getlabel(neglabel);
  1314. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1315. { !!! should happen right after the compare (JM) }
  1316. {$ifdef newra}
  1317. rg.ungetregisterint(list,hreg);
  1318. {$else}
  1319. free_scratch_reg(list,hreg);
  1320. {$endif}
  1321. a_call_name(list,'FPC_RANGEERROR');
  1322. a_label(list,neglabel);
  1323. end;
  1324. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1325. begin
  1326. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1327. a_call_name(list,'FPC_STACKCHECK');
  1328. end;
  1329. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1330. var
  1331. tmpreg : tregister;
  1332. begin
  1333. {$ifdef newra}
  1334. tmpreg:=rg.getregisterint(list,size);
  1335. {$else}
  1336. tmpreg := get_scratch_reg_int(list,size);
  1337. {$endif}
  1338. g_flags2reg(list,size,f,tmpreg);
  1339. a_load_reg_ref(list,size,tmpreg,ref);
  1340. {$ifdef newra}
  1341. rg.ungetregisterint(list,tmpreg);
  1342. {$else}
  1343. free_scratch_reg(list,tmpreg);
  1344. {$endif}
  1345. end;
  1346. function tcg.g_load_self(list : taasmoutput):tregister;
  1347. var
  1348. hp : treference;
  1349. p : tprocinfo;
  1350. i : longint;
  1351. self_reg : tregister;
  1352. begin
  1353. if not assigned(procinfo._class) then
  1354. internalerror(200303211);
  1355. self_reg:=rg.getaddressregister(list);
  1356. if lexlevel>normal_function_level then
  1357. begin
  1358. reference_reset_base(hp,procinfo.framepointer,procinfo.framepointer_offset);
  1359. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1360. p:=procinfo.parent;
  1361. for i:=3 to lexlevel-1 do
  1362. begin
  1363. reference_reset_base(hp,self_reg,p.framepointer_offset);
  1364. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1365. p:=p.parent;
  1366. end;
  1367. reference_reset_base(hp,self_reg,p.selfpointer_offset);
  1368. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1369. end
  1370. else
  1371. begin
  1372. reference_reset_base(hp,procinfo.framepointer,procinfo.selfpointer_offset);
  1373. a_load_ref_reg(list,OS_ADDR,hp,self_reg);
  1374. end;
  1375. g_load_self:=self_reg;
  1376. end;
  1377. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1378. var
  1379. OKLabel : tasmlabel;
  1380. begin
  1381. if (cs_check_object in aktlocalswitches) or
  1382. (cs_check_range in aktlocalswitches) then
  1383. begin
  1384. objectlibrary.getlabel(oklabel);
  1385. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1386. a_param_const(list,OS_INT,210,paramanager.getintparaloc(1));
  1387. a_call_name(list,'FPC_HANDLEERROR');
  1388. a_label(list,oklabel);
  1389. end;
  1390. end;
  1391. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1392. var
  1393. hrefvmt : treference;
  1394. begin
  1395. if (cs_check_object in aktlocalswitches) then
  1396. begin
  1397. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymboldata(objdef.vmt_mangledname),0);
  1398. a_paramaddr_ref(list,hrefvmt,paramanager.getintparaloc(2));
  1399. a_param_reg(list,OS_ADDR,reg,paramanager.getintparaloc(1));
  1400. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1401. end
  1402. else
  1403. if (cs_check_range in aktlocalswitches) then
  1404. begin
  1405. a_param_reg(list,OS_ADDR,reg,paramanager.getintparaloc(1));
  1406. a_call_name(list,'FPC_CHECK_OBJECT');
  1407. end;
  1408. end;
  1409. {*****************************************************************************
  1410. Entry/Exit Code Functions
  1411. *****************************************************************************}
  1412. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1413. var
  1414. href : treference;
  1415. acc : Tregister;
  1416. begin
  1417. if procinfo.vmtpointer_offset=0 then
  1418. internalerror(200303251);
  1419. if procinfo.selfpointer_offset=0 then
  1420. internalerror(200303252);
  1421. acc.enum:=R_INTREGISTER;
  1422. acc.number:=NR_ACCUMULATOR;
  1423. if is_class(procinfo._class) then
  1424. begin
  1425. if (cs_implicit_exceptions in aktmoduleswitches) then
  1426. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1427. { parameter 2 : vmt pointer, 0 when called by inherited }
  1428. reference_reset_base(href, procinfo.framepointer,procinfo.vmtpointer_offset);
  1429. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1430. { parameter 1 : self pointer }
  1431. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1432. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1433. a_call_name(list,'FPC_NEW_CLASS');
  1434. { save the self pointer }
  1435. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1436. a_load_reg_ref(list,OS_ADDR,acc,href);
  1437. { fail? }
  1438. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1439. end
  1440. else if is_object(procinfo._class) then
  1441. begin
  1442. { parameter 3 : vmt_offset }
  1443. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1444. { parameter 2 : address of pointer to vmt,
  1445. this is required to allow setting the vmt to -1 to indicate
  1446. that memory was allocated }
  1447. reference_reset_base(href, procinfo.framepointer,procinfo.vmtpointer_offset);
  1448. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1449. { parameter 1 : self pointer }
  1450. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1451. a_param_ref(list,OS_ADDR,href,paramanager.getintparaloc(1));
  1452. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1453. { save the self pointer }
  1454. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1455. a_load_reg_ref(list,OS_ADDR,acc,href);
  1456. { fail? }
  1457. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1458. end
  1459. else
  1460. internalerror(200006161);
  1461. end;
  1462. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1463. var
  1464. nofinal : tasmlabel;
  1465. href : treference;
  1466. reg : tregister;
  1467. begin
  1468. if is_class(procinfo._class) then
  1469. begin
  1470. if procinfo.selfpointer_offset=0 then
  1471. internalerror(200303253);
  1472. { parameter 2 : flag }
  1473. if procinfo.inheritedflag_offset=0 then
  1474. internalerror(200303251);
  1475. reference_reset_base(href, procinfo.framepointer,procinfo.inheritedflag_offset);
  1476. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1477. { parameter 1 : self }
  1478. if procinfo.selfpointer_offset=0 then
  1479. internalerror(200303252);
  1480. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1481. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1482. a_call_name(list,'FPC_DISPOSE_CLASS')
  1483. end
  1484. else if is_object(procinfo._class) then
  1485. begin
  1486. if procinfo.selfpointer_offset=0 then
  1487. internalerror(200303254);
  1488. if procinfo.vmtpointer_offset=0 then
  1489. internalerror(200303255);
  1490. { must the object be finalized ? }
  1491. if procinfo._class.needs_inittable then
  1492. begin
  1493. objectlibrary.getlabel(nofinal);
  1494. reference_reset_base(href,procinfo.framepointer,target_info.first_parm_offset);
  1495. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1496. reg:=g_load_self(list);
  1497. reference_reset_base(href,reg,0);
  1498. g_finalize(list,procinfo._class,href,false);
  1499. reference_release(list,href);
  1500. a_label(list,nofinal);
  1501. end;
  1502. { actually call destructor }
  1503. { parameter 3 : vmt_offset }
  1504. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1505. { parameter 2 : pointer to vmt }
  1506. reference_reset_base(href, procinfo.framepointer,procinfo.vmtpointer_offset);
  1507. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1508. { parameter 1 : address of self pointer }
  1509. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1510. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(1));
  1511. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1512. end
  1513. else
  1514. internalerror(200006162);
  1515. end;
  1516. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1517. var
  1518. href : treference;
  1519. begin
  1520. if is_class(procinfo._class) then
  1521. begin
  1522. if procinfo.selfpointer_offset=0 then
  1523. internalerror(200303256);
  1524. { parameter 2 : flag, 0 -> inherited call (=no dispose) }
  1525. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1526. { parameter 1 : self }
  1527. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1528. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1529. a_call_name(list,'FPC_DISPOSE_CLASS');
  1530. end
  1531. else if is_object(procinfo._class) then
  1532. begin
  1533. if procinfo.selfpointer_offset=0 then
  1534. internalerror(200303257);
  1535. if procinfo.vmtpointer_offset=0 then
  1536. internalerror(200303258);
  1537. { parameter 3 : vmt_offset }
  1538. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1539. { parameter 2 : address of pointer to vmt, will be reset to 0 when freed }
  1540. reference_reset_base(href, procinfo.framepointer,procinfo.vmtpointer_offset);
  1541. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1542. { parameter 1 : address of self pointer }
  1543. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1544. a_paramaddr_ref(list,href,paramanager.getintparaloc(1));
  1545. a_call_name(list,'FPC_HELP_FAIL');
  1546. end
  1547. else
  1548. internalerror(200006163);
  1549. { set self to nil }
  1550. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1551. a_load_const_ref(list,OS_ADDR,0,href);
  1552. end;
  1553. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1554. begin
  1555. end;
  1556. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1557. begin
  1558. end;
  1559. procedure tcg.g_profilecode(list : taasmoutput);
  1560. begin
  1561. end;
  1562. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1563. var r:Tregister;
  1564. begin
  1565. r.enum:=R_INTREGISTER;;
  1566. r.number:=NR_ACCUMULATOR;
  1567. a_load_reg_ref(list, OS_S32, r, href);
  1568. end;
  1569. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1570. begin
  1571. a_load_const_ref(list, OS_S32, a, href);
  1572. end;
  1573. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1574. var r:Tregister;
  1575. begin
  1576. r.enum:=R_INTREGISTER;;
  1577. r.number:=NR_ACCUMULATOR;
  1578. a_load_ref_reg(list, OS_S32, href, r);
  1579. end;
  1580. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1581. regsrc,regdst : tregister64);
  1582. begin
  1583. a_load64_reg_reg(list,regsrc,regdst);
  1584. a_op64_const_reg(list,op,value,regdst);
  1585. end;
  1586. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1587. begin
  1588. a_load64_reg_reg(list,regsrc2,regdst);
  1589. a_op64_reg_reg(list,op,regsrc1,regdst);
  1590. end;
  1591. initialization
  1592. ;
  1593. finalization
  1594. cg.free;
  1595. cg64.free;
  1596. end.
  1597. {
  1598. $Log$
  1599. Revision 1.82 2003-04-22 10:09:34 daniel
  1600. + Implemented the actual register allocator
  1601. + Scratch registers unavailable when new register allocator used
  1602. + maybe_save/maybe_restore unavailable when new register allocator used
  1603. Revision 1.81 2003/04/06 21:11:23 olle
  1604. * changed newasmsymbol to newasmsymboldata for data symbols
  1605. Revision 1.80 2003/03/28 19:16:56 peter
  1606. * generic constructor working for i386
  1607. * remove fixed self register
  1608. * esi added as address register for i386
  1609. Revision 1.79 2003/03/22 18:07:18 jonas
  1610. + add used scratch registers to usedintbyproc for non-i386
  1611. Revision 1.78 2003/03/11 21:46:24 jonas
  1612. * lots of new regallocator fixes, both in generic and ppc-specific code
  1613. (ppc compiler still can't compile the linux system unit though)
  1614. Revision 1.77 2003/02/19 22:00:14 daniel
  1615. * Code generator converted to new register notation
  1616. - Horribily outdated todo.txt removed
  1617. Revision 1.76 2003/01/31 22:47:48 peter
  1618. * maybe_testself now really uses the passed register
  1619. Revision 1.75 2003/01/30 21:46:35 peter
  1620. * maybe_testvmt added
  1621. Revision 1.74 2003/01/17 12:45:40 daniel
  1622. * Fixed internalerror 200301081 problem
  1623. Revision 1.73 2003/01/13 14:54:34 daniel
  1624. * Further work to convert codegenerator register convention;
  1625. internalerror bug fixed.
  1626. Revision 1.72 2003/01/09 22:00:53 florian
  1627. * fixed some PowerPC issues
  1628. Revision 1.71 2003/01/09 20:41:10 florian
  1629. * fixed broken PowerPC compiler
  1630. Revision 1.70 2003/01/08 18:43:56 daniel
  1631. * Tregister changed into a record
  1632. Revision 1.69 2002/12/24 15:56:50 peter
  1633. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1634. this for the pageprotection
  1635. Revision 1.68 2002/12/20 18:14:04 peter
  1636. * removed some runerror and writeln
  1637. Revision 1.67 2002/12/14 15:02:03 carl
  1638. * maxoperands -> max_operands (for portability in rautils.pas)
  1639. * fix some range-check errors with loadconst
  1640. + add ncgadd unit to m68k
  1641. * some bugfix of a_param_reg with LOC_CREFERENCE
  1642. Revision 1.66 2002/11/25 17:43:16 peter
  1643. * splitted defbase in defutil,symutil,defcmp
  1644. * merged isconvertable and is_equal into compare_defs(_ext)
  1645. * made operator search faster by walking the list only once
  1646. Revision 1.65 2002/11/17 16:27:31 carl
  1647. * document flags2reg
  1648. Revision 1.64 2002/11/16 17:06:28 peter
  1649. * return error 210 for failed self test
  1650. Revision 1.63 2002/11/15 01:58:46 peter
  1651. * merged changes from 1.0.7 up to 04-11
  1652. - -V option for generating bug report tracing
  1653. - more tracing for option parsing
  1654. - errors for cdecl and high()
  1655. - win32 import stabs
  1656. - win32 records<=8 are returned in eax:edx (turned off by default)
  1657. - heaptrc update
  1658. - more info for temp management in .s file with EXTDEBUG
  1659. Revision 1.62 2002/10/16 19:01:43 peter
  1660. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1661. implicit exception frames for procedures with initialized variables
  1662. and for constructors. The default is on for compatibility
  1663. Revision 1.61 2002/10/05 12:43:23 carl
  1664. * fixes for Delphi 6 compilation
  1665. (warning : Some features do not work under Delphi)
  1666. Revision 1.60 2002/10/02 18:20:52 peter
  1667. * Copy() is now internal syssym that calls compilerprocs
  1668. Revision 1.59 2002/09/17 18:54:02 jonas
  1669. * a_load_reg_reg() now has two size parameters: source and dest. This
  1670. allows some optimizations on architectures that don't encode the
  1671. register size in the register name.
  1672. Revision 1.58 2002/09/09 19:29:29 peter
  1673. * fixed dynarr_decr_ref call
  1674. Revision 1.57 2002/09/07 15:25:01 peter
  1675. * old logs removed and tabs fixed
  1676. Revision 1.56 2002/09/01 21:04:47 florian
  1677. * several powerpc related stuff fixed
  1678. Revision 1.55 2002/09/01 17:05:43 florian
  1679. + added abstract tcg.g_removevaluepara_openarray
  1680. Revision 1.54 2002/09/01 12:09:27 peter
  1681. + a_call_reg, a_call_loc added
  1682. * removed exprasmlist references
  1683. Revision 1.53 2002/08/19 18:17:48 carl
  1684. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1685. * more fixes to m68k for 64-bit operations
  1686. Revision 1.52 2002/08/17 22:09:43 florian
  1687. * result type handling in tcgcal.pass_2 overhauled
  1688. * better tnode.dowrite
  1689. * some ppc stuff fixed
  1690. Revision 1.51 2002/08/17 09:23:33 florian
  1691. * first part of procinfo rewrite
  1692. Revision 1.50 2002/08/16 14:24:57 carl
  1693. * issameref() to test if two references are the same (then emit no opcodes)
  1694. + ret_in_reg to replace ret_in_acc
  1695. (fix some register allocation bugs at the same time)
  1696. + save_std_register now has an extra parameter which is the
  1697. usedinproc registers
  1698. Revision 1.49 2002/08/15 08:13:54 carl
  1699. - a_load_sym_ofs_reg removed
  1700. * loadvmt now calls loadaddr_ref_reg instead
  1701. Revision 1.48 2002/08/14 19:26:02 carl
  1702. + routine to optimize opcodes with constants
  1703. Revision 1.47 2002/08/11 14:32:26 peter
  1704. * renamed current_library to objectlibrary
  1705. Revision 1.46 2002/08/11 13:24:11 peter
  1706. * saving of asmsymbols in ppu supported
  1707. * asmsymbollist global is removed and moved into a new class
  1708. tasmlibrarydata that will hold the info of a .a file which
  1709. corresponds with a single module. Added librarydata to tmodule
  1710. to keep the library info stored for the module. In the future the
  1711. objectfiles will also be stored to the tasmlibrarydata class
  1712. * all getlabel/newasmsymbol and friends are moved to the new class
  1713. Revision 1.45 2002/08/10 17:15:20 jonas
  1714. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1715. Revision 1.44 2002/08/09 19:10:05 carl
  1716. - moved new_exception and free_exception to ncgutils
  1717. Revision 1.43 2002/08/05 18:27:48 carl
  1718. + more more more documentation
  1719. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1720. Revision 1.42 2002/08/04 19:08:21 carl
  1721. + added generic exception support (still does not work!)
  1722. + more documentation
  1723. Revision 1.41 2002/07/30 20:50:43 florian
  1724. * the code generator knows now if parameters are in registers
  1725. Revision 1.40 2002/07/29 21:16:02 florian
  1726. * some more ppc fixes
  1727. Revision 1.39 2002/07/28 15:56:00 jonas
  1728. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1729. generic implementation
  1730. Revision 1.38 2002/07/27 19:53:51 jonas
  1731. + generic implementation of tcg.g_flags2ref()
  1732. * tcg.flags2xxx() now also needs a size parameter
  1733. Revision 1.37 2002/07/20 11:57:53 florian
  1734. * types.pas renamed to defbase.pas because D6 contains a types
  1735. unit so this would conflicts if D6 programms are compiled
  1736. + Willamette/SSE2 instructions to assembler added
  1737. }