cgobj.pas 80 KB

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