cgobj.pas 120 KB

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