cgobj.pas 71 KB

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