cgobj.pas 82 KB

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