cgobj.pas 137 KB

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