cgobj.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the basic code generator object
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. {# @abstract(Abstract code generator unit)
  20. Abstreact code generator unit. This contains the base class
  21. to implement for all new supported processors.
  22. WARNING: None of the routines implemented in these modules,
  23. or their descendants, should use the temp. allocator, as
  24. these routines may be called inside genentrycode, and the
  25. stack frame is already setup!
  26. }
  27. unit cgobj;
  28. {$i fpcdefs.inc}
  29. interface
  30. uses
  31. cclasses,aasmbase,aasmtai,aasmcpu,symtable,
  32. cpubase,cpuinfo,cpupara,
  33. cginfo,
  34. symconst,symbase,symtype,node
  35. {$ifdef delphi}
  36. ,dmisc
  37. {$endif}
  38. ;
  39. type
  40. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  41. {# @abstract(Abstract code generator)
  42. This class implements an abstract instruction generator. Some of
  43. the methods of this class are generic, while others must
  44. be overriden for all new processors which will be supported
  45. by Free Pascal. For 32-bit processors, the base class
  46. sould be @link(tcg64f32) and not @var(tcg).
  47. }
  48. tcg = class
  49. scratch_register_array_pointer : aword;
  50. {# List of currently unused scratch registers }
  51. unusedscratchregisters : tregisterset;
  52. alignment : talignment;
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. { returns the tcgsize corresponding with the size of reg }
  57. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  58. {# Emit a label to the instruction stream. }
  59. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  60. {# Allocates register r by inserting a pai_realloc record }
  61. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  62. {# Deallocates register r by inserting a pa_regdealloc record}
  63. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  64. {# @abstract(Returns an int register for use as scratch register)
  65. This routine returns a register which can be used by
  66. the code generator as a general purpose scratch register.
  67. Since scratch_registers are scarce resources, the register
  68. should be freed by calling @link(free_scratch_reg) as
  69. soon as it is no longer required.
  70. }
  71. function get_scratch_reg_int(list : taasmoutput) : tregister;virtual;
  72. {# @abstract(Returns an address register for use as scratch register)
  73. This routine returns a register which can be used by
  74. the code generator as a pointer scratch register.
  75. Since scratch_registers are scarce resources, the register
  76. should be freed by calling @link(free_scratch_reg) as
  77. soon as it is no longer required.
  78. }
  79. function get_scratch_reg_address(list : taasmoutput) : tregister;virtual;
  80. {# @abstract(Releases a scratch register)
  81. Releases a scratch register.
  82. This routine is used to free a register which
  83. was previously allocated using @link(get_scratch_reg).
  84. }
  85. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  86. {# Pass a parameter, which is located in a register, to a routine.
  87. This routine should push/send the parameter to the routine, as
  88. required by the specific processor ABI and routine modifiers.
  89. This must be overriden for each CPU target.
  90. @param(size size of the operand in the register)
  91. @param(r register source of the operand)
  92. @param(locpara where the parameter will be stored)
  93. }
  94. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  95. {# Pass a parameter, which is a constant, to a routine.
  96. A generic version is provided. This routine should
  97. be overriden for optimization purposes if the cpu
  98. permits directly sending this type of parameter.
  99. @param(size size of the operand in constant)
  100. @param(a value of constant to send)
  101. @param(locpara where the parameter will be stored)
  102. }
  103. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  104. {# Pass the value of a parameter, which is located in memory, to a routine.
  105. A generic version is provided. This routine should
  106. be overriden for optimization purposes if the cpu
  107. permits directly sending this type of parameter.
  108. @param(size size of the operand in constant)
  109. @param(r Memory reference of value to send)
  110. @param(locpara where the parameter will be stored)
  111. }
  112. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  113. {# Pass the value of a parameter, which can be located either in a register or memory location,
  114. to a routine.
  115. A generic version is provided.
  116. @param(l location of the operand to send)
  117. @param(nr parameter number (starting from one) of routine (from left to right))
  118. @param(locpara where the parameter will be stored)
  119. }
  120. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  121. {# Pass the address of a reference to a routine. This routine
  122. will calculate the address of the reference, and pass this
  123. calculated address as a parameter.
  124. A generic version is provided. This routine should
  125. be overriden for optimization purposes if the cpu
  126. permits directly sending this type of parameter.
  127. @param(r reference to get address from)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. }
  130. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  131. (* Remarks:
  132. * If a method specifies a size you have only to take care
  133. of that number of bits, i.e. load_const_reg with OP_8 must
  134. only load the lower 8 bit of the specified register
  135. the rest of the register can be undefined
  136. if necessary the compiler will call a method
  137. to zero or sign extend the register
  138. * The a_load_XX_XX with OP_64 needn't to be
  139. implemented for 32 bit
  140. processors, the code generator takes care of that
  141. * the addr size is for work with the natural pointer
  142. size
  143. * the procedures without fpu/mm are only for integer usage
  144. * normally the first location is the source and the
  145. second the destination
  146. *)
  147. {# Emits instruction to call the method specified by symbol name.
  148. This routine must be overriden for each new target cpu.
  149. }
  150. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  151. procedure a_call_ref(list : taasmoutput;const ref : treference);virtual;abstract;
  152. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  153. procedure a_call_loc(list : taasmoutput;const loc:tlocation);
  154. { move instructions }
  155. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  156. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  157. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  158. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  159. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  160. procedure a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  161. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  162. procedure a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);virtual;
  163. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  164. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  165. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  166. { fpu move instructions }
  167. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  168. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  169. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  170. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  171. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  172. { vector register move instructions }
  173. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  174. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  175. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  176. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  177. { basic arithmetic operations }
  178. { note: for operators which require only one argument (not, neg), use }
  179. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  180. { that in this case the *second* operand is used as both source and }
  181. { destination (JM) }
  182. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  183. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  184. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  185. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  186. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  187. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  188. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  189. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  190. { trinary operations for processors that support them, 'emulated' }
  191. { on others. None with "ref" arguments since I don't think there }
  192. { are any processors that support it (JM) }
  193. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  194. size: tcgsize; a: aword; src, dst: tregister); virtual;
  195. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  196. size: tcgsize; src1, src2, dst: tregister); virtual;
  197. { comparison operations }
  198. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  199. l : tasmlabel);virtual; abstract;
  200. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  201. l : tasmlabel); virtual;
  202. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  203. l : tasmlabel);
  204. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  205. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  206. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  207. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  208. l : tasmlabel);
  209. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  210. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  211. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  212. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  213. }
  214. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  215. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  216. {
  217. This routine tries to optimize the const_reg opcode, and should be
  218. called at the start of a_op_const_reg. It returns the actual opcode
  219. to emit, and the constant value to emit. If this routine returns
  220. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  221. @param(op The opcode to emit, returns the opcode which must be emitted)
  222. @param(a The constant which should be emitted, returns the constant which must
  223. be emitted)
  224. @param(reg The register to emit the opcode with, returns the register with
  225. which the opcode will be emitted)
  226. }
  227. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg: tregister): boolean;virtual;
  228. {#
  229. This routine is used in exception management nodes. It should
  230. save the exception reason currently in the accumulator. The
  231. save should be done either to a temp (pointed to by href).
  232. or on the stack (pushing the value on the stack).
  233. The size of the value to save is OS_S32. The default version
  234. saves the exception reason to a temp. memory area.
  235. }
  236. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  237. {#
  238. This routine is used in exception management nodes. It should
  239. save the exception reason constant. The
  240. save should be done either to a temp (pointed to by href).
  241. or on the stack (pushing the value on the stack).
  242. The size of the value to save is OS_S32. The default version
  243. saves the exception reason to a temp. memory area.
  244. }
  245. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);virtual;
  246. {#
  247. This routine is used in exception management nodes. It should
  248. load the exception reason to the accumulator. The saved value
  249. should either be in the temp. area (pointed to by href , href should
  250. *NOT* be freed) or on the stack (the value should be popped).
  251. The size of the value to save is OS_S32. The default version
  252. saves the exception reason to a temp. memory area.
  253. }
  254. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  255. procedure g_maybe_loadself(list : taasmoutput);virtual;
  256. procedure g_maybe_testself(list : taasmoutput);virtual;
  257. {# This should emit the opcode to copy len bytes from the source
  258. to destination, if loadref is true, it assumes that it first must load
  259. the source address from the memory location where
  260. source points to.
  261. It must be overriden for each new target processor.
  262. @param(source Source reference of copy)
  263. @param(dest Destination reference of copy)
  264. @param(delsource Indicates if the source reference's resources should be freed)
  265. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  266. }
  267. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  268. {# This should emit the opcode to a shortrstring from the source
  269. to destination, if loadref is true, it assumes that it first must load
  270. the source address from the memory location where
  271. source points to.
  272. @param(source Source reference of copy)
  273. @param(dest Destination reference of copy)
  274. @param(delsource Indicates if the source reference's resources should be freed)
  275. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  276. }
  277. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  278. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  279. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  280. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  281. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  282. {# Emits the call to the stack checking routine of
  283. the runtime library. The default behavior
  284. does not need to be modified, as it is generic
  285. for all platforms.
  286. @param(stackframesize Number of bytes which will be allocated on the stack)
  287. }
  288. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  289. {# Generates range checking code. It is to note
  290. that this routine does not need to be overriden,
  291. as it takes care of everything.
  292. @param(p Node which contains the value to check)
  293. @param(todef Type definition of node to range check)
  294. }
  295. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  296. const todef: tdef); virtual;
  297. {# Generates overflow checking code for a node }
  298. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  299. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  300. procedure g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  301. {# Emits instructions which should be emitted when entering
  302. a routine declared as @var(interrupt). The default
  303. behavior does nothing, should be overriden as required.
  304. }
  305. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  306. {# Emits instructions which should be emitted when exiting
  307. a routine declared as @var(interrupt). The default
  308. behavior does nothing, should be overriden as required.
  309. }
  310. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;
  311. {# Emits instructions when compilation is done in profile
  312. mode (this is set as a command line option). The default
  313. behavior does nothing, should be overriden as required.
  314. }
  315. procedure g_profilecode(list : taasmoutput);virtual;
  316. {# Emits instruction for allocating the locals in entry
  317. code of a routine. This is one of the first
  318. routine called in @var(genentrycode).
  319. @param(localsize Number of bytes to allocate as locals)
  320. }
  321. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  322. {# Emits instructiona for restoring the frame pointer
  323. at routine exit. For some processors, this routine
  324. may do nothing at all.
  325. }
  326. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  327. {# Emits instructions for returning from a subroutine.
  328. Should also restore the stack.
  329. @param(parasize Number of bytes of parameters to deallocate from stack)
  330. }
  331. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  332. procedure g_call_constructor_helper(list : taasmoutput);virtual;
  333. procedure g_call_destructor_helper(list : taasmoutput);virtual;
  334. procedure g_call_fail_helper(list : taasmoutput);virtual;
  335. {# This routine is called when generating the code for the entry point
  336. of a routine. It should save all registers which are not used in this
  337. routine, and which should be declared as saved in the std_saved_registers
  338. set.
  339. This routine is mainly used when linking to code which is generated
  340. by ABI-compliant compilers (like GCC), to make sure that the reserved
  341. registers of that ABI are not clobbered.
  342. @param(usedinproc Registers which are used in the code of this routine)
  343. }
  344. procedure g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);virtual;abstract;
  345. {# This routine is called when generating the code for the exit point
  346. of a routine. It should restore all registers which were previously
  347. saved in @var(g_save_standard_registers).
  348. @param(usedinproc Registers which are used in the code of this routine)
  349. }
  350. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);virtual;abstract;
  351. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  352. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;abstract;
  353. end;
  354. {# @abstract(Abstract code generator for 64 Bit operations)
  355. This class implements an abstract code generator class
  356. for 64 Bit operations.
  357. }
  358. tcg64 = class
  359. { Allocates 64 Bit register r by inserting a pai_realloc record }
  360. procedure a_reg_alloc(list : taasmoutput;r : tregister64);virtual;abstract;
  361. { Deallocates 64 Bit register r by inserting a pa_regdealloc record}
  362. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);virtual;abstract;
  363. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  364. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  365. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  366. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  367. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  368. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  369. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  370. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  371. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  372. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  373. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  374. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  375. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  376. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  377. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  378. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  379. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  380. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  381. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  382. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  383. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  384. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  385. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  386. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  387. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  388. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  389. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  390. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  391. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  392. {
  393. This routine tries to optimize the const_reg opcode, and should be
  394. called at the start of a_op64_const_reg. It returns the actual opcode
  395. to emit, and the constant value to emit. If this routine returns
  396. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  397. @param(op The opcode to emit, returns the opcode which must be emitted)
  398. @param(a The constant which should be emitted, returns the constant which must
  399. be emitted)
  400. @param(reg The register to emit the opcode with, returns the register with
  401. which the opcode will be emitted)
  402. }
  403. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;virtual;abstract;
  404. { override to catch 64bit rangechecks }
  405. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  406. const todef: tdef);virtual;abstract;
  407. end;
  408. var
  409. {# Main code generator class }
  410. cg : tcg;
  411. {# Code generator class for all operations working with 64-Bit operands }
  412. cg64 : tcg64;
  413. implementation
  414. uses
  415. globals,globtype,options,systems,cgbase,
  416. verbose,defutil,tgobj,symdef,paramgr,
  417. rgobj,cutils;
  418. const
  419. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  420. {*****************************************************************************
  421. basic functionallity
  422. ******************************************************************************}
  423. constructor tcg.create;
  424. var
  425. i : longint;
  426. begin
  427. scratch_register_array_pointer:=1;
  428. for i:=low(scratch_regs) to high(scratch_regs) do
  429. include(unusedscratchregisters,scratch_regs[i]);
  430. end;
  431. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  432. begin
  433. list.concat(tai_regalloc.alloc(r));
  434. end;
  435. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  436. begin
  437. list.concat(tai_regalloc.dealloc(r));
  438. end;
  439. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  440. begin
  441. list.concat(tai_label.create(l));
  442. end;
  443. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  444. var
  445. r : tregister;
  446. i : longint;
  447. begin
  448. if unusedscratchregisters=[] then
  449. internalerror(68996);
  450. for i:=scratch_register_array_pointer to
  451. (scratch_register_array_pointer+max_scratch_regs-1) do
  452. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  453. begin
  454. r:=scratch_regs[(i mod max_scratch_regs)+1];
  455. break;
  456. end;
  457. exclude(unusedscratchregisters,r);
  458. inc(scratch_register_array_pointer);
  459. if scratch_register_array_pointer>max_scratch_regs then
  460. scratch_register_array_pointer:=1;
  461. a_reg_alloc(list,r);
  462. get_scratch_reg_int:=r;
  463. end;
  464. { the default behavior simply returns a general purpose register }
  465. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  466. begin
  467. get_scratch_reg_address := get_scratch_reg_int(list);
  468. end;
  469. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  470. begin
  471. include(unusedscratchregisters,rg.makeregsize(r,OS_INT));
  472. a_reg_dealloc(list,r);
  473. end;
  474. {*****************************************************************************
  475. for better code generation these methods should be overridden
  476. ******************************************************************************}
  477. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  478. var
  479. ref : treference;
  480. begin
  481. case locpara.loc of
  482. LOC_REGISTER,LOC_CREGISTER:
  483. a_load_reg_reg(list,size,locpara.size,r,locpara.register);
  484. LOC_REFERENCE:
  485. begin
  486. if locpara.sp_fixup<>0 then
  487. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,stack_pointer_reg);
  488. reference_reset(ref);
  489. ref.base:=locpara.reference.index;
  490. ref.offset:=locpara.reference.offset;
  491. a_load_reg_ref(list,size,r,ref);
  492. end
  493. else
  494. internalerror(2002071004);
  495. end;
  496. end;
  497. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  498. var
  499. hr : tregister;
  500. begin
  501. hr:=get_scratch_reg_int(list);
  502. a_load_const_reg(list,size,a,hr);
  503. a_param_reg(list,size,hr,locpara);
  504. free_scratch_reg(list,hr);
  505. end;
  506. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  507. var
  508. hr : tregister;
  509. begin
  510. hr:=get_scratch_reg_int(list);
  511. a_load_ref_reg(list,size,r,hr);
  512. a_param_reg(list,size,hr,locpara);
  513. free_scratch_reg(list,hr);
  514. end;
  515. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  516. begin
  517. case l.loc of
  518. LOC_REGISTER,
  519. LOC_CREGISTER :
  520. a_param_reg(list,l.size,l.register,locpara);
  521. LOC_CONSTANT :
  522. a_param_const(list,l.size,l.value,locpara);
  523. LOC_CREFERENCE,
  524. LOC_REFERENCE :
  525. a_param_ref(list,l.size,l.reference,locpara);
  526. else
  527. internalerror(2002032211);
  528. end;
  529. end;
  530. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  531. var
  532. hr : tregister;
  533. begin
  534. hr:=get_scratch_reg_address(list);
  535. a_loadaddr_ref_reg(list,r,hr);
  536. a_param_reg(list,OS_ADDR,hr,locpara);
  537. free_scratch_reg(list,hr);
  538. end;
  539. {****************************************************************************
  540. some generic implementations
  541. ****************************************************************************}
  542. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  543. var
  544. tmpreg: tregister;
  545. {$ifdef i386}
  546. pushed_reg: tregister;
  547. {$endif i386}
  548. begin
  549. { verify if we have the same reference }
  550. if references_equal(sref,dref) then
  551. exit;
  552. {$ifdef i386}
  553. { the following is done with defines to avoid a speed penalty, }
  554. { since all this is only necessary for the 80x86 (because EDI }
  555. { doesn't have an 8bit component which is directly addressable) }
  556. pushed_reg := R_NO;
  557. if size in [OS_8,OS_S8] then
  558. if (rg.countunusedregsint = 0) then
  559. begin
  560. if (dref.base <> R_EBX) and
  561. (dref.index <> R_EBX) then
  562. pushed_reg := R_EBX
  563. else if (dref.base <> R_EAX) and
  564. (dref.index <> R_EAX) then
  565. pushed_reg := R_EAX
  566. else pushed_reg := R_ECX;
  567. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  568. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  569. end
  570. else
  571. tmpreg := rg.getregisterint(list)
  572. else
  573. {$endif i386}
  574. tmpreg := get_scratch_reg_int(list);
  575. tmpreg:=rg.makeregsize(tmpreg,size);
  576. a_load_ref_reg(list,size,sref,tmpreg);
  577. a_load_reg_ref(list,size,tmpreg,dref);
  578. {$ifdef i386}
  579. if size in [OS_8,OS_S8] then
  580. begin
  581. if (pushed_reg <> R_NO) then
  582. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  583. else
  584. rg.ungetregister(list,tmpreg)
  585. end
  586. else
  587. {$endif i386}
  588. free_scratch_reg(list,tmpreg);
  589. end;
  590. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  591. var
  592. tmpreg: tregister;
  593. begin
  594. tmpreg := get_scratch_reg_int(list);
  595. a_load_const_reg(list,size,a,tmpreg);
  596. a_load_reg_ref(list,size,tmpreg,ref);
  597. free_scratch_reg(list,tmpreg);
  598. end;
  599. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  600. begin
  601. case loc.loc of
  602. LOC_REFERENCE,LOC_CREFERENCE:
  603. a_load_const_ref(list,loc.size,a,loc.reference);
  604. LOC_REGISTER,LOC_CREGISTER:
  605. a_load_const_reg(list,loc.size,a,loc.register);
  606. else
  607. internalerror(200203272);
  608. end;
  609. end;
  610. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  611. begin
  612. case loc.loc of
  613. LOC_REFERENCE,LOC_CREFERENCE:
  614. a_load_reg_ref(list,size,reg,loc.reference);
  615. LOC_REGISTER,LOC_CREGISTER:
  616. a_load_reg_reg(list,size,loc.size,reg,loc.register);
  617. else
  618. internalerror(200203271);
  619. end;
  620. end;
  621. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  622. begin
  623. case loc.loc of
  624. LOC_REFERENCE,LOC_CREFERENCE:
  625. a_load_ref_reg(list,loc.size,loc.reference,reg);
  626. LOC_REGISTER,LOC_CREGISTER:
  627. a_load_reg_reg(list,loc.size,loc.size,loc.register,reg);
  628. LOC_CONSTANT:
  629. a_load_const_reg(list,loc.size,loc.value,reg);
  630. else
  631. begin
  632. writeln(ord(loc.loc));
  633. runerror(216);
  634. internalerror(200109092);
  635. end;
  636. end;
  637. end;
  638. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  639. begin
  640. case loc.loc of
  641. LOC_REFERENCE,LOC_CREFERENCE:
  642. a_load_ref_ref(list,loc.size,loc.reference,ref);
  643. LOC_REGISTER,LOC_CREGISTER:
  644. a_load_reg_ref(list,loc.size,loc.register,ref);
  645. LOC_CONSTANT:
  646. a_load_const_ref(list,loc.size,loc.value,ref);
  647. else
  648. internalerror(200109302);
  649. end;
  650. end;
  651. procedure tcg.a_call_loc(list : taasmoutput;const loc:tlocation);
  652. begin
  653. case loc.loc of
  654. LOC_REGISTER,LOC_CREGISTER:
  655. cg.a_call_reg(list,loc.register);
  656. LOC_REFERENCE,LOC_CREFERENCE :
  657. cg.a_call_ref(list,loc.reference);
  658. else
  659. internalerror(200203311);
  660. end;
  661. end;
  662. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  663. var
  664. powerval : longint;
  665. begin
  666. optimize_op_const_reg := false;
  667. case op of
  668. { or with zero returns same result }
  669. OP_OR : if a = 0 then optimize_op_const_reg := true;
  670. { and with max returns same result }
  671. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  672. { division by 1 returns result }
  673. OP_DIV :
  674. begin
  675. if a = 1 then
  676. optimize_op_const_reg := true
  677. else if ispowerof2(int64(a), powerval) then
  678. begin
  679. a := powerval;
  680. op:= OP_SHR;
  681. end;
  682. exit;
  683. end;
  684. OP_IDIV:
  685. begin
  686. if a = 1 then
  687. optimize_op_const_reg := true
  688. else if ispowerof2(int64(a), powerval) then
  689. begin
  690. a := powerval;
  691. op:= OP_SAR;
  692. end;
  693. exit;
  694. end;
  695. OP_MUL,OP_IMUL:
  696. begin
  697. if a = 1 then
  698. optimize_op_const_reg := true
  699. else if ispowerof2(int64(a), powerval) then
  700. begin
  701. a := powerval;
  702. op:= OP_SHL;
  703. end;
  704. exit;
  705. end;
  706. OP_SAR,OP_SHL,OP_SHR:
  707. begin
  708. if a = 0 then
  709. optimize_op_const_reg := true;
  710. exit;
  711. end;
  712. end;
  713. end;
  714. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  715. begin
  716. case loc.loc of
  717. LOC_REFERENCE, LOC_CREFERENCE:
  718. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  719. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  720. a_loadfpu_reg_reg(list,loc.register,reg);
  721. else
  722. runerror(226);
  723. // internalerror(200203301);
  724. end;
  725. end;
  726. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  727. begin
  728. case loc.loc of
  729. LOC_REFERENCE, LOC_CREFERENCE:
  730. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  731. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  732. a_loadfpu_reg_reg(list,reg,loc.register);
  733. else
  734. internalerror(48991);
  735. end;
  736. end;
  737. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  738. var
  739. tmpreg: tregister;
  740. begin
  741. tmpreg := get_scratch_reg_int(list);
  742. a_load_ref_reg(list,size,ref,tmpreg);
  743. a_op_const_reg(list,op,a,tmpreg);
  744. a_load_reg_ref(list,size,tmpreg,ref);
  745. free_scratch_reg(list,tmpreg);
  746. end;
  747. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  748. begin
  749. case loc.loc of
  750. LOC_REGISTER, LOC_CREGISTER:
  751. a_op_const_reg(list,op,a,loc.register);
  752. LOC_REFERENCE, LOC_CREFERENCE:
  753. a_op_const_ref(list,op,loc.size,a,loc.reference);
  754. else
  755. internalerror(200109061);
  756. end;
  757. end;
  758. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  759. var
  760. tmpreg: tregister;
  761. begin
  762. tmpreg := get_scratch_reg_int(list);
  763. a_load_ref_reg(list,size,ref,tmpreg);
  764. a_op_reg_reg(list,op,size,reg,tmpreg);
  765. a_load_reg_ref(list,size,tmpreg,ref);
  766. free_scratch_reg(list,tmpreg);
  767. end;
  768. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  769. var
  770. tmpreg: tregister;
  771. begin
  772. case op of
  773. OP_NOT,OP_NEG:
  774. { handle it as "load ref,reg; op reg" }
  775. begin
  776. a_load_ref_reg(list,size,ref,reg);
  777. a_op_reg_reg(list,op,size,reg,reg);
  778. end;
  779. else
  780. begin
  781. tmpreg := get_scratch_reg_int(list);
  782. a_load_ref_reg(list,size,ref,tmpreg);
  783. a_op_reg_reg(list,op,size,tmpreg,reg);
  784. free_scratch_reg(list,tmpreg);
  785. end;
  786. end;
  787. end;
  788. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  789. begin
  790. case loc.loc of
  791. LOC_REGISTER, LOC_CREGISTER:
  792. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  793. LOC_REFERENCE, LOC_CREFERENCE:
  794. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  795. else
  796. internalerror(200109061);
  797. end;
  798. end;
  799. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  800. var
  801. tmpreg: tregister;
  802. begin
  803. case loc.loc of
  804. LOC_REGISTER,LOC_CREGISTER:
  805. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  806. LOC_REFERENCE,LOC_CREFERENCE:
  807. begin
  808. tmpreg := get_scratch_reg_int(list);
  809. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  810. a_load_ref_reg(list,loc.size,ref,tmpreg);
  811. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  812. free_scratch_reg(list,tmpreg);
  813. end;
  814. else
  815. internalerror(200109061);
  816. end;
  817. end;
  818. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  819. size: tcgsize; a: aword; src, dst: tregister);
  820. begin
  821. a_load_reg_reg(list,size,size,src,dst);
  822. a_op_const_reg(list,op,a,dst);
  823. end;
  824. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  825. size: tcgsize; src1, src2, dst: tregister);
  826. begin
  827. a_load_reg_reg(list,size,size,src2,dst);
  828. a_op_reg_reg(list,op,size,src1,dst);
  829. end;
  830. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  831. l : tasmlabel);
  832. var
  833. tmpreg: tregister;
  834. begin
  835. tmpreg := get_scratch_reg_int(list);
  836. a_load_ref_reg(list,size,ref,tmpreg);
  837. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  838. free_scratch_reg(list,tmpreg);
  839. end;
  840. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  841. l : tasmlabel);
  842. begin
  843. case loc.loc of
  844. LOC_REGISTER,LOC_CREGISTER:
  845. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  846. LOC_REFERENCE,LOC_CREFERENCE:
  847. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  848. else
  849. internalerror(200109061);
  850. end;
  851. end;
  852. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  853. var
  854. tmpreg: tregister;
  855. begin
  856. tmpreg := get_scratch_reg_int(list);
  857. a_load_ref_reg(list,size,ref,tmpreg);
  858. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  859. free_scratch_reg(list,tmpreg);
  860. end;
  861. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  862. begin
  863. case loc.loc of
  864. LOC_REGISTER,
  865. LOC_CREGISTER:
  866. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  867. LOC_REFERENCE,
  868. LOC_CREFERENCE :
  869. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  870. LOC_CONSTANT:
  871. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  872. else
  873. internalerror(200203231);
  874. end;
  875. end;
  876. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  877. l : tasmlabel);
  878. var
  879. tmpreg: tregister;
  880. begin
  881. case loc.loc of
  882. LOC_REGISTER,LOC_CREGISTER:
  883. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  884. LOC_REFERENCE,LOC_CREFERENCE:
  885. begin
  886. {$ifdef i386}
  887. { the following is done with defines to avoid a speed penalty, }
  888. { since all this is only necessary for the 80x86 (because EDI }
  889. { doesn't have an 8bit component which is directly addressable) }
  890. if size in [OS_8,OS_S8] then
  891. tmpreg := rg.getregisterint(list)
  892. else
  893. {$endif i386}
  894. tmpreg := get_scratch_reg_int(list);
  895. tmpreg := rg.makeregsize(tmpreg,size);
  896. a_load_ref_reg(list,size,loc.reference,tmpreg);
  897. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  898. {$ifdef i386}
  899. if size in [OS_8,OS_S8] then
  900. rg.ungetregister(list,tmpreg)
  901. else
  902. {$endif i386}
  903. free_scratch_reg(list,tmpreg);
  904. end
  905. else
  906. internalerror(200109061);
  907. end;
  908. end;
  909. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  910. begin
  911. reg_cgsize := OS_INT;
  912. end;
  913. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  914. begin
  915. {$ifdef FPC}
  916. {$warning FIX ME!}
  917. {$endif}
  918. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  919. if loadref then
  920. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  921. else
  922. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  923. if delsource then
  924. reference_release(list,source);
  925. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  926. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  927. g_maybe_loadself(list);
  928. end;
  929. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  930. var
  931. href : treference;
  932. incrfunc : string;
  933. begin
  934. { These functions should not change the registers (they use
  935. the saveregister proc directive }
  936. if is_interfacecom(t) then
  937. incrfunc:='FPC_INTF_INCR_REF'
  938. else if is_ansistring(t) then
  939. incrfunc:='FPC_ANSISTR_INCR_REF'
  940. else if is_widestring(t) then
  941. incrfunc:='FPC_WIDESTR_INCR_REF'
  942. else if is_dynamic_array(t) then
  943. incrfunc:='FPC_DYNARRAY_INCR_REF'
  944. else
  945. incrfunc:='';
  946. { call the special incr function or the generic addref }
  947. if incrfunc<>'' then
  948. begin
  949. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  950. a_call_name(list,incrfunc);
  951. end
  952. else
  953. begin
  954. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  955. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  956. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  957. a_call_name(list,'FPC_ADDREF');
  958. end;
  959. end;
  960. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  961. var
  962. href : treference;
  963. decrfunc : string;
  964. needrtti : boolean;
  965. begin
  966. needrtti:=false;
  967. if is_interfacecom(t) then
  968. decrfunc:='FPC_INTF_DECR_REF'
  969. else if is_ansistring(t) then
  970. decrfunc:='FPC_ANSISTR_DECR_REF'
  971. else if is_widestring(t) then
  972. decrfunc:='FPC_WIDESTR_DECR_REF'
  973. else if is_dynamic_array(t) then
  974. begin
  975. decrfunc:='FPC_DYNARRAY_DECR_REF';
  976. needrtti:=true;
  977. end
  978. else
  979. decrfunc:='';
  980. { call the special decr function or the generic decref }
  981. if decrfunc<>'' then
  982. begin
  983. if needrtti then
  984. begin
  985. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  986. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  987. end;
  988. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  989. a_call_name(list,decrfunc);
  990. end
  991. else
  992. begin
  993. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  994. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  995. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  996. a_call_name(list,'FPC_DECREF');
  997. end;
  998. end;
  999. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1000. var
  1001. href : treference;
  1002. begin
  1003. if is_ansistring(t) or
  1004. is_widestring(t) or
  1005. is_interfacecom(t) then
  1006. a_load_const_ref(list,OS_ADDR,0,ref)
  1007. else
  1008. begin
  1009. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1010. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1011. if loadref then
  1012. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1013. else
  1014. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1015. a_call_name(list,'FPC_INITIALIZE');
  1016. end;
  1017. end;
  1018. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1019. var
  1020. href : treference;
  1021. begin
  1022. if is_ansistring(t) or
  1023. is_widestring(t) or
  1024. is_interfacecom(t) then
  1025. g_decrrefcount(list,t,ref)
  1026. else
  1027. begin
  1028. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1029. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1030. if loadref then
  1031. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1032. else
  1033. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1034. a_call_name(list,'FPC_FINALIZE');
  1035. end;
  1036. end;
  1037. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1038. { generate range checking code for the value at location p. The type }
  1039. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1040. { is the original type used at that location. When both defs are equal }
  1041. { the check is also insert (needed for succ,pref,inc,dec) }
  1042. const
  1043. {$ifdef ver1_0}
  1044. awordsignedmax=high(longint);
  1045. {$else}
  1046. awordsignedmax=high(aword) div 2;
  1047. {$endif}
  1048. var
  1049. neglabel : tasmlabel;
  1050. hreg : tregister;
  1051. fromdef : tdef;
  1052. lto,hto,
  1053. lfrom,hfrom : TConstExprInt;
  1054. from_signed: boolean;
  1055. begin
  1056. { range checking on and range checkable value? }
  1057. if not(cs_check_range in aktlocalswitches) or
  1058. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1059. exit;
  1060. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  1061. begin
  1062. cg64.g_rangecheck64(list,p,todef);
  1063. exit;
  1064. end;
  1065. { only check when assigning to scalar, subranges are different, }
  1066. { when todef=fromdef then the check is always generated }
  1067. fromdef:=p.resulttype.def;
  1068. getrange(p.resulttype.def,lfrom,hfrom);
  1069. getrange(todef,lto,hto);
  1070. { no range check if from and to are equal and are both longint/dword }
  1071. { (if we have a 32bit processor) or int64/qword, since such }
  1072. { operations can at most cause overflows (JM) }
  1073. { Note that these checks are mostly processor independent, they only }
  1074. { have to be changed once we introduce 64bit subrange types }
  1075. if (fromdef = todef) and
  1076. (fromdef.deftype=orddef) and
  1077. (((sizeof(aword) = 4) and
  1078. (((torddef(fromdef).typ = s32bit) and
  1079. (lfrom = low(longint)) and
  1080. (hfrom = high(longint))) or
  1081. ((torddef(fromdef).typ = u32bit) and
  1082. (lfrom = low(cardinal)) and
  1083. (hfrom = high(cardinal)))))) then
  1084. exit;
  1085. if todef<>fromdef then
  1086. begin
  1087. { if the from-range falls completely in the to-range, no check }
  1088. { is necessary }
  1089. if (lto<=lfrom) and (hto>=hfrom) then
  1090. exit;
  1091. end;
  1092. { generate the rangecheck code for the def where we are going to }
  1093. { store the result }
  1094. { use the trick that }
  1095. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1096. { To be able to do that, we have to make sure however that either }
  1097. { fromdef and todef are both signed or unsigned, or that we leave }
  1098. { the parts < 0 and > maxlongint out }
  1099. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1100. from_signed := is_signed(fromdef);
  1101. if from_signed xor is_signed(todef) then
  1102. if from_signed then
  1103. { from is signed, to is unsigned }
  1104. begin
  1105. { if high(from) < 0 -> always range error }
  1106. if (hfrom < 0) or
  1107. { if low(to) > maxlongint also range error }
  1108. (lto > awordsignedmax) then
  1109. begin
  1110. a_call_name(list,'FPC_RANGEERROR');
  1111. exit
  1112. end;
  1113. { from is signed and to is unsigned -> when looking at from }
  1114. { as an unsigned value, it must be < maxlongint (otherwise }
  1115. { it's negative, which is invalid since "to" is unsigned) }
  1116. if hto > awordsignedmax then
  1117. hto := awordsignedmax;
  1118. end
  1119. else
  1120. { from is unsigned, to is signed }
  1121. begin
  1122. if (lfrom > awordsignedmax) or
  1123. (hto < 0) then
  1124. begin
  1125. a_call_name(list,'FPC_RANGEERROR');
  1126. exit
  1127. end;
  1128. { from is unsigned and to is signed -> when looking at to }
  1129. { as an unsigned value, it must be >= 0 (since negative }
  1130. { values are the same as values > maxlongint) }
  1131. if lto < 0 then
  1132. lto := 0;
  1133. end;
  1134. hreg := get_scratch_reg_int(list);
  1135. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1136. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1137. aword(lto),p.location.register,hreg)
  1138. else
  1139. begin
  1140. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1141. p.location.reference,hreg);
  1142. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1143. end;
  1144. objectlibrary.getlabel(neglabel);
  1145. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1146. { !!! should happen right after the compare (JM) }
  1147. free_scratch_reg(list,hreg);
  1148. a_call_name(list,'FPC_RANGEERROR');
  1149. a_label(list,neglabel);
  1150. end;
  1151. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1152. begin
  1153. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1154. a_call_name(list,'FPC_STACKCHECK');
  1155. end;
  1156. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1157. var
  1158. tmpreg : tregister;
  1159. begin
  1160. tmpreg := get_scratch_reg_int(list);
  1161. g_flags2reg(list,size,f,tmpreg);
  1162. a_load_reg_ref(list,size,tmpreg,ref);
  1163. free_scratch_reg(list,tmpreg);
  1164. end;
  1165. procedure tcg.g_maybe_loadself(list : taasmoutput);
  1166. var
  1167. hp : treference;
  1168. p : tprocinfo;
  1169. i : longint;
  1170. begin
  1171. if assigned(procinfo._class) then
  1172. begin
  1173. list.concat(tai_regalloc.Alloc(SELF_POINTER_REG));
  1174. if lexlevel>normal_function_level then
  1175. begin
  1176. reference_reset_base(hp,procinfo.framepointer,procinfo.framepointer_offset);
  1177. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1178. p:=procinfo.parent;
  1179. for i:=3 to lexlevel-1 do
  1180. begin
  1181. reference_reset_base(hp,SELF_POINTER_REG,p.framepointer_offset);
  1182. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1183. p:=p.parent;
  1184. end;
  1185. reference_reset_base(hp,SELF_POINTER_REG,p.selfpointer_offset);
  1186. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1187. end
  1188. else
  1189. begin
  1190. reference_reset_base(hp,procinfo.framepointer,procinfo.selfpointer_offset);
  1191. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1192. end;
  1193. end;
  1194. end;
  1195. procedure tcg.g_maybe_testself(list : taasmoutput);
  1196. var
  1197. OKLabel : tasmlabel;
  1198. dummyloc : tparalocation;
  1199. begin
  1200. if (cs_check_object in aktlocalswitches) or
  1201. (cs_check_range in aktlocalswitches) then
  1202. begin
  1203. objectlibrary.getlabel(oklabel);
  1204. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,SELF_POINTER_REG,oklabel);
  1205. a_param_const(list,OS_INT,210,dummyloc);
  1206. a_call_name(list,'FPC_HANDLEERROR');
  1207. a_label(list,oklabel);
  1208. end;
  1209. end;
  1210. {*****************************************************************************
  1211. Entry/Exit Code Functions
  1212. *****************************************************************************}
  1213. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1214. var
  1215. href : treference;
  1216. hregister : tregister;
  1217. begin
  1218. if is_class(procinfo._class) then
  1219. begin
  1220. if (cs_implicit_exceptions in aktmoduleswitches) then
  1221. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1222. { parameter 2 : self pointer / flag }
  1223. {!! this is a terrible hack, normally the helper should get three params : }
  1224. { one with self register, one with flag and one with VMT pointer }
  1225. {reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);}
  1226. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, paramanager.getintparaloc(2));
  1227. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1228. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1229. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1230. a_call_name(list,'FPC_NEW_CLASS');
  1231. a_load_reg_reg(list,OS_ADDR,OS_ADDR,accumulator,SELF_POINTER_REG);
  1232. { save the self pointer result }
  1233. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1234. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1235. end
  1236. else if is_object(procinfo._class) then
  1237. begin
  1238. { parameter 3 :vmt_offset }
  1239. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1240. { parameter 2 : address of pointer to vmt }
  1241. { this is the first(?) parameter which was pushed to the constructor }
  1242. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1243. hregister:=get_scratch_reg_address(list);
  1244. a_loadaddr_ref_reg(list, href, hregister);
  1245. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1246. free_scratch_reg(list, hregister);
  1247. { parameter 1 : address of self pointer }
  1248. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1249. hregister:=get_scratch_reg_address(list);
  1250. a_loadaddr_ref_reg(list, href, hregister);
  1251. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1252. free_scratch_reg(list, hregister);
  1253. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1254. a_load_reg_reg(list,OS_ADDR,OS_ADDR,accumulator,SELF_POINTER_REG);
  1255. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1256. end
  1257. else
  1258. internalerror(200006161);
  1259. end;
  1260. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1261. var
  1262. nofinal : tasmlabel;
  1263. href : treference;
  1264. hregister : tregister;
  1265. begin
  1266. if is_class(procinfo._class) then
  1267. begin
  1268. { 2nd parameter : flag }
  1269. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);
  1270. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1271. { 1st parameter to destructor : self }
  1272. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1273. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1274. a_call_name(list,'FPC_DISPOSE_CLASS')
  1275. end
  1276. else if is_object(procinfo._class) then
  1277. begin
  1278. { must the object be finalized ? }
  1279. if procinfo._class.needs_inittable then
  1280. begin
  1281. objectlibrary.getlabel(nofinal);
  1282. reference_reset_base(href,procinfo.framepointer,target_info.first_parm_offset);
  1283. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1284. reference_reset_base(href,SELF_POINTER_REG,0);
  1285. g_finalize(list,procinfo._class,href,false);
  1286. a_label(list,nofinal);
  1287. end;
  1288. { actually call destructor }
  1289. { parameter 3 :vmt_offset }
  1290. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1291. { parameter 2 : pointer to vmt }
  1292. { this is the first parameter which was pushed to the destructor }
  1293. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1294. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1295. { parameter 1 : address of self pointer }
  1296. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1297. hregister:=get_scratch_reg_address(list);
  1298. a_loadaddr_ref_reg(list, href, hregister);
  1299. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1300. free_scratch_reg(list, hregister);
  1301. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1302. end
  1303. else
  1304. internalerror(200006162);
  1305. end;
  1306. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1307. var
  1308. href : treference;
  1309. hregister : tregister;
  1310. begin
  1311. if is_class(procinfo._class) then
  1312. begin
  1313. {
  1314. Dispose of the class then set self_pointer to nil
  1315. both in stack and in self register.
  1316. }
  1317. { 2nd parameter : flag }
  1318. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1319. { 1st parameter to destructor : self }
  1320. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1321. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1322. a_call_name(list,'FPC_DISPOSE_CLASS');
  1323. { SET SELF TO NIL }
  1324. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1325. { set the self pointer in the stack to nil }
  1326. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1327. end
  1328. else if is_object(procinfo._class) then
  1329. begin
  1330. { parameter 3 :vmt_offset }
  1331. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1332. { parameter 2 : address of pointer to vmt }
  1333. { this is the first(?) parameter which was pushed to the constructor }
  1334. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1335. hregister:=get_scratch_reg_address(list);
  1336. a_loadaddr_ref_reg(list, href, hregister);
  1337. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1338. free_scratch_reg(list, hregister);
  1339. { parameter 1 : address of self pointer }
  1340. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1341. hregister:=get_scratch_reg_address(list);
  1342. a_loadaddr_ref_reg(list, href, hregister);
  1343. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1344. free_scratch_reg(list, hregister);
  1345. a_call_name(list,'FPC_HELP_FAIL');
  1346. { SET SELF TO NIL }
  1347. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1348. end
  1349. else
  1350. internalerror(200006163);
  1351. end;
  1352. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1353. begin
  1354. end;
  1355. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1356. begin
  1357. end;
  1358. procedure tcg.g_profilecode(list : taasmoutput);
  1359. begin
  1360. end;
  1361. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1362. begin
  1363. a_load_reg_ref(list, OS_S32, accumulator, href);
  1364. end;
  1365. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1366. begin
  1367. a_load_const_ref(list, OS_S32, a, href);
  1368. end;
  1369. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1370. begin
  1371. a_load_ref_reg(list, OS_S32, href, accumulator);
  1372. end;
  1373. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1374. regsrc,regdst : tregister64);
  1375. begin
  1376. a_load64_reg_reg(list,regsrc,regdst);
  1377. a_op64_const_reg(list,op,value,regdst);
  1378. end;
  1379. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1380. begin
  1381. a_load64_reg_reg(list,regsrc2,regdst);
  1382. a_op64_reg_reg(list,op,regsrc1,regdst);
  1383. end;
  1384. initialization
  1385. ;
  1386. finalization
  1387. cg.free;
  1388. cg64.free;
  1389. end.
  1390. {
  1391. $Log$
  1392. Revision 1.66 2002-11-25 17:43:16 peter
  1393. * splitted defbase in defutil,symutil,defcmp
  1394. * merged isconvertable and is_equal into compare_defs(_ext)
  1395. * made operator search faster by walking the list only once
  1396. Revision 1.65 2002/11/17 16:27:31 carl
  1397. * document flags2reg
  1398. Revision 1.64 2002/11/16 17:06:28 peter
  1399. * return error 210 for failed self test
  1400. Revision 1.63 2002/11/15 01:58:46 peter
  1401. * merged changes from 1.0.7 up to 04-11
  1402. - -V option for generating bug report tracing
  1403. - more tracing for option parsing
  1404. - errors for cdecl and high()
  1405. - win32 import stabs
  1406. - win32 records<=8 are returned in eax:edx (turned off by default)
  1407. - heaptrc update
  1408. - more info for temp management in .s file with EXTDEBUG
  1409. Revision 1.62 2002/10/16 19:01:43 peter
  1410. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1411. implicit exception frames for procedures with initialized variables
  1412. and for constructors. The default is on for compatibility
  1413. Revision 1.61 2002/10/05 12:43:23 carl
  1414. * fixes for Delphi 6 compilation
  1415. (warning : Some features do not work under Delphi)
  1416. Revision 1.60 2002/10/02 18:20:52 peter
  1417. * Copy() is now internal syssym that calls compilerprocs
  1418. Revision 1.59 2002/09/17 18:54:02 jonas
  1419. * a_load_reg_reg() now has two size parameters: source and dest. This
  1420. allows some optimizations on architectures that don't encode the
  1421. register size in the register name.
  1422. Revision 1.58 2002/09/09 19:29:29 peter
  1423. * fixed dynarr_decr_ref call
  1424. Revision 1.57 2002/09/07 15:25:01 peter
  1425. * old logs removed and tabs fixed
  1426. Revision 1.56 2002/09/01 21:04:47 florian
  1427. * several powerpc related stuff fixed
  1428. Revision 1.55 2002/09/01 17:05:43 florian
  1429. + added abstract tcg.g_removevaluepara_openarray
  1430. Revision 1.54 2002/09/01 12:09:27 peter
  1431. + a_call_reg, a_call_loc added
  1432. * removed exprasmlist references
  1433. Revision 1.53 2002/08/19 18:17:48 carl
  1434. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1435. * more fixes to m68k for 64-bit operations
  1436. Revision 1.52 2002/08/17 22:09:43 florian
  1437. * result type handling in tcgcal.pass_2 overhauled
  1438. * better tnode.dowrite
  1439. * some ppc stuff fixed
  1440. Revision 1.51 2002/08/17 09:23:33 florian
  1441. * first part of procinfo rewrite
  1442. Revision 1.50 2002/08/16 14:24:57 carl
  1443. * issameref() to test if two references are the same (then emit no opcodes)
  1444. + ret_in_reg to replace ret_in_acc
  1445. (fix some register allocation bugs at the same time)
  1446. + save_std_register now has an extra parameter which is the
  1447. usedinproc registers
  1448. Revision 1.49 2002/08/15 08:13:54 carl
  1449. - a_load_sym_ofs_reg removed
  1450. * loadvmt now calls loadaddr_ref_reg instead
  1451. Revision 1.48 2002/08/14 19:26:02 carl
  1452. + routine to optimize opcodes with constants
  1453. Revision 1.47 2002/08/11 14:32:26 peter
  1454. * renamed current_library to objectlibrary
  1455. Revision 1.46 2002/08/11 13:24:11 peter
  1456. * saving of asmsymbols in ppu supported
  1457. * asmsymbollist global is removed and moved into a new class
  1458. tasmlibrarydata that will hold the info of a .a file which
  1459. corresponds with a single module. Added librarydata to tmodule
  1460. to keep the library info stored for the module. In the future the
  1461. objectfiles will also be stored to the tasmlibrarydata class
  1462. * all getlabel/newasmsymbol and friends are moved to the new class
  1463. Revision 1.45 2002/08/10 17:15:20 jonas
  1464. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1465. Revision 1.44 2002/08/09 19:10:05 carl
  1466. - moved new_exception and free_exception to ncgutils
  1467. Revision 1.43 2002/08/05 18:27:48 carl
  1468. + more more more documentation
  1469. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1470. Revision 1.42 2002/08/04 19:08:21 carl
  1471. + added generic exception support (still does not work!)
  1472. + more documentation
  1473. Revision 1.41 2002/07/30 20:50:43 florian
  1474. * the code generator knows now if parameters are in registers
  1475. Revision 1.40 2002/07/29 21:16:02 florian
  1476. * some more ppc fixes
  1477. Revision 1.39 2002/07/28 15:56:00 jonas
  1478. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1479. generic implementation
  1480. Revision 1.38 2002/07/27 19:53:51 jonas
  1481. + generic implementation of tcg.g_flags2ref()
  1482. * tcg.flags2xxx() now also needs a size parameter
  1483. Revision 1.37 2002/07/20 11:57:53 florian
  1484. * types.pas renamed to defbase.pas because D6 contains a types
  1485. unit so this would conflicts if D6 programms are compiled
  1486. + Willamette/SSE2 instructions to assembler added
  1487. }