cgobj.pas 130 KB

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