cgobj.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the basic code generator object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# @abstract(Abstract code generator unit)
  19. Abstreact code generator unit. This contains the base class
  20. to implement for all new supported processors.
  21. WARNING: None of the routines implemented in these modules,
  22. or their descendants, should use the temp. allocator, as
  23. these routines may be called inside genentrycode, and the
  24. stack frame is already setup!
  25. }
  26. unit cgobj;
  27. {$i fpcdefs.inc}
  28. interface
  29. uses
  30. globtype,constexp,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symtype,symdef,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. {# @abstract(Abstract code generator)
  38. This class implements an abstract instruction generator. Some of
  39. the methods of this class are generic, while others must
  40. be overridden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. should be @link(tcg64f32) and not @var(tcg).
  43. }
  44. tcg = class
  45. public
  46. { how many times is this current code executed }
  47. executionweight : longint;
  48. alignment : talignment;
  49. rg : array[tregistertype] of trgobj;
  50. {$ifdef flowgraph}
  51. aktflownode:word;
  52. {$endif}
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. {# Initialize the register allocators needed for the codegenerator.}
  57. procedure init_register_allocators;virtual;
  58. {# Clean up the register allocators needed for the codegenerator.}
  59. procedure done_register_allocators;virtual;
  60. {# Set whether live_start or live_end should be updated when allocating registers, needed when e.g. generating initcode after the rest of the code. }
  61. procedure set_regalloc_live_range_direction(dir: TRADirection);
  62. {$ifdef flowgraph}
  63. procedure init_flowgraph;
  64. procedure done_flowgraph;
  65. {$endif}
  66. {# Gets a register suitable to do integer operations on.}
  67. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  68. {# Gets a register suitable to do integer operations on.}
  69. function getaddressregister(list:TAsmList):Tregister;virtual;
  70. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  71. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  72. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  73. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  74. the cpu specific child cg object have such a method?}
  75. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  76. procedure add_move_instruction(instr:Taicpu);virtual;
  77. function uses_registers(rt:Tregistertype):boolean;virtual;
  78. {# Get a specific register.}
  79. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  80. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  81. {# Get multiple registers specified.}
  82. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  83. {# Free multiple registers specified.}
  84. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  85. procedure allocallcpuregisters(list:TAsmList);virtual;
  86. procedure deallocallcpuregisters(list:TAsmList);virtual;
  87. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  88. procedure translate_register(var reg : tregister);
  89. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  90. {# Emit a label to the instruction stream. }
  91. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  92. {# Allocates register r by inserting a pai_realloc record }
  93. procedure a_reg_alloc(list : TAsmList;r : tregister);
  94. {# Deallocates register r by inserting a pa_regdealloc record}
  95. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  96. { Synchronize register, make sure it is still valid }
  97. procedure a_reg_sync(list : TAsmList;r : tregister);
  98. {# Pass a parameter, which is located in a register, to a routine.
  99. This routine should push/send the parameter to the routine, as
  100. required by the specific processor ABI and routine modifiers.
  101. It must generate register allocation information for the cgpara in
  102. case it consists of cpuregisters.
  103. @param(size size of the operand in the register)
  104. @param(r register source of the operand)
  105. @param(cgpara where the parameter will be stored)
  106. }
  107. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  108. {# Pass a parameter, which is a constant, to a routine.
  109. A generic version is provided. This routine should
  110. be overridden for optimization purposes if the cpu
  111. permits directly sending this type of parameter.
  112. It must generate register allocation information for the cgpara in
  113. case it consists of cpuregisters.
  114. @param(size size of the operand in constant)
  115. @param(a value of constant to send)
  116. @param(cgpara where the parameter will be stored)
  117. }
  118. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  119. {# Pass the value of a parameter, which is located in memory, to a routine.
  120. A generic version is provided. This routine should
  121. be overridden for optimization purposes if the cpu
  122. permits directly sending this type of parameter.
  123. It must generate register allocation information for the cgpara in
  124. case it consists of cpuregisters.
  125. @param(size size of the operand in constant)
  126. @param(r Memory reference of value to send)
  127. @param(cgpara where the parameter will be stored)
  128. }
  129. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  130. {# Pass the value of a parameter, which can be located either in a register or memory location,
  131. to a routine.
  132. A generic version is provided.
  133. @param(l location of the operand to send)
  134. @param(nr parameter number (starting from one) of routine (from left to right))
  135. @param(cgpara where the parameter will be stored)
  136. }
  137. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  138. {# Pass the address of a reference to a routine. This routine
  139. will calculate the address of the reference, and pass this
  140. calculated address as a parameter.
  141. It must generate register allocation information for the cgpara in
  142. case it consists of cpuregisters.
  143. A generic version is provided. This routine should
  144. be overridden for optimization purposes if the cpu
  145. permits directly sending this type of parameter.
  146. @param(r reference to get address from)
  147. @param(nr parameter number (starting from one) of routine (from left to right))
  148. }
  149. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  150. {# Load a cgparaloc into a memory reference.
  151. It must generate register allocation information for the cgpara in
  152. case it consists of cpuregisters.
  153. @param(paraloc the source parameter sublocation)
  154. @param(ref the destination reference)
  155. @param(sizeleft indicates the total number of bytes left in all of
  156. the remaining sublocations of this parameter (the current
  157. sublocation and all of the sublocations coming after it).
  158. In case this location is also a reference, it is assumed
  159. to be the final part sublocation of the parameter and that it
  160. contains all of the "sizeleft" bytes).)
  161. @param(align the alignment of the paraloc in case it's a reference)
  162. }
  163. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  164. {# Load a cgparaloc into any kind of register (int, fp, mm).
  165. @param(regsize the size of the destination register)
  166. @param(paraloc the source parameter sublocation)
  167. @param(reg the destination register)
  168. @param(align the alignment of the paraloc in case it's a reference)
  169. }
  170. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  171. { Remarks:
  172. * If a method specifies a size you have only to take care
  173. of that number of bits, i.e. load_const_reg with OP_8 must
  174. only load the lower 8 bit of the specified register
  175. the rest of the register can be undefined
  176. if necessary the compiler will call a method
  177. to zero or sign extend the register
  178. * The a_load_XX_XX with OP_64 needn't to be
  179. implemented for 32 bit
  180. processors, the code generator takes care of that
  181. * the addr size is for work with the natural pointer
  182. size
  183. * the procedures without fpu/mm are only for integer usage
  184. * normally the first location is the source and the
  185. second the destination
  186. }
  187. {# Emits instruction to call the method specified by symbol name.
  188. This routine must be overridden for each new target cpu.
  189. }
  190. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  191. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  192. procedure a_call_ref(list : TAsmList;ref : treference);virtual;
  193. { same as a_call_name, might be overridden on certain architectures to emit
  194. static calls without usage of a got trampoline }
  195. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  196. { move instructions }
  197. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  198. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  199. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  200. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  201. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  202. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  203. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  204. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  205. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  206. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  207. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  208. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  209. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  210. { bit scan instructions }
  211. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister); virtual; abstract;
  212. { fpu move instructions }
  213. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  214. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  215. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  216. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  217. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  218. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  219. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  220. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  221. { vector register move instructions }
  222. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  223. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  224. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  226. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  227. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  228. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  229. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  230. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  231. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  232. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  233. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  234. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  235. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  236. { basic arithmetic operations }
  237. { note: for operators which require only one argument (not, neg), use }
  238. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  239. { that in this case the *second* operand is used as both source and }
  240. { destination (JM) }
  241. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  242. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  243. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  244. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  245. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  246. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  247. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  248. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  249. { trinary operations for processors that support them, 'emulated' }
  250. { on others. None with "ref" arguments since I don't think there }
  251. { are any processors that support it (JM) }
  252. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  253. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  254. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  255. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  256. { comparison operations }
  257. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  258. l : tasmlabel); virtual;
  259. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  260. l : tasmlabel); virtual;
  261. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  262. l : tasmlabel);
  263. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  264. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  265. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  266. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  267. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  268. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  269. l : tasmlabel);
  270. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  271. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  272. {$ifdef cpuflags}
  273. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  274. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  275. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  276. }
  277. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  278. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  279. {$endif cpuflags}
  280. {
  281. This routine tries to optimize the op_const_reg/ref opcode, and should be
  282. called at the start of a_op_const_reg/ref. It returns the actual opcode
  283. to emit, and the constant value to emit. This function can opcode OP_NONE to
  284. remove the opcode and OP_MOVE to replace it with a simple load
  285. @param(op The opcode to emit, returns the opcode which must be emitted)
  286. @param(a The constant which should be emitted, returns the constant which must
  287. be emitted)
  288. }
  289. procedure optimize_op_const(var op: topcg; var a : tcgint);virtual;
  290. {#
  291. This routine is used in exception management nodes. It should
  292. save the exception reason currently in the FUNCTION_RETURN_REG. The
  293. save should be done either to a temp (pointed to by href).
  294. or on the stack (pushing the value on the stack).
  295. The size of the value to save is OS_S32. The default version
  296. saves the exception reason to a temp. memory area.
  297. }
  298. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  299. {#
  300. This routine is used in exception management nodes. It should
  301. save the exception reason constant. The
  302. save should be done either to a temp (pointed to by href).
  303. or on the stack (pushing the value on the stack).
  304. The size of the value to save is OS_S32. The default version
  305. saves the exception reason to a temp. memory area.
  306. }
  307. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);virtual;
  308. {#
  309. This routine is used in exception management nodes. It should
  310. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  311. should either be in the temp. area (pointed to by href , href should
  312. *NOT* be freed) or on the stack (the value should be popped).
  313. The size of the value to save is OS_S32. The default version
  314. saves the exception reason to a temp. memory area.
  315. }
  316. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  317. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  318. {# This should emit the opcode to copy len bytes from the source
  319. to destination.
  320. It must be overridden for each new target processor.
  321. @param(source Source reference of copy)
  322. @param(dest Destination reference of copy)
  323. }
  324. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  325. {# This should emit the opcode to copy len bytes from the an unaligned source
  326. to destination.
  327. It must be overridden for each new target processor.
  328. @param(source Source reference of copy)
  329. @param(dest Destination reference of copy)
  330. }
  331. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  332. {# Generates overflow checking code for a node }
  333. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  334. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  335. {# Emits instructions when compilation is done in profile
  336. mode (this is set as a command line option). The default
  337. behavior does nothing, should be overridden as required.
  338. }
  339. procedure g_profilecode(list : TAsmList);virtual;
  340. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  341. @param(size Number of bytes to allocate)
  342. }
  343. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  344. {# Emits instruction for allocating the locals in entry
  345. code of a routine. This is one of the first
  346. routine called in @var(genentrycode).
  347. @param(localsize Number of bytes to allocate as locals)
  348. }
  349. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  350. {# Emits instructions for returning from a subroutine.
  351. Should also restore the framepointer and stack.
  352. @param(parasize Number of bytes of parameters to deallocate from stack)
  353. }
  354. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  355. {# This routine is called when generating the code for the entry point
  356. of a routine. It should save all registers which are not used in this
  357. routine, and which should be declared as saved in the std_saved_registers
  358. set.
  359. This routine is mainly used when linking to code which is generated
  360. by ABI-compliant compilers (like GCC), to make sure that the reserved
  361. registers of that ABI are not clobbered.
  362. @param(usedinproc Registers which are used in the code of this routine)
  363. }
  364. procedure g_save_registers(list:TAsmList);virtual;
  365. {# This routine is called when generating the code for the exit point
  366. of a routine. It should restore all registers which were previously
  367. saved in @var(g_save_standard_registers).
  368. @param(usedinproc Registers which are used in the code of this routine)
  369. }
  370. procedure g_restore_registers(list:TAsmList);virtual;
  371. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  372. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  373. { generate a stub which only purpose is to pass control the given external method,
  374. setting up any additional environment before doing so (if required).
  375. The default implementation issues a jump instruction to the external name. }
  376. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  377. { initialize the pic/got register }
  378. procedure g_maybe_got_init(list: TAsmList); virtual;
  379. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  380. procedure g_call(list: TAsmList; const s: string);
  381. { Generate code to exit an unwind-protected region. The default implementation
  382. produces a simple jump to destination label. }
  383. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;
  384. protected
  385. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual;
  386. end;
  387. {$ifdef cpu64bitalu}
  388. { This class implements an abstract code generator class
  389. for 128 Bit operations, it applies currently only to 64 Bit CPUs and supports only simple operations
  390. }
  391. tcg128 = class
  392. procedure a_load128_reg_reg(list : TAsmList;regsrc,regdst : tregister128);virtual;
  393. procedure a_load128_reg_ref(list : TAsmList;reg : tregister128;const ref : treference);virtual;
  394. procedure a_load128_ref_reg(list : TAsmList;const ref : treference;reg : tregister128);virtual;
  395. procedure a_load128_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;
  396. procedure a_load128_reg_loc(list : TAsmList;reg : tregister128;const l : tlocation);virtual;
  397. procedure a_load128_const_reg(list : TAsmList;valuelo,valuehi : int64;reg : tregister128);virtual;
  398. procedure a_load128_loc_cgpara(list : TAsmList;const l : tlocation;const paraloc : TCGPara);virtual;
  399. procedure a_load128_ref_cgpara(list: TAsmList; const r: treference;const paraloc: tcgpara);
  400. procedure a_load128_reg_cgpara(list: TAsmList; reg: tregister128;const paraloc: tcgpara);
  401. end;
  402. { Creates a tregister128 record from 2 64 Bit registers. }
  403. function joinreg128(reglo,reghi : tregister) : tregister128;
  404. {$else cpu64bitalu}
  405. {# @abstract(Abstract code generator for 64 Bit operations)
  406. This class implements an abstract code generator class
  407. for 64 Bit operations.
  408. }
  409. tcg64 = class
  410. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  411. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  412. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  413. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  414. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  415. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  416. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  417. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  418. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  419. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  420. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  421. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  422. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  423. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  424. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  425. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  426. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  427. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  428. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  429. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  430. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  431. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  432. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  433. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  434. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  435. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  436. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  437. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  438. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  439. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  440. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  441. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  442. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  443. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  444. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  445. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  446. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  447. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  448. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  449. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  450. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  451. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  452. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  453. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  454. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  455. {
  456. This routine tries to optimize the const_reg opcode, and should be
  457. called at the start of a_op64_const_reg. It returns the actual opcode
  458. to emit, and the constant value to emit. If this routine returns
  459. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  460. @param(op The opcode to emit, returns the opcode which must be emitted)
  461. @param(a The constant which should be emitted, returns the constant which must
  462. be emitted)
  463. @param(reg The register to emit the opcode with, returns the register with
  464. which the opcode will be emitted)
  465. }
  466. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  467. { override to catch 64bit rangechecks }
  468. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  469. end;
  470. {$endif cpu64bitalu}
  471. var
  472. { Main code generator class }
  473. cg : tcg;
  474. {$ifdef cpu64bitalu}
  475. { Code generator class for all operations working with 128-Bit operands }
  476. cg128 : tcg128;
  477. {$else cpu64bitalu}
  478. { Code generator class for all operations working with 64-Bit operands }
  479. cg64 : tcg64;
  480. {$endif cpu64bitalu}
  481. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  482. procedure destroy_codegen;
  483. implementation
  484. uses
  485. globals,systems,
  486. verbose,paramgr,symtable,symsym,
  487. tgobj,cutils,procinfo;
  488. {*****************************************************************************
  489. basic functionallity
  490. ******************************************************************************}
  491. constructor tcg.create;
  492. begin
  493. end;
  494. {*****************************************************************************
  495. register allocation
  496. ******************************************************************************}
  497. procedure tcg.init_register_allocators;
  498. begin
  499. fillchar(rg,sizeof(rg),0);
  500. add_reg_instruction_hook:=@add_reg_instruction;
  501. executionweight:=1;
  502. end;
  503. procedure tcg.done_register_allocators;
  504. begin
  505. { Safety }
  506. fillchar(rg,sizeof(rg),0);
  507. add_reg_instruction_hook:=nil;
  508. end;
  509. {$ifdef flowgraph}
  510. procedure Tcg.init_flowgraph;
  511. begin
  512. aktflownode:=0;
  513. end;
  514. procedure Tcg.done_flowgraph;
  515. begin
  516. end;
  517. {$endif}
  518. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  519. begin
  520. if not assigned(rg[R_INTREGISTER]) then
  521. internalerror(200312122);
  522. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  523. end;
  524. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  525. begin
  526. if not assigned(rg[R_FPUREGISTER]) then
  527. internalerror(200312123);
  528. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  529. end;
  530. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  531. begin
  532. if not assigned(rg[R_MMREGISTER]) then
  533. internalerror(2003121214);
  534. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  535. end;
  536. function tcg.getaddressregister(list:TAsmList):Tregister;
  537. begin
  538. if assigned(rg[R_ADDRESSREGISTER]) then
  539. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  540. else
  541. begin
  542. if not assigned(rg[R_INTREGISTER]) then
  543. internalerror(200312121);
  544. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  545. end;
  546. end;
  547. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  548. var
  549. subreg:Tsubregister;
  550. begin
  551. subreg:=cgsize2subreg(getregtype(reg),size);
  552. result:=reg;
  553. setsubreg(result,subreg);
  554. { notify RA }
  555. if result<>reg then
  556. list.concat(tai_regalloc.resize(result));
  557. end;
  558. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  559. begin
  560. if not assigned(rg[getregtype(r)]) then
  561. internalerror(200312125);
  562. rg[getregtype(r)].getcpuregister(list,r);
  563. end;
  564. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  565. begin
  566. if not assigned(rg[getregtype(r)]) then
  567. internalerror(200312126);
  568. rg[getregtype(r)].ungetcpuregister(list,r);
  569. end;
  570. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  571. begin
  572. if assigned(rg[rt]) then
  573. rg[rt].alloccpuregisters(list,r)
  574. else
  575. internalerror(200310092);
  576. end;
  577. procedure tcg.allocallcpuregisters(list:TAsmList);
  578. begin
  579. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  580. {$if not(defined(i386)) and not(defined(avr))}
  581. if uses_registers(R_FPUREGISTER) then
  582. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  583. {$ifdef cpumm}
  584. if uses_registers(R_MMREGISTER) then
  585. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  586. {$endif cpumm}
  587. {$endif not(defined(i386)) and not(defined(avr))}
  588. end;
  589. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  590. begin
  591. if assigned(rg[rt]) then
  592. rg[rt].dealloccpuregisters(list,r)
  593. else
  594. internalerror(200310093);
  595. end;
  596. procedure tcg.deallocallcpuregisters(list:TAsmList);
  597. begin
  598. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  599. {$if not(defined(i386)) and not(defined(avr))}
  600. if uses_registers(R_FPUREGISTER) then
  601. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  602. {$ifdef cpumm}
  603. if uses_registers(R_MMREGISTER) then
  604. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  605. {$endif cpumm}
  606. {$endif not(defined(i386)) and not(defined(avr))}
  607. end;
  608. function tcg.uses_registers(rt:Tregistertype):boolean;
  609. begin
  610. if assigned(rg[rt]) then
  611. result:=rg[rt].uses_registers
  612. else
  613. result:=false;
  614. end;
  615. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  616. var
  617. rt : tregistertype;
  618. begin
  619. rt:=getregtype(r);
  620. { Only add it when a register allocator is configured.
  621. No IE can be generated, because the VMT is written
  622. without a valid rg[] }
  623. if assigned(rg[rt]) then
  624. rg[rt].add_reg_instruction(instr,r,cg.executionweight);
  625. end;
  626. procedure tcg.add_move_instruction(instr:Taicpu);
  627. var
  628. rt : tregistertype;
  629. begin
  630. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  631. if assigned(rg[rt]) then
  632. rg[rt].add_move_instruction(instr)
  633. else
  634. internalerror(200310095);
  635. end;
  636. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  637. var
  638. rt : tregistertype;
  639. begin
  640. for rt:=low(rg) to high(rg) do
  641. begin
  642. if assigned(rg[rt]) then
  643. rg[rt].live_range_direction:=dir;
  644. end;
  645. end;
  646. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  647. var
  648. rt : tregistertype;
  649. begin
  650. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  651. begin
  652. if assigned(rg[rt]) then
  653. rg[rt].do_register_allocation(list,headertai);
  654. end;
  655. { running the other register allocator passes could require addition int/addr. registers
  656. when spilling so run int/addr register allocation at the end }
  657. if assigned(rg[R_INTREGISTER]) then
  658. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  659. if assigned(rg[R_ADDRESSREGISTER]) then
  660. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  661. end;
  662. procedure tcg.translate_register(var reg : tregister);
  663. begin
  664. rg[getregtype(reg)].translate_register(reg);
  665. end;
  666. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  667. begin
  668. list.concat(tai_regalloc.alloc(r,nil));
  669. end;
  670. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  671. begin
  672. list.concat(tai_regalloc.dealloc(r,nil));
  673. end;
  674. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  675. var
  676. instr : tai;
  677. begin
  678. instr:=tai_regalloc.sync(r);
  679. list.concat(instr);
  680. add_reg_instruction(instr,r);
  681. end;
  682. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  683. begin
  684. list.concat(tai_label.create(l));
  685. end;
  686. {*****************************************************************************
  687. for better code generation these methods should be overridden
  688. ******************************************************************************}
  689. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  690. var
  691. ref : treference;
  692. tmpreg : tregister;
  693. begin
  694. cgpara.check_simple_location;
  695. paramanager.alloccgpara(list,cgpara);
  696. if cgpara.location^.shiftval<0 then
  697. begin
  698. tmpreg:=getintregister(list,cgpara.location^.size);
  699. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.size,-cgpara.location^.shiftval,r,tmpreg);
  700. r:=tmpreg;
  701. end;
  702. case cgpara.location^.loc of
  703. LOC_REGISTER,LOC_CREGISTER:
  704. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  705. LOC_REFERENCE,LOC_CREFERENCE:
  706. begin
  707. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  708. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  709. end;
  710. LOC_MMREGISTER,LOC_CMMREGISTER:
  711. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  712. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  713. begin
  714. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  715. a_load_reg_ref(list,size,size,r,ref);
  716. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  717. tg.Ungettemp(list,ref);
  718. end
  719. else
  720. internalerror(2002071004);
  721. end;
  722. end;
  723. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  724. var
  725. ref : treference;
  726. begin
  727. cgpara.check_simple_location;
  728. paramanager.alloccgpara(list,cgpara);
  729. if cgpara.location^.shiftval<0 then
  730. a:=a shl -cgpara.location^.shiftval;
  731. case cgpara.location^.loc of
  732. LOC_REGISTER,LOC_CREGISTER:
  733. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  734. LOC_REFERENCE,LOC_CREFERENCE:
  735. begin
  736. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  737. a_load_const_ref(list,cgpara.location^.size,a,ref);
  738. end
  739. else
  740. internalerror(2010053109);
  741. end;
  742. end;
  743. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  744. var
  745. tmpref, ref: treference;
  746. tmpreg: tregister;
  747. location: pcgparalocation;
  748. orgsizeleft,
  749. sizeleft: tcgint;
  750. reghasvalue: boolean;
  751. begin
  752. location:=cgpara.location;
  753. tmpref:=r;
  754. sizeleft:=cgpara.intsize;
  755. while assigned(location) do
  756. begin
  757. paramanager.allocparaloc(list,location);
  758. case location^.loc of
  759. LOC_REGISTER,LOC_CREGISTER:
  760. begin
  761. { Parameter locations are often allocated in multiples of
  762. entire registers. If a parameter only occupies a part of
  763. such a register (e.g. a 16 bit int on a 32 bit
  764. architecture), the size of this parameter can only be
  765. determined by looking at the "size" parameter of this
  766. method -> if the size parameter is <= sizeof(aint), then
  767. we check that there is only one parameter location and
  768. then use this "size" to load the value into the parameter
  769. location }
  770. if (size<>OS_NO) and
  771. (tcgsize2size[size]<=sizeof(aint)) then
  772. begin
  773. cgpara.check_simple_location;
  774. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  775. if location^.shiftval<0 then
  776. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  777. end
  778. { there's a lot more data left, and the current paraloc's
  779. register is entirely filled with part of that data }
  780. else if (sizeleft>sizeof(aint)) then
  781. begin
  782. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  783. end
  784. { we're at the end of the data, and it can be loaded into
  785. the current location's register with a single regular
  786. load }
  787. else if (sizeleft in [1,2{$ifndef cpu16bitalu},4{$endif}{$ifdef cpu64bitalu},8{$endif}]) then
  788. begin
  789. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  790. if location^.shiftval<0 then
  791. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  792. end
  793. { we're at the end of the data, and we need multiple loads
  794. to get it in the register because it's an irregular size }
  795. else
  796. begin
  797. { should be the last part }
  798. if assigned(location^.next) then
  799. internalerror(2010052907);
  800. { load the value piecewise to get it into the register }
  801. orgsizeleft:=sizeleft;
  802. reghasvalue:=false;
  803. {$ifdef cpu64bitalu}
  804. if sizeleft>=4 then
  805. begin
  806. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  807. dec(sizeleft,4);
  808. if target_info.endian=endian_big then
  809. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  810. inc(tmpref.offset,4);
  811. reghasvalue:=true;
  812. end;
  813. {$endif cpu64bitalu}
  814. if sizeleft>=2 then
  815. begin
  816. tmpreg:=getintregister(list,location^.size);
  817. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  818. dec(sizeleft,2);
  819. if reghasvalue then
  820. begin
  821. if target_info.endian=endian_big then
  822. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  823. else
  824. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  825. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  826. end
  827. else
  828. begin
  829. if target_info.endian=endian_big then
  830. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  831. else
  832. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  833. end;
  834. inc(tmpref.offset,2);
  835. reghasvalue:=true;
  836. end;
  837. if sizeleft=1 then
  838. begin
  839. tmpreg:=getintregister(list,location^.size);
  840. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  841. dec(sizeleft,1);
  842. if reghasvalue then
  843. begin
  844. if target_info.endian=endian_little then
  845. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  846. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  847. end
  848. else
  849. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  850. inc(tmpref.offset);
  851. end;
  852. if location^.shiftval<0 then
  853. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  854. { the loop will already adjust the offset and sizeleft }
  855. dec(tmpref.offset,orgsizeleft);
  856. sizeleft:=orgsizeleft;
  857. end;
  858. end;
  859. LOC_REFERENCE,LOC_CREFERENCE:
  860. begin
  861. if assigned(location^.next) then
  862. internalerror(2010052906);
  863. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  864. if (size <> OS_NO) and
  865. (tcgsize2size[size] <= sizeof(aint)) then
  866. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  867. else
  868. { use concatcopy, because the parameter can be larger than }
  869. { what the OS_* constants can handle }
  870. g_concatcopy(list,tmpref,ref,sizeleft);
  871. end;
  872. LOC_MMREGISTER,LOC_CMMREGISTER:
  873. begin
  874. case location^.size of
  875. OS_F32,
  876. OS_F64,
  877. OS_F128:
  878. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  879. OS_M8..OS_M128,
  880. OS_MS8..OS_MS128:
  881. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  882. else
  883. internalerror(2010053101);
  884. end;
  885. end
  886. else
  887. internalerror(2010053111);
  888. end;
  889. inc(tmpref.offset,tcgsize2size[location^.size]);
  890. dec(sizeleft,tcgsize2size[location^.size]);
  891. location:=location^.next;
  892. end;
  893. end;
  894. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  895. begin
  896. case l.loc of
  897. LOC_REGISTER,
  898. LOC_CREGISTER :
  899. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  900. LOC_CONSTANT :
  901. a_load_const_cgpara(list,l.size,l.value,cgpara);
  902. LOC_CREFERENCE,
  903. LOC_REFERENCE :
  904. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  905. else
  906. internalerror(2002032211);
  907. end;
  908. end;
  909. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  910. var
  911. hr : tregister;
  912. begin
  913. cgpara.check_simple_location;
  914. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  915. begin
  916. paramanager.allocparaloc(list,cgpara.location);
  917. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  918. end
  919. else
  920. begin
  921. hr:=getaddressregister(list);
  922. a_loadaddr_ref_reg(list,r,hr);
  923. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  924. end;
  925. end;
  926. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  927. var
  928. href : treference;
  929. hreg : tregister;
  930. cgsize: tcgsize;
  931. begin
  932. case paraloc.loc of
  933. LOC_REGISTER :
  934. begin
  935. hreg:=paraloc.register;
  936. cgsize:=paraloc.size;
  937. if paraloc.shiftval>0 then
  938. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  939. else if (paraloc.shiftval<0) and
  940. (sizeleft in [1,2,4]) then
  941. begin
  942. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  943. { convert to a register of 1/2/4 bytes in size, since the
  944. original register had to be made larger to be able to hold
  945. the shifted value }
  946. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  947. hreg:=getintregister(list,cgsize);
  948. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  949. end;
  950. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref);
  951. end;
  952. LOC_MMREGISTER :
  953. begin
  954. case paraloc.size of
  955. OS_F32,
  956. OS_F64,
  957. OS_F128:
  958. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  959. OS_M8..OS_M128,
  960. OS_MS8..OS_MS128:
  961. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  962. else
  963. internalerror(2010053102);
  964. end;
  965. end;
  966. LOC_FPUREGISTER :
  967. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  968. LOC_REFERENCE :
  969. begin
  970. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  971. { use concatcopy, because it can also be a float which fails when
  972. load_ref_ref is used. Don't copy data when the references are equal }
  973. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  974. g_concatcopy(list,href,ref,sizeleft);
  975. end;
  976. else
  977. internalerror(2002081302);
  978. end;
  979. end;
  980. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  981. var
  982. href : treference;
  983. begin
  984. case paraloc.loc of
  985. LOC_REGISTER :
  986. begin
  987. if paraloc.shiftval<0 then
  988. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  989. case getregtype(reg) of
  990. R_ADDRESSREGISTER,
  991. R_INTREGISTER:
  992. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  993. R_MMREGISTER:
  994. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  995. else
  996. internalerror(2009112422);
  997. end;
  998. end;
  999. LOC_MMREGISTER :
  1000. begin
  1001. case getregtype(reg) of
  1002. R_ADDRESSREGISTER,
  1003. R_INTREGISTER:
  1004. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1005. R_MMREGISTER:
  1006. begin
  1007. case paraloc.size of
  1008. OS_F32,
  1009. OS_F64,
  1010. OS_F128:
  1011. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1012. OS_M8..OS_M128,
  1013. OS_MS8..OS_MS128:
  1014. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1015. else
  1016. internalerror(2010053102);
  1017. end;
  1018. end;
  1019. else
  1020. internalerror(2010053104);
  1021. end;
  1022. end;
  1023. LOC_FPUREGISTER :
  1024. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1025. LOC_REFERENCE :
  1026. begin
  1027. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1028. case getregtype(reg) of
  1029. R_ADDRESSREGISTER,
  1030. R_INTREGISTER :
  1031. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1032. R_FPUREGISTER :
  1033. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1034. R_MMREGISTER :
  1035. { not paraloc.size, because it may be OS_64 instead of
  1036. OS_F64 in case the parameter is passed using integer
  1037. conventions (e.g., on ARM) }
  1038. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1039. else
  1040. internalerror(2004101012);
  1041. end;
  1042. end;
  1043. else
  1044. internalerror(2002081302);
  1045. end;
  1046. end;
  1047. {****************************************************************************
  1048. some generic implementations
  1049. ****************************************************************************}
  1050. { memory/register loading }
  1051. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1052. var
  1053. tmpref : treference;
  1054. tmpreg : tregister;
  1055. i : longint;
  1056. begin
  1057. if ref.alignment<tcgsize2size[fromsize] then
  1058. begin
  1059. tmpref:=ref;
  1060. { we take care of the alignment now }
  1061. tmpref.alignment:=0;
  1062. case FromSize of
  1063. OS_16,OS_S16:
  1064. begin
  1065. tmpreg:=getintregister(list,OS_16);
  1066. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1067. if target_info.endian=endian_big then
  1068. inc(tmpref.offset);
  1069. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1070. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1071. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1072. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1073. if target_info.endian=endian_big then
  1074. dec(tmpref.offset)
  1075. else
  1076. inc(tmpref.offset);
  1077. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1078. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1079. end;
  1080. OS_32,OS_S32:
  1081. begin
  1082. { could add an optimised case for ref.alignment=2 }
  1083. tmpreg:=getintregister(list,OS_32);
  1084. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1085. if target_info.endian=endian_big then
  1086. inc(tmpref.offset,3);
  1087. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1088. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1089. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1090. for i:=1 to 3 do
  1091. begin
  1092. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1093. if target_info.endian=endian_big then
  1094. dec(tmpref.offset)
  1095. else
  1096. inc(tmpref.offset);
  1097. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1098. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1099. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1100. end;
  1101. end
  1102. else
  1103. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1104. end;
  1105. end
  1106. else
  1107. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1108. end;
  1109. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1110. var
  1111. tmpref : treference;
  1112. tmpreg,
  1113. tmpreg2 : tregister;
  1114. i : longint;
  1115. begin
  1116. if ref.alignment in [1,2] then
  1117. begin
  1118. tmpref:=ref;
  1119. { we take care of the alignment now }
  1120. tmpref.alignment:=0;
  1121. case FromSize of
  1122. OS_16,OS_S16:
  1123. if ref.alignment=2 then
  1124. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1125. else
  1126. begin
  1127. { first load in tmpreg, because the target register }
  1128. { may be used in ref as well }
  1129. if target_info.endian=endian_little then
  1130. inc(tmpref.offset);
  1131. tmpreg:=getintregister(list,OS_8);
  1132. a_load_ref_reg(list,OS_8,OS_8,tmpref,tmpreg);
  1133. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1134. a_op_const_reg(list,OP_SHL,OS_16,8,tmpreg);
  1135. if target_info.endian=endian_little then
  1136. dec(tmpref.offset)
  1137. else
  1138. inc(tmpref.offset);
  1139. a_load_ref_reg(list,OS_8,OS_16,tmpref,register);
  1140. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,register);
  1141. end;
  1142. OS_32,OS_S32:
  1143. if ref.alignment=2 then
  1144. begin
  1145. if target_info.endian=endian_little then
  1146. inc(tmpref.offset,2);
  1147. tmpreg:=getintregister(list,OS_32);
  1148. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1149. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1150. if target_info.endian=endian_little then
  1151. dec(tmpref.offset,2)
  1152. else
  1153. inc(tmpref.offset,2);
  1154. a_load_ref_reg(list,OS_16,OS_32,tmpref,register);
  1155. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,register);
  1156. end
  1157. else
  1158. begin
  1159. if target_info.endian=endian_little then
  1160. inc(tmpref.offset,3);
  1161. tmpreg:=getintregister(list,OS_32);
  1162. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1163. tmpreg2:=getintregister(list,OS_32);
  1164. for i:=1 to 3 do
  1165. begin
  1166. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1167. if target_info.endian=endian_little then
  1168. dec(tmpref.offset)
  1169. else
  1170. inc(tmpref.offset);
  1171. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1172. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1173. end;
  1174. a_load_reg_reg(list,OS_32,OS_32,tmpreg,register);
  1175. end
  1176. else
  1177. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1178. end;
  1179. end
  1180. else
  1181. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1182. end;
  1183. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1184. var
  1185. tmpreg: tregister;
  1186. begin
  1187. { verify if we have the same reference }
  1188. if references_equal(sref,dref) then
  1189. exit;
  1190. tmpreg:=getintregister(list,tosize);
  1191. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1192. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1193. end;
  1194. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1195. var
  1196. tmpreg: tregister;
  1197. begin
  1198. tmpreg:=getintregister(list,size);
  1199. a_load_const_reg(list,size,a,tmpreg);
  1200. a_load_reg_ref(list,size,size,tmpreg,ref);
  1201. end;
  1202. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1203. begin
  1204. case loc.loc of
  1205. LOC_REFERENCE,LOC_CREFERENCE:
  1206. a_load_const_ref(list,loc.size,a,loc.reference);
  1207. LOC_REGISTER,LOC_CREGISTER:
  1208. a_load_const_reg(list,loc.size,a,loc.register);
  1209. else
  1210. internalerror(200203272);
  1211. end;
  1212. end;
  1213. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1214. begin
  1215. case loc.loc of
  1216. LOC_REFERENCE,LOC_CREFERENCE:
  1217. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1218. LOC_REGISTER,LOC_CREGISTER:
  1219. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1220. LOC_MMREGISTER,LOC_CMMREGISTER:
  1221. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1222. else
  1223. internalerror(200203271);
  1224. end;
  1225. end;
  1226. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1227. begin
  1228. case loc.loc of
  1229. LOC_REFERENCE,LOC_CREFERENCE:
  1230. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1231. LOC_REGISTER,LOC_CREGISTER:
  1232. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1233. LOC_CONSTANT:
  1234. a_load_const_reg(list,tosize,loc.value,reg);
  1235. else
  1236. internalerror(200109092);
  1237. end;
  1238. end;
  1239. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1240. begin
  1241. case loc.loc of
  1242. LOC_REFERENCE,LOC_CREFERENCE:
  1243. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1244. LOC_REGISTER,LOC_CREGISTER:
  1245. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1246. LOC_CONSTANT:
  1247. a_load_const_ref(list,tosize,loc.value,ref);
  1248. else
  1249. internalerror(200109302);
  1250. end;
  1251. end;
  1252. procedure tcg.optimize_op_const(var op: topcg; var a : tcgint);
  1253. var
  1254. powerval : longint;
  1255. begin
  1256. case op of
  1257. OP_OR :
  1258. begin
  1259. { or with zero returns same result }
  1260. if a = 0 then
  1261. op:=OP_NONE
  1262. else
  1263. { or with max returns max }
  1264. if a = -1 then
  1265. op:=OP_MOVE;
  1266. end;
  1267. OP_AND :
  1268. begin
  1269. { and with max returns same result }
  1270. if (a = -1) then
  1271. op:=OP_NONE
  1272. else
  1273. { and with 0 returns 0 }
  1274. if a=0 then
  1275. op:=OP_MOVE;
  1276. end;
  1277. OP_DIV :
  1278. begin
  1279. { division by 1 returns result }
  1280. if a = 1 then
  1281. op:=OP_NONE
  1282. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1283. begin
  1284. a := powerval;
  1285. op:= OP_SHR;
  1286. end;
  1287. end;
  1288. OP_IDIV:
  1289. begin
  1290. if a = 1 then
  1291. op:=OP_NONE;
  1292. end;
  1293. OP_MUL,OP_IMUL:
  1294. begin
  1295. if a = 1 then
  1296. op:=OP_NONE
  1297. else
  1298. if a=0 then
  1299. op:=OP_MOVE
  1300. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1301. begin
  1302. a := powerval;
  1303. op:= OP_SHL;
  1304. end;
  1305. end;
  1306. OP_ADD,OP_SUB:
  1307. begin
  1308. if a = 0 then
  1309. op:=OP_NONE;
  1310. end;
  1311. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  1312. begin
  1313. if a = 0 then
  1314. op:=OP_NONE;
  1315. end;
  1316. end;
  1317. end;
  1318. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1319. begin
  1320. case loc.loc of
  1321. LOC_REFERENCE, LOC_CREFERENCE:
  1322. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1323. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1324. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1325. else
  1326. internalerror(200203301);
  1327. end;
  1328. end;
  1329. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1330. begin
  1331. case loc.loc of
  1332. LOC_REFERENCE, LOC_CREFERENCE:
  1333. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1334. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1335. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1336. else
  1337. internalerror(48991);
  1338. end;
  1339. end;
  1340. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1341. var
  1342. reg: tregister;
  1343. regsize: tcgsize;
  1344. begin
  1345. if (fromsize>=tosize) then
  1346. regsize:=fromsize
  1347. else
  1348. regsize:=tosize;
  1349. reg:=getfpuregister(list,regsize);
  1350. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1351. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1352. end;
  1353. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1354. var
  1355. ref : treference;
  1356. begin
  1357. paramanager.alloccgpara(list,cgpara);
  1358. case cgpara.location^.loc of
  1359. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1360. begin
  1361. cgpara.check_simple_location;
  1362. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1363. end;
  1364. LOC_REFERENCE,LOC_CREFERENCE:
  1365. begin
  1366. cgpara.check_simple_location;
  1367. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1368. a_loadfpu_reg_ref(list,size,size,r,ref);
  1369. end;
  1370. LOC_REGISTER,LOC_CREGISTER:
  1371. begin
  1372. { paramfpu_ref does the check_simpe_location check here if necessary }
  1373. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1374. a_loadfpu_reg_ref(list,size,size,r,ref);
  1375. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1376. tg.Ungettemp(list,ref);
  1377. end;
  1378. else
  1379. internalerror(2010053112);
  1380. end;
  1381. end;
  1382. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1383. var
  1384. href : treference;
  1385. hsize: tcgsize;
  1386. begin
  1387. case cgpara.location^.loc of
  1388. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1389. begin
  1390. cgpara.check_simple_location;
  1391. paramanager.alloccgpara(list,cgpara);
  1392. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  1393. end;
  1394. LOC_REFERENCE,LOC_CREFERENCE:
  1395. begin
  1396. cgpara.check_simple_location;
  1397. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1398. { concatcopy should choose the best way to copy the data }
  1399. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1400. end;
  1401. LOC_REGISTER,LOC_CREGISTER:
  1402. begin
  1403. { force integer size }
  1404. hsize:=int_cgsize(tcgsize2size[size]);
  1405. {$ifndef cpu64bitalu}
  1406. if (hsize in [OS_S64,OS_64]) then
  1407. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1408. else
  1409. {$endif not cpu64bitalu}
  1410. begin
  1411. cgpara.check_simple_location;
  1412. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1413. end;
  1414. end
  1415. else
  1416. internalerror(200402201);
  1417. end;
  1418. end;
  1419. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1420. var
  1421. tmpreg : tregister;
  1422. begin
  1423. tmpreg:=getintregister(list,size);
  1424. a_load_ref_reg(list,size,size,ref,tmpreg);
  1425. a_op_const_reg(list,op,size,a,tmpreg);
  1426. a_load_reg_ref(list,size,size,tmpreg,ref);
  1427. end;
  1428. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1429. begin
  1430. case loc.loc of
  1431. LOC_REGISTER, LOC_CREGISTER:
  1432. a_op_const_reg(list,op,loc.size,a,loc.register);
  1433. LOC_REFERENCE, LOC_CREFERENCE:
  1434. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1435. else
  1436. internalerror(200109061);
  1437. end;
  1438. end;
  1439. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1440. var
  1441. tmpreg : tregister;
  1442. begin
  1443. tmpreg:=getintregister(list,size);
  1444. a_load_ref_reg(list,size,size,ref,tmpreg);
  1445. a_op_reg_reg(list,op,size,reg,tmpreg);
  1446. a_load_reg_ref(list,size,size,tmpreg,ref);
  1447. end;
  1448. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1449. var
  1450. tmpreg: tregister;
  1451. begin
  1452. case op of
  1453. OP_NOT,OP_NEG:
  1454. { handle it as "load ref,reg; op reg" }
  1455. begin
  1456. a_load_ref_reg(list,size,size,ref,reg);
  1457. a_op_reg_reg(list,op,size,reg,reg);
  1458. end;
  1459. else
  1460. begin
  1461. tmpreg:=getintregister(list,size);
  1462. a_load_ref_reg(list,size,size,ref,tmpreg);
  1463. a_op_reg_reg(list,op,size,tmpreg,reg);
  1464. end;
  1465. end;
  1466. end;
  1467. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1468. begin
  1469. case loc.loc of
  1470. LOC_REGISTER, LOC_CREGISTER:
  1471. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1472. LOC_REFERENCE, LOC_CREFERENCE:
  1473. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1474. else
  1475. internalerror(200109061);
  1476. end;
  1477. end;
  1478. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1479. var
  1480. tmpreg: tregister;
  1481. begin
  1482. case loc.loc of
  1483. LOC_REGISTER,LOC_CREGISTER:
  1484. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1485. LOC_REFERENCE,LOC_CREFERENCE:
  1486. begin
  1487. tmpreg:=getintregister(list,loc.size);
  1488. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1489. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1490. end;
  1491. else
  1492. internalerror(200109061);
  1493. end;
  1494. end;
  1495. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1496. a:tcgint;src,dst:Tregister);
  1497. begin
  1498. a_load_reg_reg(list,size,size,src,dst);
  1499. a_op_const_reg(list,op,size,a,dst);
  1500. end;
  1501. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1502. size: tcgsize; src1, src2, dst: tregister);
  1503. var
  1504. tmpreg: tregister;
  1505. begin
  1506. if (dst<>src1) then
  1507. begin
  1508. a_load_reg_reg(list,size,size,src2,dst);
  1509. a_op_reg_reg(list,op,size,src1,dst);
  1510. end
  1511. else
  1512. begin
  1513. { can we do a direct operation on the target register ? }
  1514. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1515. a_op_reg_reg(list,op,size,src2,dst)
  1516. else
  1517. begin
  1518. tmpreg:=getintregister(list,size);
  1519. a_load_reg_reg(list,size,size,src2,tmpreg);
  1520. a_op_reg_reg(list,op,size,src1,tmpreg);
  1521. a_load_reg_reg(list,size,size,tmpreg,dst);
  1522. end;
  1523. end;
  1524. end;
  1525. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1526. begin
  1527. a_op_const_reg_reg(list,op,size,a,src,dst);
  1528. ovloc.loc:=LOC_VOID;
  1529. end;
  1530. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1531. begin
  1532. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1533. ovloc.loc:=LOC_VOID;
  1534. end;
  1535. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1536. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1537. var
  1538. tmpreg: tregister;
  1539. begin
  1540. tmpreg:=getintregister(list,size);
  1541. a_load_const_reg(list,size,a,tmpreg);
  1542. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1543. end;
  1544. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1545. l : tasmlabel);
  1546. var
  1547. tmpreg: tregister;
  1548. begin
  1549. tmpreg:=getintregister(list,size);
  1550. a_load_ref_reg(list,size,size,ref,tmpreg);
  1551. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1552. end;
  1553. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1554. l : tasmlabel);
  1555. begin
  1556. case loc.loc of
  1557. LOC_REGISTER,LOC_CREGISTER:
  1558. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1559. LOC_REFERENCE,LOC_CREFERENCE:
  1560. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1561. else
  1562. internalerror(200109061);
  1563. end;
  1564. end;
  1565. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1566. var
  1567. tmpreg: tregister;
  1568. begin
  1569. tmpreg:=getintregister(list,size);
  1570. a_load_ref_reg(list,size,size,ref,tmpreg);
  1571. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1572. end;
  1573. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1574. var
  1575. tmpreg: tregister;
  1576. begin
  1577. tmpreg:=getintregister(list,size);
  1578. a_load_ref_reg(list,size,size,ref,tmpreg);
  1579. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1580. end;
  1581. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1582. begin
  1583. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1584. end;
  1585. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1586. begin
  1587. case loc.loc of
  1588. LOC_REGISTER,
  1589. LOC_CREGISTER:
  1590. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1591. LOC_REFERENCE,
  1592. LOC_CREFERENCE :
  1593. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1594. LOC_CONSTANT:
  1595. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1596. else
  1597. internalerror(200203231);
  1598. end;
  1599. end;
  1600. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1601. l : tasmlabel);
  1602. var
  1603. tmpreg: tregister;
  1604. begin
  1605. case loc.loc of
  1606. LOC_REGISTER,LOC_CREGISTER:
  1607. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1608. LOC_REFERENCE,LOC_CREFERENCE:
  1609. begin
  1610. tmpreg:=getintregister(list,size);
  1611. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1612. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1613. end;
  1614. else
  1615. internalerror(200109061);
  1616. end;
  1617. end;
  1618. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1619. begin
  1620. case loc.loc of
  1621. LOC_MMREGISTER,LOC_CMMREGISTER:
  1622. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1623. LOC_REFERENCE,LOC_CREFERENCE:
  1624. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1625. LOC_REGISTER,LOC_CREGISTER:
  1626. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1627. else
  1628. internalerror(200310121);
  1629. end;
  1630. end;
  1631. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1632. begin
  1633. case loc.loc of
  1634. LOC_MMREGISTER,LOC_CMMREGISTER:
  1635. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1636. LOC_REFERENCE,LOC_CREFERENCE:
  1637. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1638. else
  1639. internalerror(200310122);
  1640. end;
  1641. end;
  1642. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1643. var
  1644. href : treference;
  1645. {$ifndef cpu64bitalu}
  1646. tmpreg : tregister;
  1647. reg64 : tregister64;
  1648. {$endif not cpu64bitalu}
  1649. begin
  1650. {$ifndef cpu64bitalu}
  1651. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1652. (size<>OS_F64) then
  1653. {$endif not cpu64bitalu}
  1654. cgpara.check_simple_location;
  1655. paramanager.alloccgpara(list,cgpara);
  1656. case cgpara.location^.loc of
  1657. LOC_MMREGISTER,LOC_CMMREGISTER:
  1658. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1659. LOC_REFERENCE,LOC_CREFERENCE:
  1660. begin
  1661. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1662. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1663. end;
  1664. LOC_REGISTER,LOC_CREGISTER:
  1665. begin
  1666. if assigned(shuffle) and
  1667. not shufflescalar(shuffle) then
  1668. internalerror(2009112510);
  1669. {$ifndef cpu64bitalu}
  1670. if (size=OS_F64) then
  1671. begin
  1672. if not assigned(cgpara.location^.next) or
  1673. assigned(cgpara.location^.next^.next) then
  1674. internalerror(2009112512);
  1675. case cgpara.location^.next^.loc of
  1676. LOC_REGISTER,LOC_CREGISTER:
  1677. tmpreg:=cgpara.location^.next^.register;
  1678. LOC_REFERENCE,LOC_CREFERENCE:
  1679. tmpreg:=getintregister(list,OS_32);
  1680. else
  1681. internalerror(2009112910);
  1682. end;
  1683. if (target_info.endian=ENDIAN_BIG) then
  1684. begin
  1685. { paraloc^ -> high
  1686. paraloc^.next -> low }
  1687. reg64.reghi:=cgpara.location^.register;
  1688. reg64.reglo:=tmpreg;
  1689. end
  1690. else
  1691. begin
  1692. { paraloc^ -> low
  1693. paraloc^.next -> high }
  1694. reg64.reglo:=cgpara.location^.register;
  1695. reg64.reghi:=tmpreg;
  1696. end;
  1697. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  1698. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1699. begin
  1700. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  1701. internalerror(2009112911);
  1702. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  1703. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  1704. end;
  1705. end
  1706. else
  1707. {$endif not cpu64bitalu}
  1708. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  1709. end
  1710. else
  1711. internalerror(200310123);
  1712. end;
  1713. end;
  1714. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1715. var
  1716. hr : tregister;
  1717. hs : tmmshuffle;
  1718. begin
  1719. cgpara.check_simple_location;
  1720. hr:=getmmregister(list,cgpara.location^.size);
  1721. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1722. if realshuffle(shuffle) then
  1723. begin
  1724. hs:=shuffle^;
  1725. removeshuffles(hs);
  1726. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  1727. end
  1728. else
  1729. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  1730. end;
  1731. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1732. begin
  1733. case loc.loc of
  1734. LOC_MMREGISTER,LOC_CMMREGISTER:
  1735. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  1736. LOC_REFERENCE,LOC_CREFERENCE:
  1737. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  1738. else
  1739. internalerror(200310123);
  1740. end;
  1741. end;
  1742. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1743. var
  1744. hr : tregister;
  1745. hs : tmmshuffle;
  1746. begin
  1747. hr:=getmmregister(list,size);
  1748. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1749. if realshuffle(shuffle) then
  1750. begin
  1751. hs:=shuffle^;
  1752. removeshuffles(hs);
  1753. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1754. end
  1755. else
  1756. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1757. end;
  1758. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1759. var
  1760. hr : tregister;
  1761. hs : tmmshuffle;
  1762. begin
  1763. hr:=getmmregister(list,size);
  1764. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1765. if realshuffle(shuffle) then
  1766. begin
  1767. hs:=shuffle^;
  1768. removeshuffles(hs);
  1769. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1770. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1771. end
  1772. else
  1773. begin
  1774. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1775. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1776. end;
  1777. end;
  1778. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  1779. var
  1780. tmpref: treference;
  1781. begin
  1782. if (tcgsize2size[fromsize]<>4) or
  1783. (tcgsize2size[tosize]<>4) then
  1784. internalerror(2009112503);
  1785. tg.gettemp(list,4,4,tt_normal,tmpref);
  1786. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1787. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  1788. tg.ungettemp(list,tmpref);
  1789. end;
  1790. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  1791. var
  1792. tmpref: treference;
  1793. begin
  1794. if (tcgsize2size[fromsize]<>4) or
  1795. (tcgsize2size[tosize]<>4) then
  1796. internalerror(2009112504);
  1797. tg.gettemp(list,8,8,tt_normal,tmpref);
  1798. cg.a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  1799. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  1800. tg.ungettemp(list,tmpref);
  1801. end;
  1802. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1803. begin
  1804. case loc.loc of
  1805. LOC_CMMREGISTER,LOC_MMREGISTER:
  1806. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1807. LOC_CREFERENCE,LOC_REFERENCE:
  1808. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1809. else
  1810. internalerror(200312232);
  1811. end;
  1812. end;
  1813. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1814. begin
  1815. g_concatcopy(list,source,dest,len);
  1816. end;
  1817. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1818. begin
  1819. g_overflowCheck(list,loc,def);
  1820. end;
  1821. {$ifdef cpuflags}
  1822. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  1823. var
  1824. tmpreg : tregister;
  1825. begin
  1826. tmpreg:=getintregister(list,size);
  1827. g_flags2reg(list,size,f,tmpreg);
  1828. a_load_reg_ref(list,size,size,tmpreg,ref);
  1829. end;
  1830. {$endif cpuflags}
  1831. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  1832. var
  1833. hrefvmt : treference;
  1834. cgpara1,cgpara2 : TCGPara;
  1835. pd: tprocdef;
  1836. begin
  1837. cgpara1.init;
  1838. cgpara2.init;
  1839. if (cs_check_object in current_settings.localswitches) then
  1840. begin
  1841. pd:=search_system_proc('fpc_check_object_ext');
  1842. paramanager.getintparaloc(pd,1,cgpara1);
  1843. paramanager.getintparaloc(pd,2,cgpara2);
  1844. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname),0,sizeof(pint));
  1845. if pd.is_pushleftright then
  1846. begin
  1847. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1848. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1849. end
  1850. else
  1851. begin
  1852. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1853. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1854. end;
  1855. paramanager.freecgpara(list,cgpara1);
  1856. paramanager.freecgpara(list,cgpara2);
  1857. allocallcpuregisters(list);
  1858. a_call_name(list,'fpc_check_object_ext',false);
  1859. deallocallcpuregisters(list);
  1860. end
  1861. else
  1862. if (cs_check_range in current_settings.localswitches) then
  1863. begin
  1864. pd:=search_system_proc('fpc_check_object');
  1865. paramanager.getintparaloc(pd,1,cgpara1);
  1866. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1867. paramanager.freecgpara(list,cgpara1);
  1868. allocallcpuregisters(list);
  1869. a_call_name(list,'fpc_check_object',false);
  1870. deallocallcpuregisters(list);
  1871. end;
  1872. cgpara1.done;
  1873. cgpara2.done;
  1874. end;
  1875. {*****************************************************************************
  1876. Entry/Exit Code Functions
  1877. *****************************************************************************}
  1878. procedure tcg.g_save_registers(list:TAsmList);
  1879. var
  1880. href : treference;
  1881. size : longint;
  1882. r : integer;
  1883. begin
  1884. { calculate temp. size }
  1885. size:=0;
  1886. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1887. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1888. inc(size,sizeof(aint));
  1889. { mm registers }
  1890. if uses_registers(R_MMREGISTER) then
  1891. begin
  1892. { Make sure we reserve enough space to do the alignment based on the offset
  1893. later on. We can't use the size for this, because the alignment of the start
  1894. of the temp is smaller than needed for an OS_VECTOR }
  1895. inc(size,tcgsize2size[OS_VECTOR]);
  1896. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1897. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1898. inc(size,tcgsize2size[OS_VECTOR]);
  1899. end;
  1900. if size>0 then
  1901. begin
  1902. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1903. include(current_procinfo.flags,pi_has_saved_regs);
  1904. { Copy registers to temp }
  1905. href:=current_procinfo.save_regs_ref;
  1906. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1907. begin
  1908. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1909. begin
  1910. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  1911. inc(href.offset,sizeof(aint));
  1912. end;
  1913. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  1914. end;
  1915. if uses_registers(R_MMREGISTER) then
  1916. begin
  1917. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  1918. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  1919. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1920. begin
  1921. { the array has to be declared even if no MM registers are saved
  1922. (such as with SSE on i386), and since 0-element arrays don't
  1923. exist, they contain a single RS_INVALID element in that case
  1924. }
  1925. if saved_mm_registers[r]<>RS_INVALID then
  1926. begin
  1927. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1928. begin
  1929. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  1930. inc(href.offset,tcgsize2size[OS_VECTOR]);
  1931. end;
  1932. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  1933. end;
  1934. end;
  1935. end;
  1936. end;
  1937. end;
  1938. procedure tcg.g_restore_registers(list:TAsmList);
  1939. var
  1940. href : treference;
  1941. r : integer;
  1942. hreg : tregister;
  1943. begin
  1944. if not(pi_has_saved_regs in current_procinfo.flags) then
  1945. exit;
  1946. { Copy registers from temp }
  1947. href:=current_procinfo.save_regs_ref;
  1948. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1949. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1950. begin
  1951. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1952. { Allocate register so the optimizer does not remove the load }
  1953. a_reg_alloc(list,hreg);
  1954. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  1955. inc(href.offset,sizeof(aint));
  1956. end;
  1957. if uses_registers(R_MMREGISTER) then
  1958. begin
  1959. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  1960. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  1961. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1962. begin
  1963. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1964. begin
  1965. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  1966. { Allocate register so the optimizer does not remove the load }
  1967. a_reg_alloc(list,hreg);
  1968. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  1969. inc(href.offset,tcgsize2size[OS_VECTOR]);
  1970. end;
  1971. end;
  1972. end;
  1973. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1974. end;
  1975. procedure tcg.g_profilecode(list : TAsmList);
  1976. begin
  1977. end;
  1978. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  1979. begin
  1980. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  1981. end;
  1982. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  1983. begin
  1984. a_load_const_ref(list, OS_INT, a, href);
  1985. end;
  1986. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  1987. begin
  1988. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  1989. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  1990. end;
  1991. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1992. var
  1993. hsym : tsym;
  1994. href : treference;
  1995. paraloc : Pcgparalocation;
  1996. begin
  1997. { calculate the parameter info for the procdef }
  1998. procdef.init_paraloc_info(callerside);
  1999. hsym:=tsym(procdef.parast.Find('self'));
  2000. if not(assigned(hsym) and
  2001. (hsym.typ=paravarsym)) then
  2002. internalerror(200305251);
  2003. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2004. while paraloc<>nil do
  2005. with paraloc^ do
  2006. begin
  2007. case loc of
  2008. LOC_REGISTER:
  2009. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2010. LOC_REFERENCE:
  2011. begin
  2012. { offset in the wrapper needs to be adjusted for the stored
  2013. return address }
  2014. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  2015. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2016. end
  2017. else
  2018. internalerror(200309189);
  2019. end;
  2020. paraloc:=next;
  2021. end;
  2022. end;
  2023. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  2024. begin
  2025. a_jmp_name(list,externalname);
  2026. end;
  2027. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2028. begin
  2029. a_call_name(list,s,false);
  2030. end;
  2031. procedure tcg.a_call_ref(list : TAsmList;ref: treference);
  2032. var
  2033. tempreg : TRegister;
  2034. begin
  2035. tempreg := getintregister(list, OS_ADDR);
  2036. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tempreg);
  2037. a_call_reg(list,tempreg);
  2038. end;
  2039. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2040. var
  2041. l: tasmsymbol;
  2042. ref: treference;
  2043. nlsymname: string;
  2044. begin
  2045. result := NR_NO;
  2046. case target_info.system of
  2047. system_powerpc_darwin,
  2048. system_i386_darwin,
  2049. system_i386_iphonesim,
  2050. system_powerpc64_darwin,
  2051. system_arm_darwin:
  2052. begin
  2053. nlsymname:='L'+symname+'$non_lazy_ptr';
  2054. l:=current_asmdata.getasmsymbol(nlsymname);
  2055. if not(assigned(l)) then
  2056. begin
  2057. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2058. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  2059. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2060. if not(is_weak in flags) then
  2061. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  2062. else
  2063. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  2064. {$ifdef cpu64bitaddr}
  2065. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2066. {$else cpu64bitaddr}
  2067. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2068. {$endif cpu64bitaddr}
  2069. end;
  2070. result := getaddressregister(list);
  2071. reference_reset_symbol(ref,l,0,sizeof(pint));
  2072. { a_load_ref_reg will turn this into a pic-load if needed }
  2073. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2074. end;
  2075. end;
  2076. end;
  2077. procedure tcg.g_maybe_got_init(list: TAsmList);
  2078. begin
  2079. end;
  2080. procedure tcg.g_call(list: TAsmList;const s: string);
  2081. begin
  2082. allocallcpuregisters(list);
  2083. a_call_name(list,s,false);
  2084. deallocallcpuregisters(list);
  2085. end;
  2086. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2087. begin
  2088. a_jmp_always(list,l);
  2089. end;
  2090. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2091. begin
  2092. internalerror(200807231);
  2093. end;
  2094. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2095. begin
  2096. internalerror(200807232);
  2097. end;
  2098. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2099. begin
  2100. internalerror(200807233);
  2101. end;
  2102. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2103. begin
  2104. internalerror(200807234);
  2105. end;
  2106. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2107. begin
  2108. Result:=TRegister(0);
  2109. internalerror(200807238);
  2110. end;
  2111. {*****************************************************************************
  2112. TCG64
  2113. *****************************************************************************}
  2114. {$ifndef cpu64bitalu}
  2115. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2116. begin
  2117. a_load64_reg_reg(list,regsrc,regdst);
  2118. a_op64_const_reg(list,op,size,value,regdst);
  2119. end;
  2120. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2121. var
  2122. tmpreg64 : tregister64;
  2123. begin
  2124. { when src1=dst then we need to first create a temp to prevent
  2125. overwriting src1 with src2 }
  2126. if (regsrc1.reghi=regdst.reghi) or
  2127. (regsrc1.reglo=regdst.reghi) or
  2128. (regsrc1.reghi=regdst.reglo) or
  2129. (regsrc1.reglo=regdst.reglo) then
  2130. begin
  2131. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2132. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2133. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2134. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2135. a_load64_reg_reg(list,tmpreg64,regdst);
  2136. end
  2137. else
  2138. begin
  2139. a_load64_reg_reg(list,regsrc2,regdst);
  2140. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2141. end;
  2142. end;
  2143. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2144. var
  2145. tmpreg64 : tregister64;
  2146. begin
  2147. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2148. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2149. a_load64_subsetref_reg(list,sref,tmpreg64);
  2150. a_op64_const_reg(list,op,size,a,tmpreg64);
  2151. a_load64_reg_subsetref(list,tmpreg64,sref);
  2152. end;
  2153. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2154. var
  2155. tmpreg64 : tregister64;
  2156. begin
  2157. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2158. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2159. a_load64_subsetref_reg(list,sref,tmpreg64);
  2160. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2161. a_load64_reg_subsetref(list,tmpreg64,sref);
  2162. end;
  2163. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2164. var
  2165. tmpreg64 : tregister64;
  2166. begin
  2167. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2168. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2169. a_load64_subsetref_reg(list,sref,tmpreg64);
  2170. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2171. a_load64_reg_subsetref(list,tmpreg64,sref);
  2172. end;
  2173. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2174. var
  2175. tmpreg64 : tregister64;
  2176. begin
  2177. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2178. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2179. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2180. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2181. end;
  2182. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2183. begin
  2184. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2185. ovloc.loc:=LOC_VOID;
  2186. end;
  2187. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2188. begin
  2189. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2190. ovloc.loc:=LOC_VOID;
  2191. end;
  2192. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2193. begin
  2194. case l.loc of
  2195. LOC_REFERENCE, LOC_CREFERENCE:
  2196. a_load64_ref_subsetref(list,l.reference,sref);
  2197. LOC_REGISTER,LOC_CREGISTER:
  2198. a_load64_reg_subsetref(list,l.register64,sref);
  2199. LOC_CONSTANT :
  2200. a_load64_const_subsetref(list,l.value64,sref);
  2201. LOC_SUBSETREF,LOC_CSUBSETREF:
  2202. a_load64_subsetref_subsetref(list,l.sref,sref);
  2203. else
  2204. internalerror(2006082210);
  2205. end;
  2206. end;
  2207. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2208. begin
  2209. case l.loc of
  2210. LOC_REFERENCE, LOC_CREFERENCE:
  2211. a_load64_subsetref_ref(list,sref,l.reference);
  2212. LOC_REGISTER,LOC_CREGISTER:
  2213. a_load64_subsetref_reg(list,sref,l.register64);
  2214. LOC_SUBSETREF,LOC_CSUBSETREF:
  2215. a_load64_subsetref_subsetref(list,sref,l.sref);
  2216. else
  2217. internalerror(2006082211);
  2218. end;
  2219. end;
  2220. {$else cpu64bitalu}
  2221. function joinreg128(reglo, reghi: tregister): tregister128;
  2222. begin
  2223. result.reglo:=reglo;
  2224. result.reghi:=reghi;
  2225. end;
  2226. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2227. var
  2228. paraloclo,
  2229. paralochi : pcgparalocation;
  2230. begin
  2231. if not(cgpara.size in [OS_128,OS_S128]) then
  2232. internalerror(2012090604);
  2233. if not assigned(cgpara.location) then
  2234. internalerror(2012090605);
  2235. { init lo/hi para }
  2236. cgparahi.reset;
  2237. if cgpara.size=OS_S128 then
  2238. cgparahi.size:=OS_S64
  2239. else
  2240. cgparahi.size:=OS_64;
  2241. cgparahi.intsize:=8;
  2242. cgparahi.alignment:=cgpara.alignment;
  2243. paralochi:=cgparahi.add_location;
  2244. cgparalo.reset;
  2245. cgparalo.size:=OS_64;
  2246. cgparalo.intsize:=8;
  2247. cgparalo.alignment:=cgpara.alignment;
  2248. paraloclo:=cgparalo.add_location;
  2249. { 2 parameter fields? }
  2250. if assigned(cgpara.location^.next) then
  2251. begin
  2252. { Order for multiple locations is always
  2253. paraloc^ -> high
  2254. paraloc^.next -> low }
  2255. if (target_info.endian=ENDIAN_BIG) then
  2256. begin
  2257. { paraloc^ -> high
  2258. paraloc^.next -> low }
  2259. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2260. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2261. end
  2262. else
  2263. begin
  2264. { paraloc^ -> low
  2265. paraloc^.next -> high }
  2266. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2267. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2268. end;
  2269. end
  2270. else
  2271. begin
  2272. { single parameter, this can only be in memory }
  2273. if cgpara.location^.loc<>LOC_REFERENCE then
  2274. internalerror(2012090606);
  2275. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2276. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2277. { for big endian low is at +8, for little endian high }
  2278. if target_info.endian = endian_big then
  2279. begin
  2280. inc(cgparalo.location^.reference.offset,8);
  2281. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2282. end
  2283. else
  2284. begin
  2285. inc(cgparahi.location^.reference.offset,8);
  2286. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2287. end;
  2288. end;
  2289. { fix size }
  2290. paraloclo^.size:=cgparalo.size;
  2291. paraloclo^.next:=nil;
  2292. paralochi^.size:=cgparahi.size;
  2293. paralochi^.next:=nil;
  2294. end;
  2295. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2296. regdst: tregister128);
  2297. begin
  2298. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2299. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2300. end;
  2301. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2302. const ref: treference);
  2303. var
  2304. tmpreg: tregister;
  2305. tmpref: treference;
  2306. begin
  2307. if target_info.endian = endian_big then
  2308. begin
  2309. tmpreg:=reg.reglo;
  2310. reg.reglo:=reg.reghi;
  2311. reg.reghi:=tmpreg;
  2312. end;
  2313. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2314. tmpref := ref;
  2315. inc(tmpref.offset,8);
  2316. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2317. end;
  2318. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2319. reg: tregister128);
  2320. var
  2321. tmpreg: tregister;
  2322. tmpref: treference;
  2323. begin
  2324. if target_info.endian = endian_big then
  2325. begin
  2326. tmpreg := reg.reglo;
  2327. reg.reglo := reg.reghi;
  2328. reg.reghi := tmpreg;
  2329. end;
  2330. tmpref := ref;
  2331. if (tmpref.base=reg.reglo) then
  2332. begin
  2333. tmpreg:=cg.getaddressregister(list);
  2334. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2335. tmpref.base:=tmpreg;
  2336. end
  2337. else
  2338. { this works only for the i386, thus the i386 needs to override }
  2339. { this method and this method must be replaced by a more generic }
  2340. { implementation FK }
  2341. if (tmpref.index=reg.reglo) then
  2342. begin
  2343. tmpreg:=cg.getaddressregister(list);
  2344. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2345. tmpref.index:=tmpreg;
  2346. end;
  2347. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2348. inc(tmpref.offset,8);
  2349. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2350. end;
  2351. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2352. const ref: treference);
  2353. begin
  2354. case l.loc of
  2355. LOC_REGISTER,LOC_CREGISTER:
  2356. a_load128_reg_ref(list,l.register128,ref);
  2357. { not yet implemented:
  2358. LOC_CONSTANT :
  2359. a_load128_const_ref(list,l.value128,ref);
  2360. LOC_SUBSETREF, LOC_CSUBSETREF:
  2361. a_load64_subsetref_ref(list,l.sref,ref); }
  2362. else
  2363. internalerror(201209061);
  2364. end;
  2365. end;
  2366. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2367. const l: tlocation);
  2368. begin
  2369. case l.loc of
  2370. LOC_REFERENCE, LOC_CREFERENCE:
  2371. a_load128_reg_ref(list,reg,l.reference);
  2372. LOC_REGISTER,LOC_CREGISTER:
  2373. a_load128_reg_reg(list,reg,l.register128);
  2374. { not yet implemented:
  2375. LOC_SUBSETREF, LOC_CSUBSETREF:
  2376. a_load64_reg_subsetref(list,reg,l.sref);
  2377. LOC_MMREGISTER, LOC_CMMREGISTER:
  2378. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2379. else
  2380. internalerror(201209062);
  2381. end;
  2382. end;
  2383. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2384. valuehi: int64; reg: tregister128);
  2385. begin
  2386. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2387. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2388. end;
  2389. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2390. const paraloc: TCGPara);
  2391. begin
  2392. case l.loc of
  2393. LOC_REGISTER,
  2394. LOC_CREGISTER :
  2395. a_load128_reg_cgpara(list,l.register128,paraloc);
  2396. {not yet implemented:
  2397. LOC_CONSTANT :
  2398. a_load128_const_cgpara(list,l.value64,paraloc);
  2399. }
  2400. LOC_CREFERENCE,
  2401. LOC_REFERENCE :
  2402. a_load128_ref_cgpara(list,l.reference,paraloc);
  2403. else
  2404. internalerror(2012090603);
  2405. end;
  2406. end;
  2407. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2408. var
  2409. tmplochi,tmploclo: tcgpara;
  2410. begin
  2411. tmploclo.init;
  2412. tmplochi.init;
  2413. splitparaloc128(paraloc,tmploclo,tmplochi);
  2414. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2415. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2416. tmploclo.done;
  2417. tmplochi.done;
  2418. end;
  2419. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2420. var
  2421. tmprefhi,tmpreflo : treference;
  2422. tmploclo,tmplochi : tcgpara;
  2423. begin
  2424. tmploclo.init;
  2425. tmplochi.init;
  2426. splitparaloc128(paraloc,tmploclo,tmplochi);
  2427. tmprefhi:=r;
  2428. tmpreflo:=r;
  2429. if target_info.endian=endian_big then
  2430. inc(tmpreflo.offset,8)
  2431. else
  2432. inc(tmprefhi.offset,8);
  2433. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2434. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2435. tmploclo.done;
  2436. tmplochi.done;
  2437. end;
  2438. {$endif cpu64bitalu}
  2439. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2440. begin
  2441. result:=[];
  2442. if sym.typ<>AT_FUNCTION then
  2443. include(result,is_data);
  2444. if sym.bind=AB_WEAK_EXTERNAL then
  2445. include(result,is_weak);
  2446. end;
  2447. procedure destroy_codegen;
  2448. begin
  2449. cg.free;
  2450. cg:=nil;
  2451. {$ifdef cpu64bitalu}
  2452. cg128.free;
  2453. cg128:=nil;
  2454. {$else cpu64bitalu}
  2455. cg64.free;
  2456. cg64:=nil;
  2457. {$endif cpu64bitalu}
  2458. end;
  2459. end.