cgobj.pas 81 KB

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