cgobj.pas 123 KB

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