cgobj.pas 133 KB

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