2
0

cgobj.pas 143 KB

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