hlcgobj.pas 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the basic high level 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. Abstract high level code generator unit. This contains the base class
  20. that either lowers most code to the regular code generator, or that
  21. has to be implemented/overridden for higher level targets (such as LLVM).
  22. }
  23. unit hlcgobj;
  24. {$i fpcdefs.inc}
  25. { define hlcginline}
  26. interface
  27. uses
  28. cclasses,globtype,constexp,
  29. cpubase,cgbase,cgutils,parabase,
  30. aasmbase,aasmtai,aasmdata,aasmcpu,
  31. symconst,symtype,symdef,rgobj,
  32. node
  33. ;
  34. type
  35. {# @abstract(Abstract high level code generator)
  36. This class implements an abstract instruction generator. All
  37. methods of this class are generic and are mapped to low level code
  38. generator methods by default. They have to be overridden for higher
  39. level targets
  40. }
  41. { thlcgobj }
  42. thlcgobj = class
  43. public
  44. {************************************************}
  45. { basic routines }
  46. constructor create;
  47. {# Initialize the register allocators needed for the codegenerator.}
  48. procedure init_register_allocators;virtual;
  49. {# Clean up the register allocators needed for the codegenerator.}
  50. procedure done_register_allocators;virtual;
  51. {# 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. }
  52. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  53. {# Gets a register suitable to do integer operations on.}
  54. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  55. {# Gets a register suitable to do integer operations on.}
  56. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  57. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  58. // we don't have high level defs yet that translate into all mm cgsizes
  59. // function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  60. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  61. function getregisterfordef(list: TAsmList;size:tdef):Tregister;virtual;
  62. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  63. the cpu specific child cg object have such a method?}
  64. function uses_registers(rt:Tregistertype):boolean; inline;
  65. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  66. procedure translate_register(var reg : tregister); inline;
  67. {# Returns the kind of register this type should be loaded in (it does not
  68. check whether this is actually possible, but if it's loaded in a register
  69. by the compiler for any purpose other than parameter passing/function
  70. result loading, this is the register type used }
  71. function def2regtyp(def: tdef): tregistertype; virtual;
  72. {# Emit a label to the instruction stream. }
  73. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  74. {# Allocates register r by inserting a pai_realloc record }
  75. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  76. {# Deallocates register r by inserting a pa_regdealloc record}
  77. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  78. { Synchronize register, make sure it is still valid }
  79. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  80. {# Pass a parameter, which is located in a register, to a routine.
  81. This routine should push/send the parameter to the routine, as
  82. required by the specific processor ABI and routine modifiers.
  83. It must generate register allocation information for the cgpara in
  84. case it consists of cpuregisters.
  85. @param(size size of the operand in the register)
  86. @param(r register source of the operand)
  87. @param(cgpara where the parameter will be stored)
  88. }
  89. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  90. {# Pass a parameter, which is a constant, to a routine.
  91. A generic version is provided. This routine should
  92. be overridden for optimization purposes if the cpu
  93. permits directly sending this type of parameter.
  94. It must generate register allocation information for the cgpara in
  95. case it consists of cpuregisters.
  96. @param(size size of the operand in constant)
  97. @param(a value of constant to send)
  98. @param(cgpara where the parameter will be stored)
  99. }
  100. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : aint;const cgpara : TCGPara);virtual;
  101. {# Pass the value of a parameter, which is located in memory, to a routine.
  102. A generic version is provided. This routine should
  103. be overridden for optimization purposes if the cpu
  104. permits directly sending this type of parameter.
  105. It must generate register allocation information for the cgpara in
  106. case it consists of cpuregisters.
  107. @param(size size of the operand in constant)
  108. @param(r Memory reference of value to send)
  109. @param(cgpara where the parameter will be stored)
  110. }
  111. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  112. {# Pass the value of a parameter, which can be located either in a register or memory location,
  113. to a routine.
  114. A generic version is provided.
  115. @param(l location of the operand to send)
  116. @param(nr parameter number (starting from one) of routine (from left to right))
  117. @param(cgpara where the parameter will be stored)
  118. }
  119. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  120. {# Pass the address of a reference to a routine. This routine
  121. will calculate the address of the reference, and pass this
  122. calculated address as a parameter.
  123. It must generate register allocation information for the cgpara in
  124. case it consists of cpuregisters.
  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. @param(fromsize type of the reference we are taking the address of)
  129. @param(tosize type of the pointer that we get as a result)
  130. @param(r reference to get address from)
  131. }
  132. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize, tosize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  133. { Remarks:
  134. * If a method specifies a size you have only to take care
  135. of that number of bits, i.e. load_const_reg with OP_8 must
  136. only load the lower 8 bit of the specified register
  137. the rest of the register can be undefined
  138. if necessary the compiler will call a method
  139. to zero or sign extend the register
  140. * The a_load_XX_XX with OP_64 needn't to be
  141. implemented for 32 bit
  142. processors, the code generator takes care of that
  143. * the addr size is for work with the natural pointer
  144. size
  145. * the procedures without fpu/mm are only for integer usage
  146. * normally the first location is the source and the
  147. second the destination
  148. }
  149. {# Emits instruction to call the method specified by symbol name.
  150. This routine must be overridden for each new target cpu.
  151. }
  152. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; weak: boolean);virtual;abstract;
  153. procedure a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister);virtual;abstract;
  154. procedure a_call_ref(list : TAsmList;pd : tabstractprocdef;ref : treference);virtual;abstract;
  155. { same as a_call_name, might be overridden on certain architectures to emit
  156. static calls without usage of a got trampoline }
  157. procedure a_call_name_static(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  158. { same as a_call_name, might be overridden on certain architectures to emit
  159. special static calls for inherited methods }
  160. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  161. { move instructions }
  162. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);virtual;abstract;
  163. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);virtual;
  164. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : aint;const loc : tlocation);virtual;
  165. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  166. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  167. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  168. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  169. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  170. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  171. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  172. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  173. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  174. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  175. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  176. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  177. { The subset stuff still need a transformation to thlcgobj }
  178. procedure a_load_subsetreg_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual; abstract;
  179. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual; abstract;
  180. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual; abstract;
  181. procedure a_load_subsetreg_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual; abstract;
  182. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosize,tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual; abstract;
  183. procedure a_load_const_subsetreg(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sreg: tsubsetregister); virtual; abstract;
  184. procedure a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  185. procedure a_load_subsetref_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual; abstract;
  186. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize, tosize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual; abstract;
  187. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual; abstract;
  188. procedure a_load_subsetref_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual; abstract;
  189. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual; abstract;
  190. procedure a_load_const_subsetref(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sref: tsubsetreference); virtual; abstract;
  191. procedure a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  192. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual; abstract;
  193. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual; abstract;
  194. { bit test instructions (still need transformation to thlcgobj) }
  195. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual; abstract;
  196. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual; abstract;
  197. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual; abstract;
  198. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsize, fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual; abstract;
  199. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual; abstract;
  200. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  201. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  202. { bit set/clear instructions (still need transformation to thlcgobj) }
  203. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual; abstract;
  204. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: aint; const ref: treference); virtual; abstract;
  205. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister); virtual; abstract;
  206. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize, destsubsetsize: tdef; bitnumber: aint; const destreg: tsubsetregister); virtual; abstract;
  207. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual; abstract;
  208. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  209. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);virtual;
  210. { bit scan instructions (still need transformation to thlcgobj) }
  211. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  212. { fpu move instructions }
  213. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  214. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  215. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  216. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  217. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  218. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  219. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  220. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  221. { vector register move instructions }
  222. // we don't have high level defs yet that translate into all mm cgsizes
  223. {
  224. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  226. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  227. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual;
  228. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  229. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  230. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  231. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  232. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  233. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  234. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  235. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  236. }
  237. // we don't have high level defs yet that translate into all mm cgsizes
  238. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  239. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  240. { basic arithmetic operations }
  241. { note: for operators which require only one argument (not, neg), use }
  242. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  243. { that in this case the *second* operand is used as both source and }
  244. { destination (JM) }
  245. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  246. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  247. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  248. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  249. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  250. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  251. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  252. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  253. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  254. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  255. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  256. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  257. { trinary operations for processors that support them, 'emulated' }
  258. { on others. None with "ref" arguments since I don't think there }
  259. { are any processors that support it (JM) }
  260. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  261. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  262. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  263. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  264. { comparison operations }
  265. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  266. l : tasmlabel);virtual;
  267. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  268. l : tasmlabel); virtual;
  269. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  270. l : tasmlabel);virtual;
  271. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  272. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  273. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  274. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  275. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  276. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  277. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);virtual;
  278. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  279. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  280. {$ifdef cpuflags}
  281. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  282. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  283. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  284. }
  285. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  286. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  287. {$endif cpuflags}
  288. // procedure g_maybe_testself(list : TAsmList;reg:tregister);
  289. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  290. {# This should emit the opcode to copy len bytes from the source
  291. to destination.
  292. It must be overridden for each new target processor.
  293. @param(source Source reference of copy)
  294. @param(dest Destination reference of copy)
  295. }
  296. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  297. {# This should emit the opcode to copy len bytes from the an unaligned source
  298. to destination.
  299. It must be overridden for each new target processor.
  300. @param(source Source reference of copy)
  301. @param(dest Destination reference of copy)
  302. }
  303. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  304. {# This should emit the opcode to a shortrstring from the source
  305. to destination.
  306. @param(source Source reference of copy)
  307. @param(dest Destination reference of copy)
  308. }
  309. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;abstract;
  310. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;abstract;
  311. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  312. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  313. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  314. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  315. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  316. const name: string);virtual;abstract;
  317. {# Generates range checking code. It is to note
  318. that this routine does not need to be overridden,
  319. as it takes care of everything.
  320. @param(p Node which contains the value to check)
  321. @param(todef Type definition of node to range check)
  322. }
  323. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  324. {# Generates overflow checking code for a node }
  325. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  326. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  327. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;abstract;
  328. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;abstract;
  329. {# Emits instructions when compilation is done in profile
  330. mode (this is set as a command line option). The default
  331. behavior does nothing, should be overridden as required.
  332. }
  333. procedure g_profilecode(list : TAsmList);virtual;
  334. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  335. @param(size Number of bytes to allocate)
  336. }
  337. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  338. {# Emits instruction for allocating the locals in entry
  339. code of a routine. This is one of the first
  340. routine called in @var(genentrycode).
  341. @param(localsize Number of bytes to allocate as locals)
  342. }
  343. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  344. {# Emits instructions for returning from a subroutine.
  345. Should also restore the framepointer and stack.
  346. @param(parasize Number of bytes of parameters to deallocate from stack)
  347. }
  348. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  349. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  350. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  351. function g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;virtual; abstract;
  352. { generate a stub which only purpose is to pass control the given external method,
  353. setting up any additional environment before doing so (if required).
  354. The default implementation issues a jump instruction to the external name. }
  355. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  356. { create "safe copy" of a tlocation that can be used later: all
  357. registers used in the tlocation are copied to new ones, so that
  358. even if the original ones change, things stay the same (except if
  359. the original location was already a register, then the register is
  360. kept). Must only be used on lvalue locations.
  361. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  362. for targets without pointers. }
  363. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual; abstract;
  364. { routines migrated from ncgutil }
  365. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  366. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  367. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  368. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  369. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  370. { Retrieve the location of the data pointed to in location l, when the location is
  371. a register it is expected to contain the address of the data }
  372. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  373. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  374. procedure gen_proc_symbol(list:TAsmList);virtual;
  375. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  376. procedure gen_initialize_code(list:TAsmList);virtual;
  377. procedure gen_finalize_code(list:TAsmList);virtual;
  378. procedure gen_entry_code(list:TAsmList);virtual;
  379. procedure gen_exit_code(list:TAsmList);virtual;
  380. protected
  381. { helpers called by gen_initialize_code/gen_finalize_code }
  382. procedure inittempvariables(list:TAsmList);virtual;
  383. procedure initialize_data(p:TObject;arg:pointer);virtual;
  384. procedure finalizetempvariables(list:TAsmList);virtual;
  385. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  386. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  387. { generates the code for finalisation of local variables }
  388. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  389. { generates the code for finalization of static symtable and
  390. all local (static) typed consts }
  391. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  392. { generates the code for decrementing the reference count of parameters }
  393. procedure final_paras(p:TObject;arg:pointer);
  394. public
  395. procedure gen_load_para_value(list:TAsmList);virtual;
  396. protected
  397. { helpers called by gen_load_para_value }
  398. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  399. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  400. procedure init_paras(p:TObject;arg:pointer);
  401. protected
  402. { Some targets have to put "something" in the function result
  403. location if it's not initialised by the Pascal code, e.g.
  404. stack-based architectures. By default it does nothing }
  405. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  406. public
  407. { load a tlocation into a cgpara }
  408. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  409. { load a cgpara into a tlocation }
  410. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  411. { load the function return value into the ABI-defined function return location }
  412. procedure gen_load_return_value(list:TAsmList);virtual;
  413. { extras refactored from other units }
  414. { queue the code/data generated for a procedure for writing out to
  415. the assembler/object file }
  416. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  417. end;
  418. var
  419. {# Main high level code generator class }
  420. hlcg : thlcgobj;
  421. procedure destroy_hlcodegen;
  422. implementation
  423. uses
  424. globals,options,systems,
  425. fmodule,export,
  426. verbose,defutil,paramgr,
  427. symbase,symsym,
  428. ncon,nld,pass_1,pass_2,
  429. cpuinfo,cgobj,tgobj,cutils,procinfo,
  430. ncgutil,ngenutil;
  431. procedure destroy_hlcodegen;
  432. begin
  433. hlcg.free;
  434. hlcg:=nil;
  435. destroy_codegen;
  436. end;
  437. { thlcgobj }
  438. constructor thlcgobj.create;
  439. begin
  440. end;
  441. procedure thlcgobj.init_register_allocators;
  442. begin
  443. cg.init_register_allocators;
  444. end;
  445. procedure thlcgobj.done_register_allocators;
  446. begin
  447. cg.done_register_allocators;
  448. end;
  449. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  450. begin
  451. cg.set_regalloc_live_range_direction(dir);
  452. end;
  453. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  454. begin
  455. result:=cg.getintregister(list,def_cgsize(size));
  456. end;
  457. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  458. begin
  459. result:=cg.getaddressregister(list);
  460. end;
  461. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  462. begin
  463. result:=cg.getfpuregister(list,def_cgsize(size));
  464. end;
  465. (*
  466. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  467. begin
  468. result:=cg.getmmregister(list,def_cgsize(size));
  469. end;
  470. *)
  471. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  472. begin
  473. result:=cg.getflagregister(list,def_cgsize(size));
  474. end;
  475. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  476. begin
  477. case def2regtyp(size) of
  478. R_INTREGISTER:
  479. result:=getintregister(list,size);
  480. R_ADDRESSREGISTER:
  481. result:=getaddressregister(list,size);
  482. R_FPUREGISTER:
  483. result:=getfpuregister(list,size);
  484. (*
  485. R_MMREGISTER:
  486. result:=getmmregister(list,size);
  487. *)
  488. else
  489. internalerror(2010122901);
  490. end;
  491. end;
  492. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  493. begin
  494. result:=cg.uses_registers(rt);
  495. end;
  496. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  497. begin
  498. cg.do_register_allocation(list,headertai);
  499. end;
  500. procedure thlcgobj.translate_register(var reg: tregister);
  501. begin
  502. cg.translate_register(reg);
  503. end;
  504. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  505. begin
  506. case def.typ of
  507. enumdef,
  508. orddef,
  509. recorddef,
  510. setdef:
  511. result:=R_INTREGISTER;
  512. stringdef,
  513. pointerdef,
  514. classrefdef,
  515. objectdef,
  516. procvardef,
  517. procdef,
  518. arraydef,
  519. formaldef:
  520. result:=R_ADDRESSREGISTER;
  521. floatdef:
  522. if use_vectorfpu(def) then
  523. result:=R_MMREGISTER
  524. else if cs_fp_emulation in current_settings.moduleswitches then
  525. result:=R_INTREGISTER
  526. else
  527. result:=R_FPUREGISTER;
  528. filedef,
  529. variantdef:
  530. internalerror(2010120507);
  531. else
  532. internalerror(2010120506);
  533. end;
  534. end;
  535. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  536. begin
  537. cg.a_label(list,l);
  538. end;
  539. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  540. begin
  541. cg.a_reg_alloc(list,r);
  542. end;
  543. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  544. begin
  545. cg.a_reg_dealloc(list,r);
  546. end;
  547. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  548. begin
  549. cg.a_reg_sync(list,r);
  550. end;
  551. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  552. var
  553. ref: treference;
  554. begin
  555. cgpara.check_simple_location;
  556. paramanager.alloccgpara(list,cgpara);
  557. case cgpara.location^.loc of
  558. LOC_REGISTER,LOC_CREGISTER:
  559. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  560. LOC_REFERENCE,LOC_CREFERENCE:
  561. begin
  562. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  563. a_load_reg_ref(list,size,cgpara.def,r,ref);
  564. end;
  565. (*
  566. LOC_MMREGISTER,LOC_CMMREGISTER:
  567. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  568. *)
  569. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  570. begin
  571. tg.gethltemp(list,size,size.size,tt_normal,ref);
  572. a_load_reg_ref(list,size,size,r,ref);
  573. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  574. tg.ungettemp(list,ref);
  575. end
  576. else
  577. internalerror(2010120415);
  578. end;
  579. end;
  580. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  581. var
  582. ref : treference;
  583. begin
  584. cgpara.check_simple_location;
  585. paramanager.alloccgpara(list,cgpara);
  586. case cgpara.location^.loc of
  587. LOC_REGISTER,LOC_CREGISTER:
  588. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  589. LOC_REFERENCE,LOC_CREFERENCE:
  590. begin
  591. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  592. a_load_const_ref(list,cgpara.def,a,ref);
  593. end
  594. else
  595. internalerror(2010120416);
  596. end;
  597. end;
  598. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  599. var
  600. ref: treference;
  601. begin
  602. cgpara.check_simple_location;
  603. paramanager.alloccgpara(list,cgpara);
  604. case cgpara.location^.loc of
  605. LOC_REGISTER,LOC_CREGISTER:
  606. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  607. LOC_REFERENCE,LOC_CREFERENCE:
  608. begin
  609. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  610. a_load_ref_ref(list,size,cgpara.def,r,ref);
  611. end
  612. (*
  613. LOC_MMREGISTER,LOC_CMMREGISTER:
  614. begin
  615. case location^.size of
  616. OS_F32,
  617. OS_F64,
  618. OS_F128:
  619. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  620. OS_M8..OS_M128,
  621. OS_MS8..OS_MS128:
  622. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  623. else
  624. internalerror(2010120417);
  625. end;
  626. end
  627. *)
  628. else
  629. internalerror(2010120418);
  630. end;
  631. end;
  632. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  633. begin
  634. case l.loc of
  635. LOC_REGISTER,
  636. LOC_CREGISTER :
  637. a_load_reg_cgpara(list,size,l.register,cgpara);
  638. LOC_CONSTANT :
  639. a_load_const_cgpara(list,size,l.value,cgpara);
  640. LOC_CREFERENCE,
  641. LOC_REFERENCE :
  642. a_load_ref_cgpara(list,size,l.reference,cgpara);
  643. else
  644. internalerror(2010120419);
  645. end;
  646. end;
  647. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  648. var
  649. hr : tregister;
  650. begin
  651. cgpara.check_simple_location;
  652. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  653. begin
  654. paramanager.allocparaloc(list,cgpara.location);
  655. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  656. end
  657. else
  658. begin
  659. hr:=getaddressregister(list,tosize);
  660. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  661. a_load_reg_cgpara(list,tosize,hr,cgpara);
  662. end;
  663. end;
  664. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr);
  665. begin
  666. a_call_name(list,pd,s,false);
  667. end;
  668. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  669. begin
  670. a_call_name(list,pd,s,false);
  671. end;
  672. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  673. var
  674. tmpreg: tregister;
  675. begin
  676. tmpreg:=getintregister(list,tosize);
  677. a_load_const_reg(list,tosize,a,tmpreg);
  678. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  679. end;
  680. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  681. begin
  682. case loc.loc of
  683. LOC_REFERENCE,LOC_CREFERENCE:
  684. a_load_const_ref(list,tosize,a,loc.reference);
  685. LOC_REGISTER,LOC_CREGISTER:
  686. a_load_const_reg(list,tosize,a,loc.register);
  687. { we don't have enough type information to handle these here
  688. LOC_SUBSETREG,LOC_CSUBSETREG:
  689. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  690. LOC_SUBSETREF,LOC_CSUBSETREF:
  691. a_load_const_subsetref(list,loc.size,a,loc.sref);
  692. }
  693. else
  694. internalerror(2010120401);
  695. end;
  696. end;
  697. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  698. begin
  699. a_load_reg_ref(list,fromsize,tosize,register,ref);
  700. end;
  701. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  702. begin
  703. case loc.loc of
  704. LOC_REFERENCE,LOC_CREFERENCE:
  705. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  706. LOC_REGISTER,LOC_CREGISTER:
  707. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  708. { we don't have enough type information to handle these here
  709. LOC_SUBSETREG,LOC_CSUBSETREG:
  710. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  711. LOC_SUBSETREF,LOC_CSUBSETREF:
  712. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  713. LOC_MMREGISTER,LOC_CMMREGISTER:
  714. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  715. }
  716. else
  717. internalerror(2010120402);
  718. end;
  719. end;
  720. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  721. begin
  722. a_load_ref_reg(list,fromsize,tosize,ref,register);
  723. end;
  724. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  725. var
  726. tmpreg: tregister;
  727. begin
  728. { verify if we have the same reference }
  729. if references_equal(sref,dref) then
  730. exit;
  731. tmpreg:=getintregister(list,tosize);
  732. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  733. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  734. end;
  735. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  736. begin
  737. case loc.loc of
  738. LOC_REFERENCE,LOC_CREFERENCE:
  739. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  740. LOC_REGISTER,LOC_CREGISTER:
  741. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  742. LOC_CONSTANT:
  743. a_load_const_reg(list,tosize,loc.value,reg);
  744. { we don't have enough type information to handle these here
  745. LOC_SUBSETREG,LOC_CSUBSETREG:
  746. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  747. LOC_SUBSETREF,LOC_CSUBSETREF:
  748. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  749. }
  750. else
  751. internalerror(2010120201);
  752. end;
  753. end;
  754. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  755. begin
  756. case loc.loc of
  757. LOC_REFERENCE,LOC_CREFERENCE:
  758. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  759. LOC_REGISTER,LOC_CREGISTER:
  760. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  761. LOC_CONSTANT:
  762. a_load_const_ref(list,tosize,loc.value,ref);
  763. { we don't have enough type information to handle these here
  764. LOC_SUBSETREG,LOC_CSUBSETREG:
  765. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  766. LOC_SUBSETREF,LOC_CSUBSETREF:
  767. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  768. }
  769. else
  770. internalerror(2010120403);
  771. end;
  772. end;
  773. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  774. begin
  775. case loc.loc of
  776. LOC_REFERENCE,LOC_CREFERENCE:
  777. a_load_ref_subsetreg(list,fromsize,tosize,tosubsetsize,loc.reference,sreg);
  778. LOC_REGISTER,LOC_CREGISTER:
  779. a_load_reg_subsetreg(list,fromsize,tosize,tosubsetsize,loc.register,sreg);
  780. LOC_CONSTANT:
  781. a_load_const_subsetreg(list,tosize,tosubsetsize,loc.value,sreg);
  782. { we don't have enough type information to handle these here
  783. LOC_SUBSETREG,LOC_CSUBSETREG:
  784. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  785. LOC_SUBSETREF,LOC_CSUBSETREF:
  786. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  787. }
  788. else
  789. internalerror(2010120404);
  790. end;
  791. end;
  792. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  793. begin
  794. case loc.loc of
  795. LOC_REFERENCE,LOC_CREFERENCE:
  796. a_load_ref_subsetref(list,fromsize,tosize,tosubsetsize,loc.reference,sref);
  797. LOC_REGISTER,LOC_CREGISTER:
  798. a_load_reg_subsetref(list,fromsize,tosize,tosubsetsize,loc.register,sref);
  799. LOC_CONSTANT:
  800. a_load_const_subsetref(list,tosize,tosubsetsize,loc.value,sref);
  801. { we don't have enough type information to handle these here
  802. LOC_SUBSETREG,LOC_CSUBSETREG:
  803. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  804. LOC_SUBSETREF,LOC_CSUBSETREF:
  805. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  806. }
  807. else
  808. internalerror(2010120405);
  809. end;
  810. end;
  811. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  812. begin
  813. case loc.loc of
  814. LOC_REFERENCE,LOC_CREFERENCE:
  815. a_load_subsetreg_ref(list,fromsize,fromsubsetsize,tosize,sreg,loc.reference);
  816. LOC_REGISTER,LOC_CREGISTER:
  817. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,tosize,sreg,loc.register);
  818. { we don't have enough type information to handle these here
  819. LOC_SUBSETREG,LOC_CSUBSETREG:
  820. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  821. LOC_SUBSETREF,LOC_CSUBSETREF:
  822. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  823. }
  824. else
  825. internalerror(2010120406);
  826. end;
  827. end;
  828. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  829. begin
  830. case loc.loc of
  831. LOC_REFERENCE,LOC_CREFERENCE:
  832. a_load_subsetref_ref(list,fromsize,fromsubsetsize,tosize,sref,loc.reference);
  833. LOC_REGISTER,LOC_CREGISTER:
  834. a_load_subsetref_reg(list,fromsize,fromsubsetsize,tosize,sref,loc.register);
  835. { we don't have enough type information to handle these here
  836. LOC_SUBSETREG,LOC_CSUBSETREG:
  837. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  838. LOC_SUBSETREF,LOC_CSUBSETREF:
  839. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  840. }
  841. else
  842. internalerror(2010120407);
  843. end;
  844. end;
  845. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  846. var
  847. tmpreg: tregister;
  848. begin
  849. case loc.loc of
  850. LOC_REFERENCE,LOC_CREFERENCE:
  851. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  852. LOC_REGISTER,LOC_CREGISTER,
  853. LOC_SUBSETREG,LOC_CSUBSETREG,
  854. LOC_CONSTANT:
  855. begin
  856. case loc.loc of
  857. LOC_REGISTER,LOC_CREGISTER:
  858. tmpreg:=loc.register;
  859. (* we don't have enough type information to handle this here
  860. LOC_SUBSETREG,LOC_CSUBSETREG:
  861. begin
  862. tmpreg:=getintregister(list,loc.size);
  863. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  864. end;
  865. *)
  866. LOC_CONSTANT:
  867. begin
  868. tmpreg:=getintregister(list,locsize);
  869. a_load_const_reg(list,locsize,loc.value,tmpreg);
  870. end;
  871. end;
  872. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  873. end;
  874. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  875. else
  876. internalerror(2010120411);
  877. end;
  878. end;
  879. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  880. begin
  881. case loc.loc of
  882. LOC_REFERENCE,LOC_CREFERENCE:
  883. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  884. LOC_REGISTER,LOC_CREGISTER:
  885. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  886. (* we don't have enough type information to handle this here
  887. LOC_SUBSETREG,LOC_CSUBSETREG:
  888. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  889. *)
  890. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  891. else
  892. internalerror(2010120410);
  893. end;
  894. end;
  895. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  896. begin
  897. case loc.loc of
  898. LOC_REFERENCE:
  899. a_bit_set_reg_ref(list,doset,fromsize,tosize,bitnumber,loc.reference);
  900. LOC_CREGISTER:
  901. a_bit_set_reg_reg(list,doset,fromsize,tosize,bitnumber,loc.register);
  902. (* we don't have enough type information to handle this here
  903. { e.g. a 2-byte set in a record regvar }
  904. LOC_CSUBSETREG:
  905. begin
  906. { hard to do in-place in a generic way, so operate on a copy }
  907. tmpreg:=getintregister(list,loc.size);
  908. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  909. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  910. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  911. end;
  912. *)
  913. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  914. else
  915. internalerror(2010120408)
  916. end;
  917. end;
  918. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  919. begin
  920. case loc.loc of
  921. LOC_REFERENCE:
  922. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  923. LOC_CREGISTER:
  924. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  925. (* we don't have enough type information to handle this here
  926. LOC_CSUBSETREG:
  927. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  928. *)
  929. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  930. else
  931. internalerror(2010120409)
  932. end;
  933. end;
  934. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  935. var
  936. reg: tregister;
  937. regsize: tdef;
  938. begin
  939. if (fromsize.size>=tosize.size) then
  940. regsize:=fromsize
  941. else
  942. regsize:=tosize;
  943. reg:=getfpuregister(list,regsize);
  944. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  945. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  946. end;
  947. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  948. begin
  949. case loc.loc of
  950. LOC_REFERENCE, LOC_CREFERENCE:
  951. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  952. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  953. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  954. else
  955. internalerror(2010120412);
  956. end;
  957. end;
  958. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  959. begin
  960. case loc.loc of
  961. LOC_REFERENCE, LOC_CREFERENCE:
  962. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  963. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  964. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  965. else
  966. internalerror(2010120413);
  967. end;
  968. end;
  969. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  970. var
  971. ref : treference;
  972. begin
  973. paramanager.alloccgpara(list,cgpara);
  974. case cgpara.location^.loc of
  975. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  976. begin
  977. cgpara.check_simple_location;
  978. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  979. end;
  980. LOC_REFERENCE,LOC_CREFERENCE:
  981. begin
  982. cgpara.check_simple_location;
  983. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  984. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  985. end;
  986. LOC_REGISTER,LOC_CREGISTER:
  987. begin
  988. { paramfpu_ref does the check_simpe_location check here if necessary }
  989. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  990. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  991. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  992. tg.Ungettemp(list,ref);
  993. end;
  994. else
  995. internalerror(2010120422);
  996. end;
  997. end;
  998. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  999. var
  1000. href : treference;
  1001. // hsize: tcgsize;
  1002. begin
  1003. case cgpara.location^.loc of
  1004. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1005. begin
  1006. cgpara.check_simple_location;
  1007. paramanager.alloccgpara(list,cgpara);
  1008. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  1009. end;
  1010. LOC_REFERENCE,LOC_CREFERENCE:
  1011. begin
  1012. cgpara.check_simple_location;
  1013. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1014. { concatcopy should choose the best way to copy the data }
  1015. g_concatcopy(list,fromsize,ref,href);
  1016. end;
  1017. (* not yet supported
  1018. LOC_REGISTER,LOC_CREGISTER:
  1019. begin
  1020. { force integer size }
  1021. hsize:=int_cgsize(tcgsize2size[size]);
  1022. {$ifndef cpu64bitalu}
  1023. if (hsize in [OS_S64,OS_64]) then
  1024. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1025. else
  1026. {$endif not cpu64bitalu}
  1027. begin
  1028. cgpara.check_simple_location;
  1029. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1030. end;
  1031. end
  1032. *)
  1033. else
  1034. internalerror(2010120423);
  1035. end;
  1036. end;
  1037. (*
  1038. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1039. begin
  1040. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  1041. end;
  1042. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1043. begin
  1044. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  1045. end;
  1046. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1047. begin
  1048. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  1049. end;
  1050. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1051. begin
  1052. case loc.loc of
  1053. LOC_MMREGISTER,LOC_CMMREGISTER:
  1054. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1055. LOC_REFERENCE,LOC_CREFERENCE:
  1056. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  1057. LOC_REGISTER,LOC_CREGISTER:
  1058. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1059. else
  1060. internalerror(2010120414);
  1061. end;
  1062. end;
  1063. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  1064. begin
  1065. case loc.loc of
  1066. LOC_MMREGISTER,LOC_CMMREGISTER:
  1067. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  1068. LOC_REFERENCE,LOC_CREFERENCE:
  1069. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  1070. else
  1071. internalerror(2010120415);
  1072. end;
  1073. end;
  1074. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  1075. var
  1076. href : treference;
  1077. begin
  1078. cgpara.check_simple_location;
  1079. paramanager.alloccgpara(list,cgpara);
  1080. case cgpara.location^.loc of
  1081. LOC_MMREGISTER,LOC_CMMREGISTER:
  1082. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  1083. LOC_REFERENCE,LOC_CREFERENCE:
  1084. begin
  1085. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1086. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  1087. end;
  1088. LOC_REGISTER,LOC_CREGISTER:
  1089. begin
  1090. if assigned(shuffle) and
  1091. not shufflescalar(shuffle) then
  1092. internalerror(2009112510);
  1093. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  1094. end
  1095. else
  1096. internalerror(2010120427);
  1097. end;
  1098. end;
  1099. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  1100. var
  1101. hr : tregister;
  1102. hs : tmmshuffle;
  1103. begin
  1104. cgpara.check_simple_location;
  1105. hr:=cg.getmmregister(list,cgpara.size);
  1106. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  1107. if realshuffle(shuffle) then
  1108. begin
  1109. hs:=shuffle^;
  1110. removeshuffles(hs);
  1111. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  1112. end
  1113. else
  1114. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  1115. end;
  1116. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  1117. begin
  1118. {$ifdef extdebug}
  1119. if def_cgsize(fromsize)<>loc.size then
  1120. internalerror(2010112105);
  1121. {$endif}
  1122. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  1123. end;
  1124. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1125. begin
  1126. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  1127. end;
  1128. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1129. begin
  1130. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  1131. end;
  1132. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  1133. begin
  1134. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  1135. end;
  1136. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1137. begin
  1138. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  1139. end;
  1140. *)
  1141. (*
  1142. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1143. begin
  1144. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  1145. end;
  1146. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1147. begin
  1148. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  1149. end;
  1150. *)
  1151. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  1152. var
  1153. tmpreg : tregister;
  1154. begin
  1155. tmpreg:=getintregister(list,size);
  1156. a_load_ref_reg(list,size,size,ref,tmpreg);
  1157. a_op_const_reg(list,op,size,a,tmpreg);
  1158. a_load_reg_ref(list,size,size,tmpreg,ref);
  1159. end;
  1160. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1161. var
  1162. tmpreg: tregister;
  1163. begin
  1164. tmpreg:=getintregister(list,size);
  1165. a_load_subsetreg_reg(list,size,subsetsize,size,sreg,tmpreg);
  1166. a_op_const_reg(list,op,size,a,tmpreg);
  1167. a_load_reg_subsetreg(list,size,size,subsetsize,tmpreg,sreg);
  1168. end;
  1169. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  1170. var
  1171. tmpreg: tregister;
  1172. begin
  1173. tmpreg:=getintregister(list,size);
  1174. a_load_subsetref_reg(list,size,subsetsize,size,sref,tmpreg);
  1175. a_op_const_reg(list,op,size,a,tmpreg);
  1176. a_load_reg_subsetref(list,size,size,subsetsize,tmpreg,sref);
  1177. end;
  1178. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  1179. begin
  1180. case loc.loc of
  1181. LOC_REGISTER, LOC_CREGISTER:
  1182. a_op_const_reg(list,op,size,a,loc.register);
  1183. LOC_REFERENCE, LOC_CREFERENCE:
  1184. a_op_const_ref(list,op,size,a,loc.reference);
  1185. { we don't have enough type information to handle these here
  1186. LOC_SUBSETREG, LOC_CSUBSETREG:
  1187. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1188. LOC_SUBSETREF, LOC_CSUBSETREF:
  1189. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1190. }
  1191. else
  1192. internalerror(2010120428);
  1193. end;
  1194. end;
  1195. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  1196. var
  1197. tmpreg: tregister;
  1198. begin
  1199. tmpreg:=getintregister(list,size);
  1200. a_load_ref_reg(list,size,size,ref,tmpreg);
  1201. case op of
  1202. OP_NOT,OP_NEG:
  1203. begin
  1204. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1205. end;
  1206. else
  1207. begin
  1208. a_op_reg_reg(list,op,size,reg,tmpreg);
  1209. end;
  1210. end;
  1211. a_load_reg_ref(list,size,size,tmpreg,ref);
  1212. end;
  1213. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1214. var
  1215. tmpreg: tregister;
  1216. begin
  1217. case op of
  1218. OP_NOT,OP_NEG:
  1219. { handle it as "load ref,reg; op reg" }
  1220. begin
  1221. a_load_ref_reg(list,size,size,ref,reg);
  1222. a_op_reg_reg(list,op,size,reg,reg);
  1223. end;
  1224. else
  1225. begin
  1226. tmpreg:=getintregister(list,size);
  1227. a_load_ref_reg(list,size,size,ref,tmpreg);
  1228. a_op_reg_reg(list,op,size,tmpreg,reg);
  1229. end;
  1230. end;
  1231. end;
  1232. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  1233. var
  1234. tmpreg: tregister;
  1235. begin
  1236. tmpreg:=getintregister(list,opsize);
  1237. a_load_subsetreg_reg(list,destsize,destsubsetsize,opsize,sreg,tmpreg);
  1238. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1239. a_load_reg_subsetreg(list,opsize,destsize,destsubsetsize,tmpreg,sreg);
  1240. end;
  1241. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  1242. var
  1243. tmpreg: tregister;
  1244. begin
  1245. tmpreg:=getintregister(list,opsize);
  1246. a_load_subsetref_reg(list,destsize,destsubsetsize,opsize,sref,tmpreg);
  1247. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1248. a_load_reg_subsetref(list,opsize,destsize,destsubsetsize,tmpreg,sref);
  1249. end;
  1250. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  1251. begin
  1252. case loc.loc of
  1253. LOC_REGISTER, LOC_CREGISTER:
  1254. a_op_reg_reg(list,op,size,reg,loc.register);
  1255. LOC_REFERENCE, LOC_CREFERENCE:
  1256. a_op_reg_ref(list,op,size,reg,loc.reference);
  1257. { we don't have enough type information to handle these here
  1258. LOC_SUBSETREG, LOC_CSUBSETREG:
  1259. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1260. LOC_SUBSETREF, LOC_CSUBSETREF:
  1261. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1262. }
  1263. else
  1264. internalerror(2010120429);
  1265. end;
  1266. end;
  1267. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  1268. var
  1269. tmpreg: tregister;
  1270. begin
  1271. case loc.loc of
  1272. LOC_REGISTER,LOC_CREGISTER:
  1273. a_op_ref_reg(list,op,size,ref,loc.register);
  1274. LOC_REFERENCE,LOC_CREFERENCE:
  1275. begin
  1276. tmpreg:=getintregister(list,size);
  1277. a_load_ref_reg(list,size,size,ref,tmpreg);
  1278. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1279. end;
  1280. { we don't have enough type information to handle these here
  1281. LOC_SUBSETREG, LOC_CSUBSETREG:
  1282. begin
  1283. tmpreg:=getintregister(list,loc.size);
  1284. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1285. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1286. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1287. end;
  1288. LOC_SUBSETREF, LOC_CSUBSETREF:
  1289. begin
  1290. tmpreg:=getintregister(list,loc.size);
  1291. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1292. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1293. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1294. end;
  1295. }
  1296. else
  1297. internalerror(2010120429);
  1298. end;
  1299. end;
  1300. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  1301. begin
  1302. a_load_reg_reg(list,size,size,src,dst);
  1303. a_op_const_reg(list,op,size,a,dst);
  1304. end;
  1305. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1306. var
  1307. tmpreg: tregister;
  1308. begin
  1309. if (dst<>src1) then
  1310. begin
  1311. a_load_reg_reg(list,size,size,src2,dst);
  1312. a_op_reg_reg(list,op,size,src1,dst);
  1313. end
  1314. else
  1315. begin
  1316. { can we do a direct operation on the target register ? }
  1317. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1318. a_op_reg_reg(list,op,size,src2,dst)
  1319. else
  1320. begin
  1321. tmpreg:=getintregister(list,size);
  1322. a_load_reg_reg(list,size,size,src2,tmpreg);
  1323. a_op_reg_reg(list,op,size,src1,tmpreg);
  1324. a_load_reg_reg(list,size,size,tmpreg,dst);
  1325. end;
  1326. end;
  1327. end;
  1328. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1329. begin
  1330. if not setflags then
  1331. a_op_const_reg_reg(list,op,size,a,src,dst)
  1332. else
  1333. internalerror(2010122910);
  1334. end;
  1335. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1336. begin
  1337. if not setflags then
  1338. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  1339. else
  1340. internalerror(2010122911);
  1341. end;
  1342. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1343. var
  1344. tmpreg: tregister;
  1345. begin
  1346. tmpreg:=getintregister(list,size);
  1347. a_load_const_reg(list,size,a,tmpreg);
  1348. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1349. end;
  1350. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  1351. var
  1352. tmpreg: tregister;
  1353. begin
  1354. tmpreg:=getintregister(list,size);
  1355. a_load_ref_reg(list,size,size,ref,tmpreg);
  1356. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1357. end;
  1358. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  1359. begin
  1360. case loc.loc of
  1361. LOC_REGISTER,LOC_CREGISTER:
  1362. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1363. LOC_REFERENCE,LOC_CREFERENCE:
  1364. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1365. { we don't have enough type information to handle these here
  1366. LOC_SUBSETREG, LOC_CSUBSETREG:
  1367. begin
  1368. tmpreg:=getintregister(list,size);
  1369. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1370. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1371. end;
  1372. LOC_SUBSETREF, LOC_CSUBSETREF:
  1373. begin
  1374. tmpreg:=getintregister(list,size);
  1375. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1376. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1377. end;
  1378. }
  1379. else
  1380. internalerror(2010120430);
  1381. end;
  1382. end;
  1383. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1384. var
  1385. tmpreg: tregister;
  1386. begin
  1387. tmpreg:=getintregister(list,size);
  1388. a_load_ref_reg(list,size,size,ref,tmpreg);
  1389. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1390. end;
  1391. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1392. var
  1393. tmpreg: tregister;
  1394. begin
  1395. tmpreg:=getintregister(list,size);
  1396. a_load_ref_reg(list,size,size,ref,tmpreg);
  1397. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1398. end;
  1399. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  1400. var
  1401. tmpreg: tregister;
  1402. begin
  1403. tmpreg:=getintregister(list,cmpsize);
  1404. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,cmpsize,sreg,tmpreg);
  1405. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1406. end;
  1407. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  1408. var
  1409. tmpreg: tregister;
  1410. begin
  1411. tmpreg:=getintregister(list,cmpsize);
  1412. a_load_subsetref_reg(list,fromsize,fromsubsetsize,cmpsize,sref,tmpreg);
  1413. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1414. end;
  1415. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  1416. begin
  1417. case loc.loc of
  1418. LOC_REGISTER,
  1419. LOC_CREGISTER:
  1420. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1421. LOC_REFERENCE,
  1422. LOC_CREFERENCE :
  1423. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1424. LOC_CONSTANT:
  1425. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1426. { we don't have enough type information to handle these here
  1427. LOC_SUBSETREG,
  1428. LOC_CSUBSETREG:
  1429. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1430. LOC_SUBSETREF,
  1431. LOC_CSUBSETREF:
  1432. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1433. }
  1434. else
  1435. internalerror(2010120431);
  1436. end;
  1437. end;
  1438. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  1439. begin
  1440. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1441. end;
  1442. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  1443. var
  1444. tmpreg: tregister;
  1445. begin
  1446. case loc.loc of
  1447. LOC_REGISTER,LOC_CREGISTER:
  1448. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1449. LOC_REFERENCE,LOC_CREFERENCE:
  1450. begin
  1451. tmpreg:=getintregister(list,size);
  1452. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1453. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1454. end;
  1455. LOC_CONSTANT:
  1456. begin
  1457. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  1458. end
  1459. { we don't have enough type information to handle these here
  1460. LOC_SUBSETREG, LOC_CSUBSETREG:
  1461. begin
  1462. tmpreg:=getintregister(list, size);
  1463. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1464. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1465. end;
  1466. LOC_SUBSETREF, LOC_CSUBSETREF:
  1467. begin
  1468. tmpreg:=getintregister(list, size);
  1469. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1470. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1471. end;
  1472. }
  1473. else
  1474. internalerror(2010120432);
  1475. end;
  1476. end;
  1477. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1478. begin
  1479. {
  1480. if use_vectorfpu(size) then
  1481. a_loadmm_ref_ref()
  1482. else
  1483. }
  1484. if size.typ<>floatdef then
  1485. a_load_ref_ref(list,size,size,source,dest)
  1486. else
  1487. a_loadfpu_ref_ref(list,size,size,source,dest);
  1488. end;
  1489. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  1490. begin
  1491. g_concatcopy(list,size,source,dest);
  1492. end;
  1493. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  1494. begin
  1495. if not(cs_check_range in current_settings.localswitches) then
  1496. exit;
  1497. internalerror(2011010610);
  1498. end;
  1499. procedure thlcgobj.g_profilecode(list: TAsmList);
  1500. begin
  1501. end;
  1502. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  1503. var
  1504. hregister,
  1505. hregister2: tregister;
  1506. hl : tasmlabel;
  1507. oldloc : tlocation;
  1508. begin
  1509. oldloc:=l;
  1510. hregister:=getregisterfordef(list,dst_size);
  1511. { load value in new register }
  1512. case l.loc of
  1513. {$ifdef cpuflags}
  1514. LOC_FLAGS :
  1515. g_flags2reg(list,dst_size,l.resflags,hregister);
  1516. {$endif cpuflags}
  1517. LOC_JUMP :
  1518. begin
  1519. a_label(list,current_procinfo.CurrTrueLabel);
  1520. a_load_const_reg(list,dst_size,1,hregister);
  1521. current_asmdata.getjumplabel(hl);
  1522. a_jmp_always(list,hl);
  1523. a_label(list,current_procinfo.CurrFalseLabel);
  1524. a_load_const_reg(list,dst_size,0,hregister);
  1525. a_label(list,hl);
  1526. end;
  1527. else
  1528. begin
  1529. { load_loc_reg can only handle size >= l.size, when the
  1530. new size is smaller then we need to adjust the size
  1531. of the orignal and maybe recalculate l.register for i386 }
  1532. if (dst_size.size<src_size.size) then
  1533. begin
  1534. hregister2:=getregisterfordef(list,src_size);
  1535. { prevent problems with memory locations -- at this high
  1536. level we cannot twiddle with the reference offset, since
  1537. that may not mean anything (e.g., it refers to fixed-sized
  1538. stack slots on Java) }
  1539. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  1540. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  1541. end
  1542. else
  1543. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  1544. end;
  1545. end;
  1546. if (l.loc <> LOC_CREGISTER) or
  1547. not maybeconst then
  1548. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  1549. else
  1550. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  1551. l.register:=hregister;
  1552. { Release temp if it was a reference }
  1553. if oldloc.loc=LOC_REFERENCE then
  1554. location_freetemp(list,oldloc);
  1555. end;
  1556. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  1557. var
  1558. reg : tregister;
  1559. begin
  1560. if (l.loc<>LOC_FPUREGISTER) and
  1561. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  1562. begin
  1563. { if it's in an mm register, store to memory first }
  1564. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  1565. internalerror(2011012903);
  1566. reg:=getfpuregister(list,size);
  1567. a_loadfpu_loc_reg(list,size,size,l,reg);
  1568. location_freetemp(list,l);
  1569. location_reset(l,LOC_FPUREGISTER,l.size);
  1570. l.register:=reg;
  1571. end;
  1572. end;
  1573. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  1574. var
  1575. r : treference;
  1576. begin
  1577. case l.loc of
  1578. LOC_FPUREGISTER,
  1579. LOC_CFPUREGISTER :
  1580. begin
  1581. tg.gethltemp(list,size,size.size,tt_normal,r);
  1582. a_loadfpu_reg_ref(list,size,size,l.register,r);
  1583. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1584. l.reference:=r;
  1585. end;
  1586. (*
  1587. LOC_MMREGISTER,
  1588. LOC_CMMREGISTER:
  1589. begin
  1590. tg.gethltemp(list,size,size.size,tt_normal,r);
  1591. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  1592. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1593. l.reference:=r;
  1594. end;
  1595. *)
  1596. LOC_CONSTANT,
  1597. LOC_REGISTER,
  1598. LOC_CREGISTER :
  1599. begin
  1600. tg.gethltemp(list,size,size.size,tt_normal,r);
  1601. a_load_loc_ref(list,size,size,l,r);
  1602. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1603. l.reference:=r;
  1604. end;
  1605. (*
  1606. LOC_SUBSETREG,
  1607. LOC_CSUBSETREG,
  1608. LOC_SUBSETREF,
  1609. LOC_CSUBSETREF:
  1610. begin
  1611. tg.gethltemp(list,size,size.size,tt_normal,r);
  1612. cg.a_load_loc_ref(list,l.size,l,r);
  1613. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1614. l.reference:=r;
  1615. end;
  1616. *)
  1617. LOC_CREFERENCE,
  1618. LOC_REFERENCE : ;
  1619. else
  1620. internalerror(2011010304);
  1621. end;
  1622. end;
  1623. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1624. begin
  1625. case l.loc of
  1626. LOC_REGISTER,
  1627. LOC_CREGISTER :
  1628. begin
  1629. if not loadref then
  1630. internalerror(200410231);
  1631. reference_reset_base(ref,l.register,0,alignment);
  1632. end;
  1633. LOC_REFERENCE,
  1634. LOC_CREFERENCE :
  1635. begin
  1636. if loadref then
  1637. begin
  1638. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  1639. { it's a pointer to def }
  1640. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  1641. end
  1642. else
  1643. ref:=l.reference;
  1644. end;
  1645. else
  1646. internalerror(200309181);
  1647. end;
  1648. end;
  1649. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  1650. {
  1651. produces jumps to true respectively false labels using boolean expressions
  1652. depending on whether the loading of regvars is currently being
  1653. synchronized manually (such as in an if-node) or automatically (most of
  1654. the other cases where this procedure is called), loadregvars can be
  1655. "lr_load_regvars" or "lr_dont_load_regvars"
  1656. }
  1657. var
  1658. storepos : tfileposinfo;
  1659. begin
  1660. if nf_error in p.flags then
  1661. exit;
  1662. storepos:=current_filepos;
  1663. current_filepos:=p.fileinfo;
  1664. if is_boolean(p.resultdef) then
  1665. begin
  1666. if is_constboolnode(p) then
  1667. begin
  1668. if Tordconstnode(p).value.uvalue<>0 then
  1669. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  1670. else
  1671. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  1672. end
  1673. else
  1674. begin
  1675. case p.location.loc of
  1676. (*
  1677. LOC_SUBSETREG,LOC_CSUBSETREG,
  1678. LOC_SUBSETREF,LOC_CSUBSETREF:
  1679. begin
  1680. tmpreg := cg.getintregister(list,OS_INT);
  1681. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  1682. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  1683. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1684. end;
  1685. *)
  1686. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  1687. begin
  1688. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  1689. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1690. end;
  1691. LOC_JUMP:
  1692. ;
  1693. {$ifdef cpuflags}
  1694. LOC_FLAGS :
  1695. begin
  1696. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  1697. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1698. end;
  1699. {$endif cpuflags}
  1700. else
  1701. begin
  1702. printnode(output,p);
  1703. internalerror(2011010418);
  1704. end;
  1705. end;
  1706. end;
  1707. end
  1708. else
  1709. internalerror(2011010419);
  1710. current_filepos:=storepos;
  1711. end;
  1712. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  1713. var
  1714. item,
  1715. previtem : TCmdStrListItem;
  1716. begin
  1717. previtem:=nil;
  1718. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1719. while assigned(item) do
  1720. begin
  1721. {$ifdef arm}
  1722. if current_settings.cputype in cpu_thumb2 then
  1723. list.concat(tai_thumb_func.create);
  1724. {$endif arm}
  1725. { "double link" all procedure entry symbols via .reference }
  1726. { directives on darwin, because otherwise the linker }
  1727. { sometimes strips the procedure if only on of the symbols }
  1728. { is referenced }
  1729. if assigned(previtem) and
  1730. (target_info.system in systems_darwin) then
  1731. list.concat(tai_directive.create(asd_reference,item.str));
  1732. if (cs_profile in current_settings.moduleswitches) or
  1733. (po_global in current_procinfo.procdef.procoptions) then
  1734. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1735. else
  1736. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1737. if assigned(previtem) and
  1738. (target_info.system in systems_darwin) then
  1739. list.concat(tai_directive.create(asd_reference,previtem.str));
  1740. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1741. list.concat(Tai_function_name.create(item.str));
  1742. previtem:=item;
  1743. item := TCmdStrListItem(item.next);
  1744. end;
  1745. current_procinfo.procdef.procstarttai:=tai(list.last);
  1746. end;
  1747. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  1748. begin
  1749. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1750. current_procinfo.procdef.procendtai:=tai(list.last);
  1751. if (current_module.islibrary) then
  1752. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1753. { setinitname may generate a new section -> don't add to the
  1754. current list, because we assume this remains a text section }
  1755. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  1756. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1757. begin
  1758. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1759. not(current_module.islibrary) then
  1760. begin
  1761. new_section(list,sec_code,'',4);
  1762. list.concat(tai_symbol.createname_global(
  1763. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1764. { keep argc, argv and envp properly on the stack }
  1765. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1766. end;
  1767. end;
  1768. end;
  1769. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  1770. begin
  1771. { initialize local data like ansistrings }
  1772. case current_procinfo.procdef.proctypeoption of
  1773. potype_unitinit:
  1774. begin
  1775. { this is also used for initialization of variables in a
  1776. program which does not have a globalsymtable }
  1777. if assigned(current_module.globalsymtable) then
  1778. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1779. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1780. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1781. end;
  1782. { units have seperate code for initilization and finalization }
  1783. potype_unitfinalize: ;
  1784. { program init/final is generated in separate procedure }
  1785. potype_proginit:
  1786. begin
  1787. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1788. end;
  1789. else
  1790. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1791. end;
  1792. { initialisizes temp. ansi/wide string data }
  1793. inittempvariables(list);
  1794. {$ifdef OLDREGVARS}
  1795. load_regvars(list,nil);
  1796. {$endif OLDREGVARS}
  1797. end;
  1798. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  1799. begin
  1800. {$ifdef OLDREGVARS}
  1801. cleanup_regvars(list);
  1802. {$endif OLDREGVARS}
  1803. { finalize temporary data }
  1804. finalizetempvariables(list);
  1805. { finalize local data like ansistrings}
  1806. case current_procinfo.procdef.proctypeoption of
  1807. potype_unitfinalize:
  1808. begin
  1809. { this is also used for initialization of variables in a
  1810. program which does not have a globalsymtable }
  1811. if assigned(current_module.globalsymtable) then
  1812. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1813. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1814. end;
  1815. { units/progs have separate code for initialization and finalization }
  1816. potype_unitinit: ;
  1817. { program init/final is generated in separate procedure }
  1818. potype_proginit: ;
  1819. else
  1820. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1821. end;
  1822. { finalize paras data }
  1823. if assigned(current_procinfo.procdef.parast) and
  1824. not(po_assembler in current_procinfo.procdef.procoptions) then
  1825. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1826. end;
  1827. procedure thlcgobj.gen_entry_code(list: TAsmList);
  1828. begin
  1829. { the actual profile code can clobber some registers,
  1830. therefore if the context must be saved, do it before
  1831. the actual call to the profile code
  1832. }
  1833. if (cs_profile in current_settings.moduleswitches) and
  1834. not(po_assembler in current_procinfo.procdef.procoptions) then
  1835. begin
  1836. { non-win32 can call mcout even in main }
  1837. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1838. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1839. begin
  1840. g_profilecode(list);
  1841. end;
  1842. end;
  1843. { TODO: create high level version (create compilerprocs in system unit,
  1844. look up procdef, use hlcgobj.a_call_name()) }
  1845. { call startup helpers from main program }
  1846. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1847. begin
  1848. { initialize units }
  1849. cg.allocallcpuregisters(list);
  1850. if not(current_module.islibrary) then
  1851. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  1852. else
  1853. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  1854. cg.deallocallcpuregisters(list);
  1855. end;
  1856. list.concat(Tai_force_line.Create);
  1857. {$ifdef OLDREGVARS}
  1858. load_regvars(list,nil);
  1859. {$endif OLDREGVARS}
  1860. end;
  1861. procedure thlcgobj.gen_exit_code(list: TAsmList);
  1862. begin
  1863. { TODO: create high level version (create compilerproc in system unit,
  1864. look up procdef, use hlcgobj.a_call_name()) }
  1865. { call __EXIT for main program }
  1866. if (not DLLsource) and
  1867. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1868. cg.a_call_name(list,'FPC_DO_EXIT',false);
  1869. end;
  1870. procedure thlcgobj.inittempvariables(list: TAsmList);
  1871. var
  1872. hp : ptemprecord;
  1873. href : treference;
  1874. begin
  1875. hp:=tg.templist;
  1876. while assigned(hp) do
  1877. begin
  1878. if assigned(hp^.def) and
  1879. is_managed_type(hp^.def) then
  1880. begin
  1881. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1882. g_initialize(list,hp^.def,href);
  1883. end;
  1884. hp:=hp^.next;
  1885. end;
  1886. end;
  1887. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  1888. var
  1889. OldAsmList : TAsmList;
  1890. hp : tnode;
  1891. begin
  1892. if (tsym(p).typ = localvarsym) and
  1893. { local (procedure or unit) variables only need initialization if
  1894. they are used }
  1895. ((tabstractvarsym(p).refs>0) or
  1896. { managed return symbols must be inited }
  1897. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  1898. ) and
  1899. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  1900. not(vo_is_external in tabstractvarsym(p).varoptions) and
  1901. (is_managed_type(tabstractvarsym(p).vardef) or
  1902. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  1903. ) then
  1904. begin
  1905. OldAsmList:=current_asmdata.CurrAsmList;
  1906. current_asmdata.CurrAsmList:=TAsmList(arg);
  1907. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  1908. firstpass(hp);
  1909. secondpass(hp);
  1910. hp.free;
  1911. current_asmdata.CurrAsmList:=OldAsmList;
  1912. end;
  1913. end;
  1914. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  1915. var
  1916. hp : ptemprecord;
  1917. href : treference;
  1918. begin
  1919. hp:=tg.templist;
  1920. while assigned(hp) do
  1921. begin
  1922. if assigned(hp^.def) and
  1923. is_managed_type(hp^.def) then
  1924. begin
  1925. include(current_procinfo.flags,pi_needs_implicit_finally);
  1926. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1927. g_finalize(list,hp^.def,href);
  1928. end;
  1929. hp:=hp^.next;
  1930. end;
  1931. end;
  1932. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  1933. var
  1934. href : treference;
  1935. begin
  1936. if (tsym(p).typ=staticvarsym) then
  1937. begin
  1938. { Static variables can have the initialloc only set to LOC_CxREGISTER
  1939. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  1940. case tstaticvarsym(p).initialloc.loc of
  1941. LOC_CREGISTER :
  1942. begin
  1943. {$ifndef cpu64bitalu}
  1944. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  1945. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  1946. else
  1947. {$endif not cpu64bitalu}
  1948. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  1949. tstaticvarsym(p).initialloc.register);
  1950. end;
  1951. (*
  1952. LOC_CMMREGISTER :
  1953. { clear the whole register }
  1954. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  1955. tstaticvarsym(p).initialloc.register,
  1956. tstaticvarsym(p).initialloc.register,
  1957. nil);
  1958. *)
  1959. LOC_CFPUREGISTER :
  1960. begin
  1961. { initialize fpu regvar by loading from memory }
  1962. reference_reset_symbol(href,
  1963. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  1964. var_align(tstaticvarsym(p).vardef.alignment));
  1965. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  1966. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  1967. end;
  1968. LOC_INVALID :
  1969. ;
  1970. else
  1971. internalerror(200410124);
  1972. end;
  1973. end;
  1974. end;
  1975. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  1976. var
  1977. hp : tnode;
  1978. OldAsmList : TAsmList;
  1979. begin
  1980. include(current_procinfo.flags,pi_needs_implicit_finally);
  1981. OldAsmList:=current_asmdata.CurrAsmList;
  1982. current_asmdata.CurrAsmList:=asmlist;
  1983. hp:=cloadnode.create(sym,sym.owner);
  1984. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  1985. include(hp.flags,nf_isinternal_ignoreconst);
  1986. hp:=cnodeutils.finalize_data_node(hp);
  1987. firstpass(hp);
  1988. secondpass(hp);
  1989. hp.free;
  1990. current_asmdata.CurrAsmList:=OldAsmList;
  1991. end;
  1992. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  1993. begin
  1994. if (tsym(p).typ=localvarsym) and
  1995. (tlocalvarsym(p).refs>0) and
  1996. not(vo_is_external in tlocalvarsym(p).varoptions) and
  1997. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  1998. is_managed_type(tlocalvarsym(p).vardef) then
  1999. finalize_sym(TAsmList(arg),tsym(p));
  2000. end;
  2001. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  2002. var
  2003. i : longint;
  2004. pd : tprocdef;
  2005. begin
  2006. case tsym(p).typ of
  2007. staticvarsym :
  2008. begin
  2009. { local (procedure or unit) variables only need finalization
  2010. if they are used
  2011. }
  2012. if ((tstaticvarsym(p).refs>0) or
  2013. { global (unit) variables always need finalization, since
  2014. they may also be used in another unit
  2015. }
  2016. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  2017. (
  2018. (tstaticvarsym(p).varspez<>vs_const) or
  2019. (vo_force_finalize in tstaticvarsym(p).varoptions)
  2020. ) and
  2021. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  2022. not(vo_is_external in tstaticvarsym(p).varoptions) and
  2023. is_managed_type(tstaticvarsym(p).vardef) then
  2024. finalize_sym(TAsmList(arg),tsym(p));
  2025. end;
  2026. procsym :
  2027. begin
  2028. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  2029. begin
  2030. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  2031. if assigned(pd.localst) and
  2032. (pd.procsym=tprocsym(p)) and
  2033. (pd.localst.symtabletype<>staticsymtable) then
  2034. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  2035. end;
  2036. end;
  2037. end;
  2038. end;
  2039. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  2040. var
  2041. list : TAsmList;
  2042. href : treference;
  2043. hsym : tparavarsym;
  2044. eldef : tdef;
  2045. highloc : tlocation;
  2046. begin
  2047. if not(tsym(p).typ=paravarsym) then
  2048. exit;
  2049. list:=TAsmList(arg);
  2050. if is_managed_type(tparavarsym(p).vardef) then
  2051. begin
  2052. if (tparavarsym(p).varspez=vs_value) then
  2053. begin
  2054. include(current_procinfo.flags,pi_needs_implicit_finally);
  2055. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2056. if is_open_array(tparavarsym(p).vardef) then
  2057. begin
  2058. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2059. begin
  2060. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2061. if not assigned(hsym) then
  2062. internalerror(201003032);
  2063. highloc:=hsym.initialloc
  2064. end
  2065. else
  2066. highloc.loc:=LOC_INVALID;
  2067. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2068. g_array_rtti_helper(list,eldef,href,highloc,'FPC_DECREF_ARRAY');
  2069. end
  2070. else
  2071. g_decrrefcount(list,tparavarsym(p).vardef,href);
  2072. end;
  2073. end;
  2074. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  2075. if (tparavarsym(p).varspez=vs_value) and
  2076. (is_open_array(tparavarsym(p).vardef) or
  2077. is_array_of_const(tparavarsym(p).vardef)) then
  2078. begin
  2079. { cdecl functions don't have a high pointer so it is not possible to generate
  2080. a local copy }
  2081. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2082. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  2083. end;
  2084. end;
  2085. { generates the code for incrementing the reference count of parameters and
  2086. initialize out parameters }
  2087. { generates the code for incrementing the reference count of parameters and
  2088. initialize out parameters }
  2089. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  2090. var
  2091. href : treference;
  2092. hsym : tparavarsym;
  2093. eldef : tdef;
  2094. list : TAsmList;
  2095. highloc : tlocation;
  2096. needs_inittable (*,
  2097. do_trashing *) : boolean;
  2098. begin
  2099. list:=TAsmList(arg);
  2100. if (tsym(p).typ=paravarsym) then
  2101. begin
  2102. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  2103. (*
  2104. do_trashing:=
  2105. (localvartrashing <> -1) and
  2106. (not assigned(tparavarsym(p).defaultconstsym)) and
  2107. not needs_inittable;
  2108. *)
  2109. case tparavarsym(p).varspez of
  2110. vs_value :
  2111. if needs_inittable then
  2112. begin
  2113. { variants are already handled by the call to fpc_variant_copy_overwrite if
  2114. they are passed by reference }
  2115. if not((tparavarsym(p).vardef.typ=variantdef) and
  2116. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2117. begin
  2118. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2119. if is_open_array(tparavarsym(p).vardef) then
  2120. begin
  2121. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2122. begin
  2123. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2124. if not assigned(hsym) then
  2125. internalerror(201003032);
  2126. highloc:=hsym.initialloc
  2127. end
  2128. else
  2129. highloc.loc:=LOC_INVALID;
  2130. { open arrays do not contain correct element count in their rtti,
  2131. the actual count must be passed separately. }
  2132. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2133. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  2134. end
  2135. else
  2136. g_incrrefcount(list,tparavarsym(p).vardef,href);
  2137. end;
  2138. end;
  2139. vs_out :
  2140. begin
  2141. if needs_inittable (*or
  2142. do_trashing*) then
  2143. begin
  2144. { we have no idea about the alignment at the callee side,
  2145. and the user also cannot specify "unaligned" here, so
  2146. assume worst case }
  2147. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2148. (*
  2149. if do_trashing and
  2150. { needs separate implementation to trash open arrays }
  2151. { since their size is only known at run time }
  2152. not is_special_array(tparavarsym(p).vardef) then
  2153. { may be an open string, even if is_open_string() returns }
  2154. { false (for some helpers in the system unit) }
  2155. if not is_shortstring(tparavarsym(p).vardef) then
  2156. trash_reference(list,href,tparavarsym(p).vardef.size)
  2157. else
  2158. trash_reference(list,href,2);
  2159. *)
  2160. if needs_inittable then
  2161. begin
  2162. if is_open_array(tparavarsym(p).vardef) then
  2163. begin
  2164. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2165. begin
  2166. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2167. if not assigned(hsym) then
  2168. internalerror(201003032);
  2169. highloc:=hsym.initialloc
  2170. end
  2171. else
  2172. highloc.loc:=LOC_INVALID;
  2173. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2174. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  2175. end
  2176. else
  2177. g_initialize(list,tparavarsym(p).vardef,href);
  2178. end;
  2179. end;
  2180. end;
  2181. (*
  2182. else if do_trashing and
  2183. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  2184. begin
  2185. { should always have standard alignment. If a function is assigned
  2186. to a non-aligned variable, the optimisation to pass this variable
  2187. directly as hidden function result must/cannot be performed
  2188. (see tcallnode.funcret_can_be_reused)
  2189. }
  2190. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  2191. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  2192. { may be an open string, even if is_open_string() returns }
  2193. { false (for some helpers in the system unit) }
  2194. if not is_shortstring(tparavarsym(p).vardef) then
  2195. trash_reference(list,href,tparavarsym(p).vardef.size)
  2196. else
  2197. { an open string has at least size 2 }
  2198. trash_reference(list,href,2);
  2199. end
  2200. *)
  2201. end;
  2202. end;
  2203. end;
  2204. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  2205. var
  2206. i: longint;
  2207. currpara: tparavarsym;
  2208. begin
  2209. if (po_assembler in current_procinfo.procdef.procoptions) then
  2210. exit;
  2211. { Copy parameters to local references/registers }
  2212. for i:=0 to current_procinfo.procdef.paras.count-1 do
  2213. begin
  2214. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  2215. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  2216. end;
  2217. { generate copies of call by value parameters, must be done before
  2218. the initialization and body is parsed because the refcounts are
  2219. incremented using the local copies }
  2220. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  2221. if not(po_assembler in current_procinfo.procdef.procoptions) then
  2222. begin
  2223. { has to be done here rather than in gen_initialize_code, because
  2224. the initialisation code is generated a) later and b) with
  2225. rad_backwards, so the register allocator would generate
  2226. information as if this code comes before loading the parameters
  2227. from their original registers to their local location }
  2228. // if (localvartrashing <> -1) then
  2229. // current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  2230. { initialize refcounted paras, and trash others. Needed here
  2231. instead of in gen_initialize_code, because when a reference is
  2232. intialised or trashed while the pointer to that reference is kept
  2233. in a regvar, we add a register move and that one again has to
  2234. come after the parameter loading code as far as the register
  2235. allocator is concerned }
  2236. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  2237. end;
  2238. end;
  2239. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  2240. var
  2241. href : treference;
  2242. hreg : tregister;
  2243. list : TAsmList;
  2244. hsym : tparavarsym;
  2245. l : longint;
  2246. highloc,
  2247. localcopyloc : tlocation;
  2248. begin
  2249. list:=TAsmList(arg);
  2250. if (tsym(p).typ=paravarsym) and
  2251. (tparavarsym(p).varspez=vs_value) and
  2252. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2253. begin
  2254. { we have no idea about the alignment at the caller side }
  2255. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2256. if is_open_array(tparavarsym(p).vardef) or
  2257. is_array_of_const(tparavarsym(p).vardef) then
  2258. begin
  2259. { cdecl functions don't have a high pointer so it is not possible to generate
  2260. a local copy }
  2261. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2262. begin
  2263. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2264. begin
  2265. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2266. if not assigned(hsym) then
  2267. internalerror(2011020506);
  2268. highloc:=hsym.initialloc
  2269. end
  2270. else
  2271. highloc.loc:=LOC_INVALID;
  2272. hreg:=getaddressregister(list,voidpointertype);
  2273. if not is_packed_array(tparavarsym(p).vardef) then
  2274. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  2275. else
  2276. internalerror(2011020507);
  2277. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  2278. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  2279. end;
  2280. end
  2281. else
  2282. begin
  2283. { Allocate space for the local copy }
  2284. l:=tparavarsym(p).getsize;
  2285. localcopyloc.loc:=LOC_REFERENCE;
  2286. localcopyloc.size:=int_cgsize(l);
  2287. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  2288. { Copy data }
  2289. if is_shortstring(tparavarsym(p).vardef) then
  2290. begin
  2291. { this code is only executed before the code for the body and the entry/exit code is generated
  2292. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2293. }
  2294. include(current_procinfo.flags,pi_do_call);
  2295. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  2296. end
  2297. else if tparavarsym(p).vardef.typ=variantdef then
  2298. begin
  2299. { this code is only executed before the code for the body and the entry/exit code is generated
  2300. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2301. }
  2302. include(current_procinfo.flags,pi_do_call);
  2303. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  2304. end
  2305. else
  2306. begin
  2307. { pass proper alignment info }
  2308. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  2309. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  2310. end;
  2311. { update localloc of varsym }
  2312. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  2313. tparavarsym(p).localloc:=localcopyloc;
  2314. tparavarsym(p).initialloc:=localcopyloc;
  2315. end;
  2316. end;
  2317. end;
  2318. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  2319. begin
  2320. case l.loc of
  2321. (*
  2322. LOC_MMREGISTER,
  2323. LOC_CMMREGISTER:
  2324. case cgpara.location^.loc of
  2325. LOC_REFERENCE,
  2326. LOC_CREFERENCE,
  2327. LOC_MMREGISTER,
  2328. LOC_CMMREGISTER,
  2329. LOC_REGISTER,
  2330. LOC_CREGISTER :
  2331. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  2332. LOC_FPUREGISTER,
  2333. LOC_CFPUREGISTER:
  2334. begin
  2335. tmploc:=l;
  2336. location_force_fpureg(list,tmploc,false);
  2337. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  2338. end;
  2339. else
  2340. internalerror(200204249);
  2341. end;
  2342. *)
  2343. LOC_FPUREGISTER,
  2344. LOC_CFPUREGISTER:
  2345. case cgpara.location^.loc of
  2346. (*
  2347. LOC_MMREGISTER,
  2348. LOC_CMMREGISTER:
  2349. begin
  2350. tmploc:=l;
  2351. location_force_mmregscalar(list,tmploc,false);
  2352. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  2353. end;
  2354. *)
  2355. { Some targets pass floats in normal registers }
  2356. LOC_REGISTER,
  2357. LOC_CREGISTER,
  2358. LOC_REFERENCE,
  2359. LOC_CREFERENCE,
  2360. LOC_FPUREGISTER,
  2361. LOC_CFPUREGISTER:
  2362. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  2363. else
  2364. internalerror(2011010210);
  2365. end;
  2366. LOC_REFERENCE,
  2367. LOC_CREFERENCE:
  2368. case cgpara.location^.loc of
  2369. (*
  2370. LOC_MMREGISTER,
  2371. LOC_CMMREGISTER:
  2372. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  2373. *)
  2374. { Some targets pass floats in normal registers }
  2375. LOC_REGISTER,
  2376. LOC_CREGISTER,
  2377. LOC_REFERENCE,
  2378. LOC_CREFERENCE,
  2379. LOC_FPUREGISTER,
  2380. LOC_CFPUREGISTER:
  2381. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  2382. else
  2383. internalerror(2011010211);
  2384. end;
  2385. LOC_REGISTER,
  2386. LOC_CREGISTER :
  2387. a_load_loc_cgpara(list,size,l,cgpara);
  2388. else
  2389. internalerror(2011010212);
  2390. end;
  2391. end;
  2392. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  2393. begin
  2394. { do nothing by default }
  2395. end;
  2396. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  2397. begin
  2398. { Handle Floating point types differently
  2399. This doesn't depend on emulator settings, emulator settings should
  2400. be handled by cpupara }
  2401. if (vardef.typ=floatdef) or
  2402. { some ABIs return certain records in an fpu register }
  2403. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  2404. (assigned(cgpara.location) and
  2405. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  2406. begin
  2407. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  2408. exit;
  2409. end;
  2410. case l.loc of
  2411. LOC_CONSTANT,
  2412. LOC_REGISTER,
  2413. LOC_CREGISTER,
  2414. LOC_REFERENCE,
  2415. LOC_CREFERENCE :
  2416. begin
  2417. a_load_loc_cgpara(list,vardef,l,cgpara);
  2418. end;
  2419. (*
  2420. LOC_MMREGISTER,
  2421. LOC_CMMREGISTER:
  2422. begin
  2423. case l.size of
  2424. OS_F32,
  2425. OS_F64:
  2426. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  2427. else
  2428. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  2429. end;
  2430. end;
  2431. *)
  2432. else
  2433. internalerror(2011010213);
  2434. end;
  2435. end;
  2436. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  2437. var
  2438. href : treference;
  2439. begin
  2440. para.check_simple_location;
  2441. { skip e.g. empty records }
  2442. if (para.location^.loc = LOC_VOID) then
  2443. exit;
  2444. case destloc.loc of
  2445. LOC_REFERENCE :
  2446. begin
  2447. { If the parameter location is reused we don't need to copy
  2448. anything }
  2449. if not reusepara then
  2450. begin
  2451. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  2452. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  2453. end;
  2454. end;
  2455. { TODO other possible locations }
  2456. else
  2457. internalerror(2011010308);
  2458. end;
  2459. end;
  2460. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  2461. var
  2462. ressym : tabstractnormalvarsym;
  2463. funcretloc : TCGPara;
  2464. begin
  2465. { Is the loading needed? }
  2466. if is_void(current_procinfo.procdef.returndef) or
  2467. (
  2468. (po_assembler in current_procinfo.procdef.procoptions) and
  2469. (not(assigned(current_procinfo.procdef.funcretsym)) or
  2470. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  2471. ) then
  2472. exit;
  2473. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  2474. { constructors return self }
  2475. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2476. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  2477. else
  2478. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  2479. if (ressym.refs>0) or
  2480. is_managed_type(ressym.vardef) then
  2481. begin
  2482. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  2483. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2484. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  2485. end
  2486. else
  2487. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  2488. end;
  2489. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  2490. begin
  2491. { add the procedure to the al_procedures }
  2492. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  2493. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  2494. current_asmdata.asmlists[al_procedures].concatlist(code);
  2495. { save local data (casetable) also in the same file }
  2496. if assigned(data) and
  2497. (not data.empty) then
  2498. current_asmdata.asmlists[al_procedures].concatlist(data);
  2499. end;
  2500. end.