cgobj.pas 134 KB

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