cgobj.pas 126 KB

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