cgobj.pas 136 KB

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