cgobj.pas 77 KB

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