cgobj.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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. {$ifdef delphi}
  32. dmisc,
  33. {$endif}
  34. cclasses,globtype,
  35. cpubase,cpuinfo,cgbase,parabase,
  36. aasmbase,aasmtai,aasmcpu,
  37. symconst,symbase,symtype,symdef,symtable,rgobj
  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. public
  50. alignment : talignment;
  51. rg : array[tregistertype] of trgobj;
  52. t_times:cardinal;
  53. {$ifdef flowgraph}
  54. aktflownode:word;
  55. {$endif}
  56. {************************************************}
  57. { basic routines }
  58. constructor create;
  59. {# Initialize the register allocators needed for the codegenerator.}
  60. procedure init_register_allocators;virtual;
  61. {# Clean up the register allocators needed for the codegenerator.}
  62. procedure done_register_allocators;virtual;
  63. {$ifdef flowgraph}
  64. procedure init_flowgraph;
  65. procedure done_flowgraph;
  66. {$endif}
  67. {# Gets a register suitable to do integer operations on.}
  68. function getintregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  69. {# Gets a register suitable to do integer operations on.}
  70. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  71. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  72. function getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  73. function getflagregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  74. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  75. the cpu specific child cg object have such a method?}
  76. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  77. procedure add_move_instruction(instr:Taicpu);virtual;
  78. function uses_registers(rt:Tregistertype):boolean;virtual;
  79. {# Get a specific register.}
  80. procedure getcpuregister(list:Taasmoutput;r:Tregister);virtual;
  81. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);virtual;
  82. {# Get multiple registers specified.}
  83. procedure alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);virtual;
  84. {# Free multiple registers specified.}
  85. procedure dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);virtual;
  86. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  87. function makeregsize(list:Taasmoutput;reg:Tregister;size:Tcgsize):Tregister;
  88. {# Emit a label to the instruction stream. }
  89. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  90. {# Allocates register r by inserting a pai_realloc record }
  91. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  92. {# Deallocates register r by inserting a pa_regdealloc record}
  93. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  94. { Synchronize register, make sure it is still valid }
  95. procedure a_reg_sync(list : taasmoutput;r : tregister);
  96. {# Pass a parameter, which is located in a register, to a routine.
  97. This routine should push/send the parameter to the routine, as
  98. required by the specific processor ABI and routine modifiers.
  99. This must be overriden for each CPU target.
  100. @param(size size of the operand in the register)
  101. @param(r register source of the operand)
  102. @param(paraloc where the parameter will be stored)
  103. }
  104. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const paraloc : TCGPara);virtual;
  105. {# Pass a parameter, which is a constant, to a routine.
  106. A generic version is provided. This routine should
  107. be overriden for optimization purposes if the cpu
  108. permits directly sending this type of parameter.
  109. @param(size size of the operand in constant)
  110. @param(a value of constant to send)
  111. @param(paraloc where the parameter will be stored)
  112. }
  113. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : TCGPara);virtual;
  114. {# Pass the value of a parameter, which is located in memory, to a routine.
  115. A generic version is provided. This routine should
  116. be overriden for optimization purposes if the cpu
  117. permits directly sending this type of parameter.
  118. @param(size size of the operand in constant)
  119. @param(r Memory reference of value to send)
  120. @param(paraloc where the parameter will be stored)
  121. }
  122. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : TCGPara);virtual;
  123. {# Pass the value of a parameter, which can be located either in a register or memory location,
  124. to a routine.
  125. A generic version is provided.
  126. @param(l location of the operand to send)
  127. @param(nr parameter number (starting from one) of routine (from left to right))
  128. @param(paraloc where the parameter will be stored)
  129. }
  130. procedure a_param_loc(list : taasmoutput;const l : tlocation;const paraloc : TCGPara);
  131. {# Pass the address of a reference to a routine. This routine
  132. will calculate the address of the reference, and pass this
  133. calculated address as a parameter.
  134. A generic version is provided. This routine should
  135. be overriden for optimization purposes if the cpu
  136. permits directly sending this type of parameter.
  137. @param(r reference to get address from)
  138. @param(nr parameter number (starting from one) of routine (from left to right))
  139. }
  140. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : TCGPara);virtual;
  141. { Copies a whole memory block to the stack, the paraloc must be a memory location }
  142. procedure a_param_copy_ref(list : taasmoutput;size : aint;const r : treference;const paraloc : TCGPara);
  143. { Remarks:
  144. * If a method specifies a size you have only to take care
  145. of that number of bits, i.e. load_const_reg with OP_8 must
  146. only load the lower 8 bit of the specified register
  147. the rest of the register can be undefined
  148. if necessary the compiler will call a method
  149. to zero or sign extend the register
  150. * The a_load_XX_XX with OP_64 needn't to be
  151. implemented for 32 bit
  152. processors, the code generator takes care of that
  153. * the addr size is for work with the natural pointer
  154. size
  155. * the procedures without fpu/mm are only for integer usage
  156. * normally the first location is the source and the
  157. second the destination
  158. }
  159. {# Emits instruction to call the method specified by symbol name.
  160. This routine must be overriden for each new target cpu.
  161. There is no a_call_ref because loading the reference will use
  162. a temp register on most cpu's resulting in conflicts with the
  163. registers used for the parameters (PFV)
  164. }
  165. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  166. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  167. { move instructions }
  168. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aint;register : tregister);virtual; abstract;
  169. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);virtual;
  170. procedure a_load_const_loc(list : taasmoutput;a : aint;const loc : tlocation);
  171. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  172. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  173. procedure a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  174. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  175. procedure a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  176. procedure a_load_loc_reg(list : taasmoutput;tosize: tcgsize; const loc: tlocation; reg : tregister);
  177. procedure a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  178. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  179. { fpu move instructions }
  180. procedure a_loadfpu_reg_reg(list: taasmoutput; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  181. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  182. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  183. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  184. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  185. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const paraloc : TCGPara);virtual;
  186. procedure a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const paraloc : TCGPara);virtual;
  187. { vector register move instructions }
  188. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  189. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  190. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  191. procedure a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  192. procedure a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  193. procedure a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const paraloc : TCGPara;shuffle : pmmshuffle); virtual;
  194. procedure a_parammm_ref(list: taasmoutput; size: tcgsize; const ref: treference;const paraloc : TCGPara;shuffle : pmmshuffle); virtual;
  195. procedure a_parammm_loc(list: taasmoutput; const loc: tlocation; const paraloc : TCGPara;shuffle : pmmshuffle); virtual;
  196. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  197. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  198. procedure a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  199. procedure a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  200. { basic arithmetic operations }
  201. { note: for operators which require only one argument (not, neg), use }
  202. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  203. { that in this case the *second* operand is used as both source and }
  204. { destination (JM) }
  205. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; reg: TRegister); virtual; abstract;
  206. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; const ref: TReference); virtual;
  207. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: Aint; const loc: tlocation);
  208. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  209. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  210. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  211. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  212. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  213. { trinary operations for processors that support them, 'emulated' }
  214. { on others. None with "ref" arguments since I don't think there }
  215. { are any processors that support it (JM) }
  216. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); virtual;
  217. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  218. procedure a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  219. procedure a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  220. { comparison operations }
  221. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  222. l : tasmlabel);virtual; abstract;
  223. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  224. l : tasmlabel); virtual;
  225. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aint; const loc: tlocation;
  226. l : tasmlabel);
  227. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  228. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  229. procedure a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  230. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  231. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  232. l : tasmlabel);
  233. procedure a_jmp_name(list : taasmoutput;const s : string); virtual; abstract;
  234. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  235. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  236. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  237. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  238. }
  239. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  240. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  241. {
  242. This routine tries to optimize the const_reg opcode, and should be
  243. called at the start of a_op_const_reg. It returns the actual opcode
  244. to emit, and the constant value to emit. If this routine returns
  245. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  246. @param(op The opcode to emit, returns the opcode which must be emitted)
  247. @param(a The constant which should be emitted, returns the constant which must
  248. be emitted)
  249. @param(reg The register to emit the opcode with, returns the register with
  250. which the opcode will be emitted)
  251. }
  252. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg: tregister): boolean;virtual;
  253. {#
  254. This routine is used in exception management nodes. It should
  255. save the exception reason currently in the FUNCTION_RETURN_REG. The
  256. save should be done either to a temp (pointed to by href).
  257. or on the stack (pushing the value on the stack).
  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_save(list : taasmoutput; const href : treference);virtual;
  262. {#
  263. This routine is used in exception management nodes. It should
  264. save the exception reason constant. The
  265. save should be done either to a temp (pointed to by href).
  266. or on the stack (pushing the value on the stack).
  267. The size of the value to save is OS_S32. The default version
  268. saves the exception reason to a temp. memory area.
  269. }
  270. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);virtual;
  271. {#
  272. This routine is used in exception management nodes. It should
  273. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  274. should either be in the temp. area (pointed to by href , href should
  275. *NOT* be freed) or on the stack (the value should be popped).
  276. The size of the value to save is OS_S32. The default version
  277. saves the exception reason to a temp. memory area.
  278. }
  279. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  280. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  281. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  282. {# This should emit the opcode to copy len bytes from the source
  283. to destination, if loadref is true, it assumes that it first must load
  284. the source address from the memory location where
  285. source points to.
  286. It must be overriden for each new target processor.
  287. @param(source Source reference of copy)
  288. @param(dest Destination reference of copy)
  289. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  290. }
  291. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint;loadref : boolean);virtual; abstract;
  292. {# This should emit the opcode to copy len bytes from the an unaligned source
  293. to destination, if loadref is true, it assumes that it first must load
  294. the source address from the memory location where
  295. source points to.
  296. It must be overriden for each new target processor.
  297. @param(source Source reference of copy)
  298. @param(dest Destination reference of copy)
  299. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  300. }
  301. procedure g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint;loadref : boolean);virtual;
  302. {# This should emit the opcode to a shortrstring from the source
  303. to destination, if loadref is true, it assumes that it first must load
  304. the source address from the memory location where
  305. source points to.
  306. @param(source Source reference of copy)
  307. @param(dest Destination reference of copy)
  308. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  309. }
  310. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;loadref : boolean);
  311. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  312. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  313. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  314. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  315. {# Generates range checking code. It is to note
  316. that this routine does not need to be overriden,
  317. as it takes care of everything.
  318. @param(p Node which contains the value to check)
  319. @param(todef Type definition of node to range check)
  320. }
  321. procedure g_rangecheck(list: taasmoutput; const l:tlocation; fromdef,todef: tdef); virtual;
  322. {# Generates overflow checking code for a node }
  323. procedure g_overflowcheck(list: taasmoutput; const Loc:tlocation; def:tdef); virtual;abstract;
  324. procedure g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  325. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;loadref:boolean);virtual;
  326. procedure g_releasevaluepara_openarray(list : taasmoutput;const ref:treference);virtual;
  327. {# Emits instructions when compilation is done in profile
  328. mode (this is set as a command line option). The default
  329. behavior does nothing, should be overriden as required.
  330. }
  331. procedure g_profilecode(list : taasmoutput);virtual;
  332. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  333. @param(size Number of bytes to allocate)
  334. }
  335. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  336. {# Emits instruction for allocating the locals in entry
  337. code of a routine. This is one of the first
  338. routine called in @var(genentrycode).
  339. @param(localsize Number of bytes to allocate as locals)
  340. }
  341. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);virtual; abstract;
  342. {# Emits instructions for returning from a subroutine.
  343. Should also restore the framepointer and stack.
  344. @param(parasize Number of bytes of parameters to deallocate from stack)
  345. }
  346. procedure g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);virtual;abstract;
  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);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);virtual;abstract;
  363. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  364. procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:TCGPara);virtual;abstract;
  365. end;
  366. {$ifndef cpu64bit}
  367. {# @abstract(Abstract code generator for 64 Bit operations)
  368. This class implements an abstract code generator class
  369. for 64 Bit operations.
  370. }
  371. tcg64 = class
  372. procedure a_load64_const_ref(list : taasmoutput;value : int64;const ref : treference);virtual;abstract;
  373. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  374. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  375. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  376. procedure a_load64_const_reg(list : taasmoutput;value : int64;reg : tregister64);virtual;abstract;
  377. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  378. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  379. procedure a_load64_const_loc(list : taasmoutput;value : int64;const l : tlocation);virtual;abstract;
  380. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  381. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  382. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  383. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  384. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  385. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  386. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  387. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  388. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  389. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  390. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;regdst : tregister64);virtual;abstract;
  391. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : int64;const ref : treference);virtual;abstract;
  392. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : int64;const l: tlocation);virtual;abstract;
  393. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  394. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  395. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);virtual;
  396. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  397. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  398. procedure a_param64_const(list : taasmoutput;value : int64;const loc : TCGPara);virtual;abstract;
  399. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : TCGPara);virtual;abstract;
  400. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : TCGPara);virtual;abstract;
  401. {
  402. This routine tries to optimize the const_reg opcode, and should be
  403. called at the start of a_op64_const_reg. It returns the actual opcode
  404. to emit, and the constant value to emit. If this routine returns
  405. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  406. @param(op The opcode to emit, returns the opcode which must be emitted)
  407. @param(a The constant which should be emitted, returns the constant which must
  408. be emitted)
  409. @param(reg The register to emit the opcode with, returns the register with
  410. which the opcode will be emitted)
  411. }
  412. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  413. { override to catch 64bit rangechecks }
  414. procedure g_rangecheck64(list: taasmoutput; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  415. end;
  416. {$endif cpu64bit}
  417. { tlocation handling }
  418. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  419. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  420. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  421. procedure location_swap(var destloc,sourceloc : tlocation);
  422. var
  423. {# Main code generator class }
  424. cg : tcg;
  425. {$ifndef cpu64bit}
  426. {# Code generator class for all operations working with 64-Bit operands }
  427. cg64 : tcg64;
  428. {$endif cpu64bit}
  429. implementation
  430. uses
  431. globals,options,systems,
  432. verbose,defutil,paramgr,
  433. tgobj,cutils,
  434. cgutils;
  435. const
  436. { Please leave this here, this module should NOT use
  437. exprasmlist, the lists are always passed as arguments.
  438. Declaring it as string here results in an error when compiling (PFV) }
  439. exprasmlist = 'error';
  440. {*****************************************************************************
  441. basic functionallity
  442. ******************************************************************************}
  443. constructor tcg.create;
  444. begin
  445. end;
  446. {*****************************************************************************
  447. register allocation
  448. ******************************************************************************}
  449. procedure tcg.init_register_allocators;
  450. begin
  451. fillchar(rg,sizeof(rg),0);
  452. add_reg_instruction_hook:={$ifdef FPCPROCVAR}@{$endif}add_reg_instruction;
  453. end;
  454. procedure tcg.done_register_allocators;
  455. begin
  456. { Safety }
  457. fillchar(rg,sizeof(rg),0);
  458. add_reg_instruction_hook:=nil;
  459. end;
  460. {$ifdef flowgraph}
  461. procedure Tcg.init_flowgraph;
  462. begin
  463. aktflownode:=0;
  464. end;
  465. procedure Tcg.done_flowgraph;
  466. begin
  467. end;
  468. {$endif}
  469. function tcg.getintregister(list:Taasmoutput;size:Tcgsize):Tregister;
  470. begin
  471. if not assigned(rg[R_INTREGISTER]) then
  472. internalerror(200312122);
  473. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  474. end;
  475. function tcg.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  476. begin
  477. if not assigned(rg[R_FPUREGISTER]) then
  478. internalerror(200312123);
  479. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  480. end;
  481. function tcg.getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;
  482. begin
  483. if not assigned(rg[R_MMREGISTER]) then
  484. internalerror(200312124);
  485. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  486. end;
  487. function tcg.getaddressregister(list:Taasmoutput):Tregister;
  488. begin
  489. if assigned(rg[R_ADDRESSREGISTER]) then
  490. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  491. else
  492. begin
  493. if not assigned(rg[R_INTREGISTER]) then
  494. internalerror(200312121);
  495. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  496. end;
  497. end;
  498. function Tcg.makeregsize(list:Taasmoutput;reg:Tregister;size:Tcgsize):Tregister;
  499. var
  500. subreg:Tsubregister;
  501. begin
  502. subreg:=cgsize2subreg(size);
  503. result:=reg;
  504. setsubreg(result,subreg);
  505. { notify RA }
  506. if result<>reg then
  507. list.concat(tai_regalloc.resize(result));
  508. end;
  509. procedure tcg.getcpuregister(list:Taasmoutput;r:Tregister);
  510. begin
  511. if not assigned(rg[getregtype(r)]) then
  512. internalerror(200312125);
  513. rg[getregtype(r)].getcpuregister(list,r);
  514. end;
  515. procedure tcg.ungetcpuregister(list:Taasmoutput;r:Tregister);
  516. begin
  517. if not assigned(rg[getregtype(r)]) then
  518. internalerror(200312126);
  519. rg[getregtype(r)].ungetcpuregister(list,r);
  520. end;
  521. procedure tcg.alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  522. begin
  523. if assigned(rg[rt]) then
  524. rg[rt].alloccpuregisters(list,r)
  525. else
  526. internalerror(200310092);
  527. end;
  528. procedure tcg.dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  529. begin
  530. if assigned(rg[rt]) then
  531. rg[rt].dealloccpuregisters(list,r)
  532. else
  533. internalerror(200310093);
  534. end;
  535. function tcg.uses_registers(rt:Tregistertype):boolean;
  536. begin
  537. if assigned(rg[rt]) then
  538. result:=rg[rt].uses_registers
  539. else
  540. result:=false;
  541. end;
  542. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  543. var
  544. rt : tregistertype;
  545. begin
  546. rt:=getregtype(r);
  547. { Only add it when a register allocator is configured.
  548. No IE can be generated, because the VMT is written
  549. without a valid rg[] }
  550. if assigned(rg[rt]) then
  551. rg[rt].add_reg_instruction(instr,r);
  552. end;
  553. procedure tcg.add_move_instruction(instr:Taicpu);
  554. var
  555. rt : tregistertype;
  556. begin
  557. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  558. if assigned(rg[rt]) then
  559. rg[rt].add_move_instruction(instr)
  560. else
  561. internalerror(200310095);
  562. end;
  563. procedure tcg.do_register_allocation(list:Taasmoutput;headertai:tai);
  564. var
  565. rt : tregistertype;
  566. begin
  567. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  568. begin
  569. if assigned(rg[rt]) then
  570. rg[rt].do_register_allocation(list,headertai);
  571. end;
  572. { running the other register allocator passes could require addition int/addr. registers
  573. when spilling so run int/addr register allocation at the end }
  574. if assigned(rg[R_INTREGISTER]) then
  575. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  576. if assigned(rg[R_ADDRESSREGISTER]) then
  577. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  578. end;
  579. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  580. begin
  581. list.concat(tai_regalloc.alloc(r,nil));
  582. end;
  583. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  584. begin
  585. list.concat(tai_regalloc.dealloc(r,nil));
  586. end;
  587. procedure tcg.a_reg_sync(list : taasmoutput;r : tregister);
  588. var
  589. instr : tai;
  590. begin
  591. instr:=tai_regalloc.sync(r);
  592. list.concat(instr);
  593. add_reg_instruction(instr,r);
  594. end;
  595. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  596. begin
  597. list.concat(tai_label.create(l));
  598. end;
  599. {*****************************************************************************
  600. for better code generation these methods should be overridden
  601. ******************************************************************************}
  602. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const paraloc : TCGPara);
  603. var
  604. ref : treference;
  605. begin
  606. paraloc.check_simple_location;
  607. case paraloc.location^.loc of
  608. LOC_REGISTER,LOC_CREGISTER:
  609. a_load_reg_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  610. LOC_REFERENCE,LOC_CREFERENCE:
  611. begin
  612. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  613. a_load_reg_ref(list,size,paraloc.location^.size,r,ref);
  614. end
  615. else
  616. internalerror(2002071004);
  617. end;
  618. end;
  619. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : TCGPara);
  620. var
  621. ref : treference;
  622. begin
  623. paraloc.check_simple_location;
  624. case paraloc.location^.loc of
  625. LOC_REGISTER,LOC_CREGISTER:
  626. a_load_const_reg(list,paraloc.location^.size,a,paraloc.location^.register);
  627. LOC_REFERENCE,LOC_CREFERENCE:
  628. begin
  629. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  630. a_load_const_ref(list,paraloc.location^.size,a,ref);
  631. end
  632. else
  633. internalerror(2002071004);
  634. end;
  635. end;
  636. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : TCGPara);
  637. var
  638. ref : treference;
  639. begin
  640. paraloc.check_simple_location;
  641. case paraloc.location^.loc of
  642. LOC_REGISTER,LOC_CREGISTER:
  643. a_load_ref_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  644. LOC_REFERENCE,LOC_CREFERENCE:
  645. begin
  646. reference_reset(ref);
  647. ref.base:=paraloc.location^.reference.index;
  648. ref.offset:=paraloc.location^.reference.offset;
  649. { use concatcopy, because it can also be a float which fails when
  650. load_ref_ref is used }
  651. g_concatcopy(list,r,ref,tcgsize2size[size],false);
  652. end
  653. else
  654. internalerror(2002071004);
  655. end;
  656. end;
  657. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const paraloc : TCGPara);
  658. begin
  659. case l.loc of
  660. LOC_REGISTER,
  661. LOC_CREGISTER :
  662. a_param_reg(list,l.size,l.register,paraloc);
  663. LOC_CONSTANT :
  664. a_param_const(list,l.size,l.value,paraloc);
  665. LOC_CREFERENCE,
  666. LOC_REFERENCE :
  667. a_param_ref(list,l.size,l.reference,paraloc);
  668. else
  669. internalerror(2002032211);
  670. end;
  671. end;
  672. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : TCGPara);
  673. var
  674. hr : tregister;
  675. begin
  676. hr:=getaddressregister(list);
  677. a_loadaddr_ref_reg(list,r,hr);
  678. a_param_reg(list,OS_ADDR,hr,paraloc);
  679. end;
  680. procedure tcg.a_param_copy_ref(list : taasmoutput;size : aint;const r : treference;const paraloc : TCGPara);
  681. var
  682. ref : treference;
  683. begin
  684. paraloc.check_simple_location;
  685. if not(paraloc.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  686. internalerror(2003010901);
  687. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  688. g_concatcopy(list,r,ref,size,false);
  689. end;
  690. {****************************************************************************
  691. some generic implementations
  692. ****************************************************************************}
  693. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  694. var
  695. tmpreg: tregister;
  696. begin
  697. { verify if we have the same reference }
  698. if references_equal(sref,dref) then
  699. exit;
  700. tmpreg:=getintregister(list,tosize);
  701. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  702. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  703. end;
  704. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);
  705. var
  706. tmpreg: tregister;
  707. begin
  708. tmpreg:=getintregister(list,size);
  709. a_load_const_reg(list,size,a,tmpreg);
  710. a_load_reg_ref(list,size,size,tmpreg,ref);
  711. end;
  712. procedure tcg.a_load_const_loc(list : taasmoutput;a : aint;const loc: tlocation);
  713. begin
  714. case loc.loc of
  715. LOC_REFERENCE,LOC_CREFERENCE:
  716. a_load_const_ref(list,loc.size,a,loc.reference);
  717. LOC_REGISTER,LOC_CREGISTER:
  718. a_load_const_reg(list,loc.size,a,loc.register);
  719. else
  720. internalerror(200203272);
  721. end;
  722. end;
  723. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  724. begin
  725. case loc.loc of
  726. LOC_REFERENCE,LOC_CREFERENCE:
  727. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  728. LOC_REGISTER,LOC_CREGISTER:
  729. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  730. else
  731. internalerror(200203271);
  732. end;
  733. end;
  734. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  735. begin
  736. case loc.loc of
  737. LOC_REFERENCE,LOC_CREFERENCE:
  738. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  739. LOC_REGISTER,LOC_CREGISTER:
  740. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  741. LOC_CONSTANT:
  742. a_load_const_reg(list,tosize,loc.value,reg);
  743. else
  744. internalerror(200109092);
  745. end;
  746. end;
  747. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  748. begin
  749. case loc.loc of
  750. LOC_REFERENCE,LOC_CREFERENCE:
  751. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  752. LOC_REGISTER,LOC_CREGISTER:
  753. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  754. LOC_CONSTANT:
  755. a_load_const_ref(list,tosize,loc.value,ref);
  756. else
  757. internalerror(200109302);
  758. end;
  759. end;
  760. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg:tregister): boolean;
  761. var
  762. powerval : longint;
  763. begin
  764. optimize_op_const_reg := false;
  765. case op of
  766. { or with zero returns same result }
  767. OP_OR : if a = 0 then optimize_op_const_reg := true;
  768. { and with max returns same result }
  769. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  770. { division by 1 returns result }
  771. OP_DIV :
  772. begin
  773. if a = 1 then
  774. optimize_op_const_reg := true
  775. else if ispowerof2(int64(a), powerval) then
  776. begin
  777. a := powerval;
  778. op:= OP_SHR;
  779. end;
  780. exit;
  781. end;
  782. OP_IDIV:
  783. begin
  784. if a = 1 then
  785. optimize_op_const_reg := true
  786. else if ispowerof2(int64(a), powerval) then
  787. begin
  788. a := powerval;
  789. op:= OP_SAR;
  790. end;
  791. exit;
  792. end;
  793. OP_MUL,OP_IMUL:
  794. begin
  795. if a = 1 then
  796. optimize_op_const_reg := true
  797. else if ispowerof2(int64(a), powerval) then
  798. begin
  799. a := powerval;
  800. op:= OP_SHL;
  801. end;
  802. exit;
  803. end;
  804. OP_SAR,OP_SHL,OP_SHR:
  805. begin
  806. if a = 0 then
  807. optimize_op_const_reg := true;
  808. exit;
  809. end;
  810. end;
  811. end;
  812. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  813. begin
  814. case loc.loc of
  815. LOC_REFERENCE, LOC_CREFERENCE:
  816. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  817. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  818. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  819. else
  820. internalerror(200203301);
  821. end;
  822. end;
  823. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  824. begin
  825. case loc.loc of
  826. LOC_REFERENCE, LOC_CREFERENCE:
  827. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  828. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  829. a_loadfpu_reg_reg(list,size,reg,loc.register);
  830. else
  831. internalerror(48991);
  832. end;
  833. end;
  834. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const paraloc : TCGPara);
  835. var
  836. ref : treference;
  837. begin
  838. paraloc.check_simple_location;
  839. case paraloc.location^.loc of
  840. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  841. a_loadfpu_reg_reg(list,size,r,paraloc.location^.register);
  842. LOC_REFERENCE,LOC_CREFERENCE:
  843. begin
  844. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  845. a_loadfpu_reg_ref(list,size,r,ref);
  846. end
  847. else
  848. internalerror(2002071004);
  849. end;
  850. end;
  851. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  852. var
  853. href : treference;
  854. begin
  855. paraloc.check_simple_location;
  856. case paraloc.location^.loc of
  857. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  858. a_loadfpu_ref_reg(list,size,ref,paraloc.location^.register);
  859. LOC_REFERENCE,LOC_CREFERENCE:
  860. begin
  861. reference_reset_base(href,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  862. { concatcopy should choose the best way to copy the data }
  863. g_concatcopy(list,ref,href,tcgsize2size[size],false);
  864. end
  865. else
  866. internalerror(200402201);
  867. end;
  868. end;
  869. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  870. var
  871. tmpreg : tregister;
  872. begin
  873. tmpreg:=getintregister(list,size);
  874. a_load_ref_reg(list,size,size,ref,tmpreg);
  875. a_op_const_reg(list,op,size,a,tmpreg);
  876. a_load_reg_ref(list,size,size,tmpreg,ref);
  877. end;
  878. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: aint; const loc: tlocation);
  879. begin
  880. case loc.loc of
  881. LOC_REGISTER, LOC_CREGISTER:
  882. a_op_const_reg(list,op,loc.size,a,loc.register);
  883. LOC_REFERENCE, LOC_CREFERENCE:
  884. a_op_const_ref(list,op,loc.size,a,loc.reference);
  885. else
  886. internalerror(200109061);
  887. end;
  888. end;
  889. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  890. var
  891. tmpreg : tregister;
  892. begin
  893. tmpreg:=getintregister(list,size);
  894. a_load_ref_reg(list,size,size,ref,tmpreg);
  895. a_op_reg_reg(list,op,size,reg,tmpreg);
  896. a_load_reg_ref(list,size,size,tmpreg,ref);
  897. end;
  898. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  899. var
  900. tmpreg: tregister;
  901. begin
  902. case op of
  903. OP_NOT,OP_NEG:
  904. { handle it as "load ref,reg; op reg" }
  905. begin
  906. a_load_ref_reg(list,size,size,ref,reg);
  907. a_op_reg_reg(list,op,size,reg,reg);
  908. end;
  909. else
  910. begin
  911. tmpreg:=getintregister(list,size);
  912. a_load_ref_reg(list,size,size,ref,tmpreg);
  913. a_op_reg_reg(list,op,size,tmpreg,reg);
  914. end;
  915. end;
  916. end;
  917. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  918. begin
  919. case loc.loc of
  920. LOC_REGISTER, LOC_CREGISTER:
  921. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  922. LOC_REFERENCE, LOC_CREFERENCE:
  923. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  924. else
  925. internalerror(200109061);
  926. end;
  927. end;
  928. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  929. var
  930. tmpreg: tregister;
  931. begin
  932. case loc.loc of
  933. LOC_REGISTER,LOC_CREGISTER:
  934. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  935. LOC_REFERENCE,LOC_CREFERENCE:
  936. begin
  937. tmpreg:=getintregister(list,loc.size);
  938. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  939. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  940. end;
  941. else
  942. internalerror(200109061);
  943. end;
  944. end;
  945. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  946. a:aint;src,dst:Tregister);
  947. begin
  948. a_load_reg_reg(list,size,size,src,dst);
  949. a_op_const_reg(list,op,size,a,dst);
  950. end;
  951. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  952. size: tcgsize; src1, src2, dst: tregister);
  953. var
  954. tmpreg: tregister;
  955. begin
  956. if (dst<>src1) then
  957. begin
  958. a_load_reg_reg(list,size,size,src2,dst);
  959. a_op_reg_reg(list,op,size,src1,dst);
  960. end
  961. else
  962. begin
  963. tmpreg:=getintregister(list,size);
  964. a_load_reg_reg(list,size,size,src2,tmpreg);
  965. a_op_reg_reg(list,op,size,src1,tmpreg);
  966. a_load_reg_reg(list,size,size,tmpreg,dst);
  967. end;
  968. end;
  969. procedure tcg.a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  970. begin
  971. a_op_const_reg_reg(list,op,size,a,src,dst);
  972. ovloc.loc:=LOC_VOID;
  973. end;
  974. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  975. begin
  976. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  977. ovloc.loc:=LOC_VOID;
  978. end;
  979. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  980. l : tasmlabel);
  981. var
  982. tmpreg: tregister;
  983. begin
  984. tmpreg:=getintregister(list,size);
  985. a_load_ref_reg(list,size,size,ref,tmpreg);
  986. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  987. end;
  988. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  989. l : tasmlabel);
  990. begin
  991. case loc.loc of
  992. LOC_REGISTER,LOC_CREGISTER:
  993. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  994. LOC_REFERENCE,LOC_CREFERENCE:
  995. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  996. else
  997. internalerror(200109061);
  998. end;
  999. end;
  1000. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1001. var
  1002. tmpreg: tregister;
  1003. begin
  1004. tmpreg:=getintregister(list,size);
  1005. a_load_ref_reg(list,size,size,ref,tmpreg);
  1006. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1007. end;
  1008. procedure tcg.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1009. var
  1010. tmpreg: tregister;
  1011. begin
  1012. tmpreg:=getintregister(list,size);
  1013. a_load_ref_reg(list,size,size,ref,tmpreg);
  1014. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1015. end;
  1016. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1017. begin
  1018. case loc.loc of
  1019. LOC_REGISTER,
  1020. LOC_CREGISTER:
  1021. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1022. LOC_REFERENCE,
  1023. LOC_CREFERENCE :
  1024. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1025. LOC_CONSTANT:
  1026. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1027. else
  1028. internalerror(200203231);
  1029. end;
  1030. end;
  1031. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1032. l : tasmlabel);
  1033. var
  1034. tmpreg: tregister;
  1035. begin
  1036. case loc.loc of
  1037. LOC_REGISTER,LOC_CREGISTER:
  1038. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1039. LOC_REFERENCE,LOC_CREFERENCE:
  1040. begin
  1041. tmpreg:=getintregister(list,size);
  1042. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1043. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1044. end
  1045. else
  1046. internalerror(200109061);
  1047. end;
  1048. end;
  1049. procedure tcg.a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1050. begin
  1051. case loc.loc of
  1052. LOC_MMREGISTER,LOC_CMMREGISTER:
  1053. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1054. LOC_REFERENCE,LOC_CREFERENCE:
  1055. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1056. else
  1057. internalerror(200310121);
  1058. end;
  1059. end;
  1060. procedure tcg.a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1061. begin
  1062. case loc.loc of
  1063. LOC_MMREGISTER,LOC_CMMREGISTER:
  1064. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1065. LOC_REFERENCE,LOC_CREFERENCE:
  1066. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1067. else
  1068. internalerror(200310122);
  1069. end;
  1070. end;
  1071. procedure tcg.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const paraloc : TCGPara;shuffle : pmmshuffle);
  1072. var
  1073. href : treference;
  1074. begin
  1075. paraloc.check_simple_location;
  1076. case paraloc.location^.loc of
  1077. LOC_MMREGISTER,LOC_CMMREGISTER:
  1078. a_loadmm_reg_reg(list,size,paraloc.location^.size,reg,paraloc.location^.register,shuffle);
  1079. LOC_REFERENCE,LOC_CREFERENCE:
  1080. begin
  1081. reference_reset_base(href,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  1082. a_loadmm_reg_ref(list,size,paraloc.location^.size,reg,href,shuffle);
  1083. end
  1084. else
  1085. internalerror(200310123);
  1086. end;
  1087. end;
  1088. procedure tcg.a_parammm_ref(list: taasmoutput; size: tcgsize;const ref: treference;const paraloc : TCGPara;shuffle : pmmshuffle);
  1089. var
  1090. hr : tregister;
  1091. hs : tmmshuffle;
  1092. begin
  1093. paraloc.check_simple_location;
  1094. hr:=getmmregister(list,paraloc.location^.size);
  1095. a_loadmm_ref_reg(list,size,paraloc.location^.size,ref,hr,shuffle);
  1096. if realshuffle(shuffle) then
  1097. begin
  1098. hs:=shuffle^;
  1099. removeshuffles(hs);
  1100. a_parammm_reg(list,paraloc.location^.size,hr,paraloc,@hs);
  1101. end
  1102. else
  1103. a_parammm_reg(list,paraloc.location^.size,hr,paraloc,shuffle);
  1104. end;
  1105. procedure tcg.a_parammm_loc(list: taasmoutput;const loc: tlocation; const paraloc : TCGPara;shuffle : pmmshuffle);
  1106. begin
  1107. case loc.loc of
  1108. LOC_MMREGISTER,LOC_CMMREGISTER:
  1109. a_parammm_reg(list,loc.size,loc.register,paraloc,shuffle);
  1110. LOC_REFERENCE,LOC_CREFERENCE:
  1111. a_parammm_ref(list,loc.size,loc.reference,paraloc,shuffle);
  1112. else
  1113. internalerror(200310123);
  1114. end;
  1115. end;
  1116. procedure tcg.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1117. var
  1118. hr : tregister;
  1119. hs : tmmshuffle;
  1120. begin
  1121. hr:=getmmregister(list,size);
  1122. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1123. if realshuffle(shuffle) then
  1124. begin
  1125. hs:=shuffle^;
  1126. removeshuffles(hs);
  1127. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1128. end
  1129. else
  1130. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1131. end;
  1132. procedure tcg.a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1133. var
  1134. hr : tregister;
  1135. hs : tmmshuffle;
  1136. begin
  1137. hr:=getmmregister(list,size);
  1138. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1139. if realshuffle(shuffle) then
  1140. begin
  1141. hs:=shuffle^;
  1142. removeshuffles(hs);
  1143. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1144. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1145. end
  1146. else
  1147. begin
  1148. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1149. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1150. end;
  1151. end;
  1152. procedure tcg.a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1153. begin
  1154. case loc.loc of
  1155. LOC_CMMREGISTER,LOC_MMREGISTER:
  1156. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1157. LOC_CREFERENCE,LOC_REFERENCE:
  1158. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1159. else
  1160. internalerror(200312232);
  1161. end;
  1162. end;
  1163. procedure tcg.g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint;loadref : boolean);
  1164. begin
  1165. g_concatcopy(list,source,dest,len,loadref);
  1166. end;
  1167. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;loadref : boolean);
  1168. var
  1169. paraloc1,paraloc2,paraloc3 : TCGPara;
  1170. begin
  1171. paraloc1.init;
  1172. paraloc2.init;
  1173. paraloc3.init;
  1174. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1175. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1176. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1177. paramanager.allocparaloc(list,paraloc3);
  1178. a_paramaddr_ref(list,dest,paraloc3);
  1179. paramanager.allocparaloc(list,paraloc2);
  1180. if loadref then
  1181. a_param_ref(list,OS_ADDR,source,paraloc2)
  1182. else
  1183. a_paramaddr_ref(list,source,paraloc2);
  1184. paramanager.allocparaloc(list,paraloc1);
  1185. a_param_const(list,OS_INT,len,paraloc1);
  1186. paramanager.freeparaloc(list,paraloc3);
  1187. paramanager.freeparaloc(list,paraloc2);
  1188. paramanager.freeparaloc(list,paraloc1);
  1189. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1190. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1191. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1192. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1193. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1194. paraloc3.done;
  1195. paraloc2.done;
  1196. paraloc1.done;
  1197. end;
  1198. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  1199. var
  1200. href : treference;
  1201. incrfunc : string;
  1202. paraloc1,paraloc2 : TCGPara;
  1203. begin
  1204. paraloc1.init;
  1205. paraloc2.init;
  1206. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1207. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1208. { These functions should not change the registers (they use
  1209. the saveregister proc directive }
  1210. if is_interfacecom(t) then
  1211. incrfunc:='FPC_INTF_INCR_REF'
  1212. else if is_ansistring(t) then
  1213. {$ifdef ansistring_bits}
  1214. begin
  1215. case Tstringdef(t).string_typ of
  1216. st_ansistring16:
  1217. incrfunc:='FPC_ANSISTR16_INCR_REF';
  1218. st_ansistring32:
  1219. incrfunc:='FPC_ANSISTR32_INCR_REF';
  1220. st_ansistring64:
  1221. incrfunc:='FPC_ANSISTR64_INCR_REF';
  1222. end;
  1223. end
  1224. {$else}
  1225. incrfunc:='FPC_ANSISTR_INCR_REF'
  1226. {$endif}
  1227. else if is_widestring(t) then
  1228. incrfunc:='FPC_WIDESTR_INCR_REF'
  1229. else if is_dynamic_array(t) then
  1230. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1231. else
  1232. incrfunc:='';
  1233. { call the special incr function or the generic addref }
  1234. if incrfunc<>'' then
  1235. begin
  1236. { these functions get the pointer by value }
  1237. paramanager.allocparaloc(list,paraloc1);
  1238. a_param_ref(list,OS_ADDR,ref,paraloc1);
  1239. paramanager.freeparaloc(list,paraloc1);
  1240. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1241. a_call_name(list,incrfunc);
  1242. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1243. end
  1244. else
  1245. begin
  1246. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1247. paramanager.allocparaloc(list,paraloc2);
  1248. a_paramaddr_ref(list,href,paraloc2);
  1249. paramanager.allocparaloc(list,paraloc1);
  1250. if loadref then
  1251. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1252. else
  1253. a_paramaddr_ref(list,ref,paraloc1);
  1254. paramanager.freeparaloc(list,paraloc1);
  1255. paramanager.freeparaloc(list,paraloc2);
  1256. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1257. a_call_name(list,'FPC_ADDREF');
  1258. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1259. end;
  1260. paraloc2.done;
  1261. paraloc1.done;
  1262. end;
  1263. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference; loadref:boolean);
  1264. var
  1265. hreg : tregister;
  1266. href : treference;
  1267. decrfunc : string;
  1268. needrtti : boolean;
  1269. paraloc1,paraloc2 : TCGPara;
  1270. begin
  1271. paraloc1.init;
  1272. paraloc2.init;
  1273. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1274. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1275. needrtti:=false;
  1276. if is_interfacecom(t) then
  1277. decrfunc:='FPC_INTF_DECR_REF'
  1278. else if is_ansistring(t) then
  1279. {$ifdef ansistring_bits}
  1280. begin
  1281. case Tstringdef(t).string_typ of
  1282. st_ansistring16:
  1283. decrfunc:='FPC_ANSISTR16_DECR_REF';
  1284. st_ansistring32:
  1285. decrfunc:='FPC_ANSISTR32_DECR_REF';
  1286. st_ansistring64:
  1287. decrfunc:='FPC_ANSISTR64_DECR_REF';
  1288. end;
  1289. end
  1290. {$else}
  1291. decrfunc:='FPC_ANSISTR_DECR_REF'
  1292. {$endif}
  1293. else if is_widestring(t) then
  1294. decrfunc:='FPC_WIDESTR_DECR_REF'
  1295. else if is_dynamic_array(t) then
  1296. begin
  1297. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1298. needrtti:=true;
  1299. end
  1300. else
  1301. decrfunc:='';
  1302. { call the special decr function or the generic decref }
  1303. if decrfunc<>'' then
  1304. begin
  1305. if needrtti then
  1306. begin
  1307. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1308. paramanager.allocparaloc(list,paraloc2);
  1309. a_paramaddr_ref(list,href,paraloc2);
  1310. end;
  1311. paramanager.allocparaloc(list,paraloc1);
  1312. if loadref then
  1313. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1314. else
  1315. a_paramaddr_ref(list,ref,paraloc1);
  1316. paramanager.freeparaloc(list,paraloc1);
  1317. if needrtti then
  1318. paramanager.freeparaloc(list,paraloc2);
  1319. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1320. a_call_name(list,decrfunc);
  1321. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1322. end
  1323. else
  1324. begin
  1325. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1326. paramanager.allocparaloc(list,paraloc2);
  1327. a_paramaddr_ref(list,href,paraloc2);
  1328. paramanager.allocparaloc(list,paraloc1);
  1329. if loadref then
  1330. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1331. else
  1332. a_paramaddr_ref(list,ref,paraloc1);
  1333. paramanager.freeparaloc(list,paraloc1);
  1334. paramanager.freeparaloc(list,paraloc2);
  1335. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1336. a_call_name(list,'FPC_DECREF');
  1337. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1338. end;
  1339. { Temp locations need always to be reset to 0 }
  1340. if tg.istemp(ref) then
  1341. begin
  1342. if loadref then
  1343. begin
  1344. hreg:=getaddressregister(list);
  1345. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,hreg);
  1346. reference_reset_base(href,hreg,0);
  1347. a_load_const_ref(list,OS_ADDR,0,href);
  1348. end
  1349. else
  1350. a_load_const_ref(list,OS_ADDR,0,ref);
  1351. end;
  1352. paraloc2.done;
  1353. paraloc1.done;
  1354. end;
  1355. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1356. var
  1357. href : treference;
  1358. paraloc1,paraloc2 : TCGPara;
  1359. begin
  1360. paraloc1.init;
  1361. paraloc2.init;
  1362. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1363. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1364. if is_ansistring(t) or
  1365. is_widestring(t) or
  1366. is_interfacecom(t) or
  1367. is_dynamic_array(t) then
  1368. a_load_const_ref(list,OS_ADDR,0,ref)
  1369. else
  1370. begin
  1371. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1372. paramanager.allocparaloc(list,paraloc2);
  1373. a_paramaddr_ref(list,href,paraloc2);
  1374. paramanager.allocparaloc(list,paraloc1);
  1375. if loadref then
  1376. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1377. else
  1378. a_paramaddr_ref(list,ref,paraloc1);
  1379. paramanager.freeparaloc(list,paraloc1);
  1380. paramanager.freeparaloc(list,paraloc2);
  1381. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1382. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1383. a_call_name(list,'FPC_INITIALIZE');
  1384. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1385. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1386. end;
  1387. paraloc1.done;
  1388. paraloc2.done;
  1389. end;
  1390. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1391. var
  1392. hreg : tregister;
  1393. href : treference;
  1394. paraloc1,paraloc2 : TCGPara;
  1395. begin
  1396. paraloc1.init;
  1397. paraloc2.init;
  1398. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1399. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1400. if is_ansistring(t) or
  1401. is_widestring(t) or
  1402. is_interfacecom(t) then
  1403. begin
  1404. g_decrrefcount(list,t,ref,loadref);
  1405. { Temp locations are already reset to 0 }
  1406. if not tg.istemp(ref) then
  1407. begin
  1408. if loadref then
  1409. begin
  1410. hreg:=getaddressregister(list);
  1411. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,hreg);
  1412. reference_reset_base(href,hreg,0);
  1413. a_load_const_ref(list,OS_ADDR,0,href);
  1414. end
  1415. else
  1416. a_load_const_ref(list,OS_ADDR,0,ref);
  1417. end;
  1418. end
  1419. else
  1420. begin
  1421. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1422. paramanager.allocparaloc(list,paraloc2);
  1423. a_paramaddr_ref(list,href,paraloc2);
  1424. paramanager.allocparaloc(list,paraloc1);
  1425. if loadref then
  1426. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1427. else
  1428. a_paramaddr_ref(list,ref,paraloc1);
  1429. paramanager.freeparaloc(list,paraloc1);
  1430. paramanager.freeparaloc(list,paraloc2);
  1431. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1432. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1433. a_call_name(list,'FPC_FINALIZE');
  1434. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1435. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1436. end;
  1437. paraloc1.done;
  1438. paraloc2.done;
  1439. end;
  1440. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1441. { generate range checking code for the value at location p. The type }
  1442. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1443. { is the original type used at that location. When both defs are equal }
  1444. { the check is also insert (needed for succ,pref,inc,dec) }
  1445. {$ifndef ver1_0}
  1446. const
  1447. aintmax=high(aint);
  1448. {$endif}
  1449. var
  1450. neglabel : tasmlabel;
  1451. hreg : tregister;
  1452. lto,hto,
  1453. lfrom,hfrom : TConstExprInt;
  1454. from_signed: boolean;
  1455. {$ifdef ver1_0}
  1456. aintmax : aint;
  1457. {$endif ver1_0}
  1458. begin
  1459. {$ifdef ver1_0}
  1460. {$ifdef cpu64bit}
  1461. { this is required to prevent incorrect code }
  1462. aintmax:=$7fffffff;
  1463. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1464. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1465. {$else cpu64bit}
  1466. aintmax:=high(aint);
  1467. {$endif cpu64bit}
  1468. {$endif}
  1469. { range checking on and range checkable value? }
  1470. if not(cs_check_range in aktlocalswitches) or
  1471. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1472. exit;
  1473. {$ifndef cpu64bit}
  1474. { handle 64bit rangechecks separate for 32bit processors }
  1475. if is_64bit(fromdef) or is_64bit(todef) then
  1476. begin
  1477. cg64.g_rangecheck64(list,l,fromdef,todef);
  1478. exit;
  1479. end;
  1480. {$endif cpu64bit}
  1481. { only check when assigning to scalar, subranges are different, }
  1482. { when todef=fromdef then the check is always generated }
  1483. getrange(fromdef,lfrom,hfrom);
  1484. getrange(todef,lto,hto);
  1485. from_signed := is_signed(fromdef);
  1486. { no range check if from and to are equal and are both longint/dword }
  1487. { (if we have a 32bit processor) or int64/qword, since such }
  1488. { operations can at most cause overflows (JM) }
  1489. { Note that these checks are mostly processor independent, they only }
  1490. { have to be changed once we introduce 64bit subrange types }
  1491. {$ifdef cpu64bit}
  1492. if (fromdef = todef) and
  1493. (fromdef.deftype=orddef) and
  1494. (((((torddef(fromdef).typ = s64bit) and
  1495. (lfrom = low(int64)) and
  1496. (hfrom = high(int64))) or
  1497. ((torddef(fromdef).typ = u64bit) and
  1498. (lfrom = low(qword)) and
  1499. (hfrom = high(qword)))))) then
  1500. exit;
  1501. {$else cpu64bit}
  1502. if (fromdef = todef) and
  1503. (fromdef.deftype=orddef) and
  1504. (((((torddef(fromdef).typ = s32bit) and
  1505. (lfrom = low(longint)) and
  1506. (hfrom = high(longint))) or
  1507. ((torddef(fromdef).typ = u32bit) and
  1508. (lfrom = low(cardinal)) and
  1509. (hfrom = high(cardinal)))))) then
  1510. exit;
  1511. {$endif cpu64bit}
  1512. { if the from-range falls completely in the to-range, no check }
  1513. { is necessary. Don't do this conversion for the largest unsigned type }
  1514. if (todef<>fromdef) and
  1515. (from_signed or (hfrom>=0)) and
  1516. (lto<=lfrom) and (hto>=hfrom) then
  1517. exit;
  1518. { generate the rangecheck code for the def where we are going to }
  1519. { store the result }
  1520. { use the trick that }
  1521. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1522. { To be able to do that, we have to make sure however that either }
  1523. { fromdef and todef are both signed or unsigned, or that we leave }
  1524. { the parts < 0 and > maxlongint out }
  1525. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1526. if from_signed xor is_signed(todef) then
  1527. begin
  1528. if from_signed then
  1529. { from is signed, to is unsigned }
  1530. begin
  1531. { if high(from) < 0 -> always range error }
  1532. if (hfrom < 0) or
  1533. { if low(to) > maxlongint also range error }
  1534. (lto > aintmax) then
  1535. begin
  1536. a_call_name(list,'FPC_RANGEERROR');
  1537. exit
  1538. end;
  1539. { from is signed and to is unsigned -> when looking at to }
  1540. { as an signed value, it must be < maxaint (otherwise }
  1541. { it will become negative, which is invalid since "to" is unsigned) }
  1542. if hto > aintmax then
  1543. hto := aintmax;
  1544. end
  1545. else
  1546. { from is unsigned, to is signed }
  1547. begin
  1548. if (lfrom > aintmax) or
  1549. (hto < 0) then
  1550. begin
  1551. a_call_name(list,'FPC_RANGEERROR');
  1552. exit
  1553. end;
  1554. { from is unsigned and to is signed -> when looking at to }
  1555. { as an unsigned value, it must be >= 0 (since negative }
  1556. { values are the same as values > maxlongint) }
  1557. if lto < 0 then
  1558. lto := 0;
  1559. end;
  1560. end;
  1561. hreg:=getintregister(list,OS_INT);
  1562. a_load_loc_reg(list,OS_INT,l,hreg);
  1563. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  1564. objectlibrary.getlabel(neglabel);
  1565. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  1566. a_call_name(list,'FPC_RANGEERROR');
  1567. a_label(list,neglabel);
  1568. end;
  1569. procedure tcg.g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1570. begin
  1571. g_overflowCheck(list,loc,def);
  1572. end;
  1573. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1574. var
  1575. tmpreg : tregister;
  1576. begin
  1577. tmpreg:=getintregister(list,size);
  1578. g_flags2reg(list,size,f,tmpreg);
  1579. a_load_reg_ref(list,size,size,tmpreg,ref);
  1580. end;
  1581. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1582. var
  1583. OKLabel : tasmlabel;
  1584. paraloc1 : TCGPara;
  1585. begin
  1586. if (cs_check_object in aktlocalswitches) or
  1587. (cs_check_range in aktlocalswitches) then
  1588. begin
  1589. objectlibrary.getlabel(oklabel);
  1590. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1591. paraloc1.init;
  1592. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1593. paramanager.allocparaloc(list,paraloc1);
  1594. a_param_const(list,OS_INT,210,paraloc1);
  1595. paramanager.freeparaloc(list,paraloc1);
  1596. a_call_name(list,'FPC_HANDLEERROR');
  1597. a_label(list,oklabel);
  1598. paraloc1.done;
  1599. end;
  1600. end;
  1601. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1602. var
  1603. hrefvmt : treference;
  1604. paraloc1,paraloc2 : TCGPara;
  1605. begin
  1606. paraloc1.init;
  1607. paraloc2.init;
  1608. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1609. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1610. if (cs_check_object in aktlocalswitches) then
  1611. begin
  1612. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(objdef.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1613. paramanager.allocparaloc(list,paraloc2);
  1614. a_paramaddr_ref(list,hrefvmt,paraloc2);
  1615. paramanager.allocparaloc(list,paraloc1);
  1616. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1617. paramanager.freeparaloc(list,paraloc1);
  1618. paramanager.freeparaloc(list,paraloc2);
  1619. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1620. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1621. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1622. end
  1623. else
  1624. if (cs_check_range in aktlocalswitches) then
  1625. begin
  1626. paramanager.allocparaloc(list,paraloc1);
  1627. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1628. paramanager.freeparaloc(list,paraloc1);
  1629. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1630. a_call_name(list,'FPC_CHECK_OBJECT');
  1631. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1632. end;
  1633. paraloc1.done;
  1634. paraloc2.done;
  1635. end;
  1636. {*****************************************************************************
  1637. Entry/Exit Code Functions
  1638. *****************************************************************************}
  1639. procedure tcg.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;loadref:boolean);
  1640. var
  1641. sizereg,sourcereg,destreg : tregister;
  1642. paraloc1,paraloc2,paraloc3 : TCGPara;
  1643. begin
  1644. { because ppc abi doesn't support dynamic stack allocation properly
  1645. open array value parameters are copied onto the heap
  1646. }
  1647. { allocate two registers for len and source }
  1648. sizereg:=getintregister(list,OS_INT);
  1649. sourcereg:=getintregister(list,OS_ADDR);
  1650. destreg:=getintregister(list,OS_ADDR);
  1651. { calculate necessary memory }
  1652. a_load_loc_reg(list,OS_INT,lenloc,sizereg);
  1653. a_op_const_reg(list,OP_ADD,OS_INT,1,sizereg);
  1654. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  1655. { load source }
  1656. if loadref then
  1657. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,sourcereg)
  1658. else
  1659. begin
  1660. if (ref.index<>NR_NO) or (ref.offset<>0) then
  1661. internalerror(200410126);
  1662. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,sourcereg);
  1663. end;
  1664. { do getmem call }
  1665. paraloc1.init;
  1666. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1667. paramanager.allocparaloc(list,paraloc1);
  1668. a_param_reg(list,OS_INT,sizereg,paraloc1);
  1669. paramanager.freeparaloc(list,paraloc1);
  1670. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1671. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1672. a_call_name(list,'FPC_GETMEM');
  1673. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1674. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1675. paraloc1.done;
  1676. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  1677. { patch the new address }
  1678. if loadref then
  1679. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,ref)
  1680. else
  1681. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,ref.base);
  1682. { do move call }
  1683. paraloc1.init;
  1684. paraloc2.init;
  1685. paraloc3.init;
  1686. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1687. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1688. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1689. { load size }
  1690. paramanager.allocparaloc(list,paraloc3);
  1691. a_param_reg(list,OS_INT,sizereg,paraloc3);
  1692. { load destination }
  1693. paramanager.allocparaloc(list,paraloc2);
  1694. a_param_reg(list,OS_ADDR,destreg,paraloc2);
  1695. { load source }
  1696. paramanager.allocparaloc(list,paraloc1);
  1697. a_param_reg(list,OS_ADDR,sourcereg,paraloc1);
  1698. paramanager.freeparaloc(list,paraloc3);
  1699. paramanager.freeparaloc(list,paraloc2);
  1700. paramanager.freeparaloc(list,paraloc1);
  1701. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1702. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1703. a_call_name(list,'FPC_MOVE');
  1704. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1705. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1706. paraloc3.done;
  1707. paraloc2.done;
  1708. paraloc1.done;
  1709. end;
  1710. procedure tcg.g_releasevaluepara_openarray(list : taasmoutput;const ref:treference);
  1711. var
  1712. paraloc1 : TCGPara;
  1713. begin
  1714. { do move call }
  1715. paraloc1.init;
  1716. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1717. { load source }
  1718. paramanager.allocparaloc(list,paraloc1);
  1719. a_param_ref(list,OS_ADDR,ref,paraloc1);
  1720. paramanager.freeparaloc(list,paraloc1);
  1721. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1722. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1723. a_call_name(list,'FPC_FREEMEM');
  1724. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1725. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1726. paraloc1.done;
  1727. end;
  1728. procedure tcg.g_profilecode(list : taasmoutput);
  1729. begin
  1730. end;
  1731. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1732. begin
  1733. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  1734. end;
  1735. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);
  1736. begin
  1737. a_load_const_ref(list, OS_INT, a, href);
  1738. end;
  1739. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1740. begin
  1741. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  1742. end;
  1743. {*****************************************************************************
  1744. TCG64
  1745. *****************************************************************************}
  1746. {$ifndef cpu64bit}
  1747. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64; regsrc,regdst : tregister64);
  1748. begin
  1749. a_load64_reg_reg(list,regsrc,regdst);
  1750. a_op64_const_reg(list,op,value,regdst);
  1751. end;
  1752. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1753. var
  1754. tmpreg64 : tregister64;
  1755. begin
  1756. { when src1=dst then we need to first create a temp to prevent
  1757. overwriting src1 with src2 }
  1758. if (regsrc1.reghi=regdst.reghi) or
  1759. (regsrc1.reglo=regdst.reghi) or
  1760. (regsrc1.reghi=regdst.reglo) or
  1761. (regsrc1.reglo=regdst.reglo) then
  1762. begin
  1763. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  1764. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  1765. a_load64_reg_reg(list,regsrc2,tmpreg64);
  1766. a_op64_reg_reg(list,op,regsrc1,tmpreg64);
  1767. a_load64_reg_reg(list,tmpreg64,regdst);
  1768. end
  1769. else
  1770. begin
  1771. a_load64_reg_reg(list,regsrc2,regdst);
  1772. a_op64_reg_reg(list,op,regsrc1,regdst);
  1773. end;
  1774. end;
  1775. {$endif cpu64bit}
  1776. {****************************************************************************
  1777. TLocation
  1778. ****************************************************************************}
  1779. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1780. begin
  1781. FillChar(l,sizeof(tlocation),0);
  1782. l.loc:=lt;
  1783. l.size:=lsize;
  1784. {$ifdef arm}
  1785. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1786. l.reference.signindex:=1;
  1787. {$endif arm}
  1788. end;
  1789. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1790. begin
  1791. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1792. tg.ungetiftemp(list,l.reference);
  1793. end;
  1794. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  1795. begin
  1796. destloc:=sourceloc;
  1797. end;
  1798. procedure location_swap(var destloc,sourceloc : tlocation);
  1799. var
  1800. swapl : tlocation;
  1801. begin
  1802. swapl := destloc;
  1803. destloc := sourceloc;
  1804. sourceloc := swapl;
  1805. end;
  1806. initialization
  1807. ;
  1808. finalization
  1809. cg.free;
  1810. {$ifndef cpu64bit}
  1811. cg64.free;
  1812. {$endif cpu64bit}
  1813. end.
  1814. {
  1815. $Log$
  1816. Revision 1.178 2004-10-13 21:12:51 peter
  1817. * -Or fixes for open array
  1818. Revision 1.177 2004/10/11 15:46:45 peter
  1819. * length parameter for copyvaluearray changed to tlocation
  1820. Revision 1.176 2004/10/10 20:31:48 peter
  1821. * concatcopy_unaligned maps by default to concatcopy, sparc will
  1822. override it with call to fpc_move
  1823. Revision 1.175 2004/10/10 20:22:53 peter
  1824. * symtable allocation rewritten
  1825. * loading of parameters to local temps/regs cleanup
  1826. * regvar support for parameters
  1827. * regvar support for staticsymtable (main body)
  1828. Revision 1.174 2004/10/05 20:41:01 peter
  1829. * more spilling rewrites
  1830. Revision 1.173 2004/09/29 18:55:40 florian
  1831. * fixed more sparc overflow stuff
  1832. * fixed some op64 stuff for sparc
  1833. Revision 1.172 2004/09/26 21:04:35 florian
  1834. + partial overflow checking on sparc; multiplication still missing
  1835. Revision 1.171 2004/09/26 17:45:30 peter
  1836. * simple regvar support, not yet finished
  1837. Revision 1.170 2004/09/25 14:23:54 peter
  1838. * ungetregister is now only used for cpuregisters, renamed to
  1839. ungetcpuregister
  1840. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1841. * removed location-release/reference_release
  1842. Revision 1.169 2004/09/21 17:25:12 peter
  1843. * paraloc branch merged
  1844. Revision 1.168.4.4 2004/09/20 20:45:57 peter
  1845. * remove cg64.a_reg_alloc, it should not be used since it
  1846. create more register conflicts
  1847. Revision 1.168.4.3 2004/09/18 20:22:40 jonas
  1848. * allocate the volatile fpu registers around procedures that might use
  1849. them (e.g. FPCMOVE may use them)
  1850. Revision 1.168.4.2 2004/09/12 13:36:40 peter
  1851. * fixed alignment issues
  1852. Revision 1.168.4.1 2004/08/31 20:43:06 peter
  1853. * paraloc patch
  1854. Revision 1.168 2004/07/09 23:41:04 jonas
  1855. * support register parameters for inlined procedures + some inline
  1856. cleanups
  1857. Revision 1.167 2004/07/03 11:47:04 peter
  1858. * fix rangecheck error when assigning u32bit=s32bit
  1859. Revision 1.166 2004/06/20 08:55:28 florian
  1860. * logs truncated
  1861. Revision 1.165 2004/06/16 20:07:07 florian
  1862. * dwarf branch merged
  1863. Revision 1.164 2004/05/22 23:34:27 peter
  1864. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1865. Revision 1.163 2004/04/29 19:56:36 daniel
  1866. * Prepare compiler infrastructure for multiple ansistring types
  1867. Revision 1.162 2004/04/18 07:52:43 florian
  1868. * fixed web bug 3048: comparision of dyn. arrays
  1869. Revision 1.161.2.17 2004/06/13 10:51:16 florian
  1870. * fixed several register allocator problems (sparc/arm)
  1871. }