cgobj.pas 79 KB

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