cgobj.pas 127 KB

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