cgobj.pas 138 KB

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