cgobj.pas 124 KB

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