2
0

cgobj.pas 136 KB

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