cgobj.pas 79 KB

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