cgobj.pas 93 KB

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