cgobj.pas 76 KB

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