cgobj.pas 140 KB

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