cgobj.pas 141 KB

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