cgobj.pas 117 KB

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