cgobj.pas 124 KB

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