cgobj.pas 123 KB

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