hlcgobj.pas 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073
  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. tsubsetloadopt = (SL_REG,SL_REGNOSRCMASK,SL_SETZERO,SL_SETMAX);
  36. {# @abstract(Abstract high level code generator)
  37. This class implements an abstract instruction generator. All
  38. methods of this class are generic and are mapped to low level code
  39. generator methods by default. They have to be overridden for higher
  40. level targets
  41. }
  42. { thlcgobj }
  43. thlcgobj = class
  44. public
  45. {************************************************}
  46. { basic routines }
  47. constructor create;
  48. {# Initialize the register allocators needed for the codegenerator.}
  49. procedure init_register_allocators;virtual;
  50. {# Clean up the register allocators needed for the codegenerator.}
  51. procedure done_register_allocators;virtual;
  52. {# 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. }
  53. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  54. {# Gets a register suitable to do integer operations on.}
  55. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  56. {# Gets a register suitable to do integer operations on.}
  57. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  58. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  59. // we don't have high level defs yet that translate into all mm cgsizes
  60. // function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  61. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  62. function getregisterfordef(list: TAsmList;size:tdef):Tregister;virtual;
  63. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  64. the cpu specific child cg object have such a method?}
  65. function uses_registers(rt:Tregistertype):boolean; inline;
  66. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  67. procedure translate_register(var reg : tregister); inline;
  68. {# Returns the kind of register this type should be loaded in (it does not
  69. check whether this is actually possible, but if it's loaded in a register
  70. by the compiler for any purpose other than parameter passing/function
  71. result loading, this is the register type used }
  72. function def2regtyp(def: tdef): tregistertype; virtual;
  73. { # Returns orddef corresponding to size }
  74. class function tcgsize2orddef(size: tcgsize): torddef; static;
  75. {# Emit a label to the instruction stream. }
  76. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  77. {# Allocates register r by inserting a pai_realloc record }
  78. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  79. {# Deallocates register r by inserting a pa_regdealloc record}
  80. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  81. { Synchronize register, make sure it is still valid }
  82. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  83. {# Pass a parameter, which is located in a register, to a routine.
  84. This routine should push/send the parameter to the routine, as
  85. required by the specific processor ABI and routine modifiers.
  86. It must generate register allocation information for the cgpara in
  87. case it consists of cpuregisters.
  88. @param(size size of the operand in the register)
  89. @param(r register source of the operand)
  90. @param(cgpara where the parameter will be stored)
  91. }
  92. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  93. {# Pass a parameter, which is a constant, to a routine.
  94. A generic version is provided. This routine should
  95. be overridden for optimization purposes if the cpu
  96. permits directly sending this type of parameter.
  97. It must generate register allocation information for the cgpara in
  98. case it consists of cpuregisters.
  99. @param(size size of the operand in constant)
  100. @param(a value of constant to send)
  101. @param(cgpara where the parameter will be stored)
  102. }
  103. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : aint;const cgpara : TCGPara);virtual;
  104. {# Pass the value of a parameter, which is located in memory, to a routine.
  105. A generic version is provided. This routine should
  106. be overridden for optimization purposes if the cpu
  107. permits directly sending this type of parameter.
  108. It must generate register allocation information for the cgpara in
  109. case it consists of cpuregisters.
  110. @param(size size of the operand in constant)
  111. @param(r Memory reference of value to send)
  112. @param(cgpara where the parameter will be stored)
  113. }
  114. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  115. {# Pass the value of a parameter, which can be located either in a register or memory location,
  116. to a routine.
  117. A generic version is provided.
  118. @param(l location of the operand to send)
  119. @param(nr parameter number (starting from one) of routine (from left to right))
  120. @param(cgpara where the parameter will be stored)
  121. }
  122. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  123. {# Pass the address of a reference to a routine. This routine
  124. will calculate the address of the reference, and pass this
  125. calculated address as a parameter.
  126. It must generate register allocation information for the cgpara in
  127. case it consists of cpuregisters.
  128. A generic version is provided. This routine should
  129. be overridden for optimization purposes if the cpu
  130. permits directly sending this type of parameter.
  131. @param(fromsize type of the reference we are taking the address of)
  132. @param(tosize type of the pointer that we get as a result)
  133. @param(r reference to get address from)
  134. }
  135. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize, tosize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  136. { Remarks:
  137. * If a method specifies a size you have only to take care
  138. of that number of bits, i.e. load_const_reg with OP_8 must
  139. only load the lower 8 bit of the specified register
  140. the rest of the register can be undefined
  141. if necessary the compiler will call a method
  142. to zero or sign extend the register
  143. * The a_load_XX_XX with OP_64 needn't to be
  144. implemented for 32 bit
  145. processors, the code generator takes care of that
  146. * the addr size is for work with the natural pointer
  147. size
  148. * the procedures without fpu/mm are only for integer usage
  149. * normally the first location is the source and the
  150. second the destination
  151. }
  152. {# Emits instruction to call the method specified by symbol name.
  153. This routine must be overridden for each new target cpu.
  154. }
  155. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; weak: boolean);virtual;abstract;
  156. procedure a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister);virtual;abstract;
  157. procedure a_call_ref(list : TAsmList;pd : tabstractprocdef;const ref : treference);virtual;
  158. { same as a_call_name, might be overridden on certain architectures to emit
  159. static calls without usage of a got trampoline }
  160. procedure a_call_name_static(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  161. { same as a_call_name, might be overridden on certain architectures to emit
  162. special static calls for inherited methods }
  163. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  164. { move instructions }
  165. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);virtual;abstract;
  166. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);virtual;
  167. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : aint;const loc : tlocation);virtual;
  168. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  169. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  170. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  171. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  172. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  173. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  174. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  175. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  176. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  177. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  178. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  179. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  180. { For subsetreg/ref, the def is the size of the packed element. The
  181. size of the register that holds the data is a tcgsize, and hence
  182. always must be an orddef of the corresponding size in practice }
  183. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual;
  184. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual;
  185. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual;
  186. procedure a_load_subsetreg_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual;
  187. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual;
  188. procedure a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister); virtual;
  189. procedure a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  190. procedure a_load_subsetref_reg(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual;
  191. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual;
  192. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual;
  193. procedure a_load_subsetref_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual;
  194. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual;
  195. procedure a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference); virtual;
  196. procedure a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  197. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  198. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  199. { bit test instructions }
  200. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual;
  201. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual;
  202. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual;
  203. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual;
  204. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual;
  205. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  206. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  207. { bit set/clear instructions }
  208. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual;
  209. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: aint; const ref: treference); virtual;
  210. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister); virtual;
  211. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister); virtual;
  212. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual;
  213. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  214. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);virtual;
  215. protected
  216. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  217. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  218. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  219. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  220. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  221. { return a subsetref that represents bit "bitnumber" in ref, if ref
  222. has the type "refdef". The subsetref must be addressable via
  223. (unsigned) 8 bit access, unless all the *_bit_* methods are
  224. overloaded and use something else. }
  225. function get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  226. function get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  227. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  228. public
  229. { bit scan instructions (still need transformation to thlcgobj) }
  230. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  231. { fpu move instructions }
  232. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  233. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  234. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  235. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  236. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  237. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  238. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  239. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  240. { vector register move instructions }
  241. // we don't have high level defs yet that translate into all mm cgsizes
  242. {
  243. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  244. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  245. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  246. }
  247. { required for subsetreg/ref; still tcgsize rather than tdef because of reason mentioned above }
  248. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual; abstract;
  249. {
  250. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  251. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  252. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  253. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  254. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  255. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  256. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  257. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  258. }
  259. // we don't have high level defs yet that translate into all mm cgsizes
  260. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  261. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  262. { basic arithmetic operations }
  263. { note: for operators which require only one argument (not, neg), use }
  264. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  265. { that in this case the *second* operand is used as both source and }
  266. { destination (JM) }
  267. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  268. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  269. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  270. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  271. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  272. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  273. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  274. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  275. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  276. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  277. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  278. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  279. { trinary operations for processors that support them, 'emulated' }
  280. { on others. None with "ref" arguments since I don't think there }
  281. { are any processors that support it (JM) }
  282. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  283. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  284. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  285. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  286. { comparison operations }
  287. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  288. l : tasmlabel);virtual;
  289. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  290. l : tasmlabel); virtual;
  291. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  292. l : tasmlabel);virtual;
  293. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  294. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  295. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  296. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  297. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  298. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  299. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  300. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  301. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  302. {$ifdef cpuflags}
  303. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  304. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  305. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  306. }
  307. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  308. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  309. {$endif cpuflags}
  310. // procedure g_maybe_testself(list : TAsmList;reg:tregister);
  311. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  312. {# This should emit the opcode to copy len bytes from the source
  313. to destination.
  314. It must be overridden for each new target processor.
  315. @param(source Source reference of copy)
  316. @param(dest Destination reference of copy)
  317. }
  318. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  319. {# This should emit the opcode to copy len bytes from the an unaligned source
  320. to destination.
  321. It must be overridden for each new target processor.
  322. @param(source Source reference of copy)
  323. @param(dest Destination reference of copy)
  324. }
  325. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  326. {# This should emit the opcode to a shortrstring from the source
  327. to destination.
  328. @param(source Source reference of copy)
  329. @param(dest Destination reference of copy)
  330. }
  331. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;abstract;
  332. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;abstract;
  333. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  334. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  335. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  336. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  337. const name: string);virtual;abstract;
  338. {# Generates range checking code. It is to note
  339. that this routine does not need to be overridden,
  340. as it takes care of everything.
  341. @param(p Node which contains the value to check)
  342. @param(todef Type definition of node to range check)
  343. }
  344. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  345. {# Generates overflow checking code for a node }
  346. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  347. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  348. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;abstract;
  349. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;abstract;
  350. {# Emits instructions when compilation is done in profile
  351. mode (this is set as a command line option). The default
  352. behavior does nothing, should be overridden as required.
  353. }
  354. procedure g_profilecode(list : TAsmList);virtual;
  355. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  356. @param(size Number of bytes to allocate)
  357. }
  358. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  359. {# Emits instruction for allocating the locals in entry
  360. code of a routine. This is one of the first
  361. routine called in @var(genentrycode).
  362. @param(localsize Number of bytes to allocate as locals)
  363. }
  364. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  365. {# Emits instructions for returning from a subroutine.
  366. Should also restore the framepointer and stack.
  367. @param(parasize Number of bytes of parameters to deallocate from stack)
  368. }
  369. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  370. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  371. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  372. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual; abstract;
  373. { generate a stub which only purpose is to pass control the given external method,
  374. setting up any additional environment before doing so (if required).
  375. The default implementation issues a jump instruction to the external name. }
  376. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  377. protected
  378. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  379. public
  380. { create "safe copy" of a tlocation that can be used later: all
  381. registers used in the tlocation are copied to new ones, so that
  382. even if the original ones change, things stay the same (except if
  383. the original location was already a register, then the register is
  384. kept). Must only be used on lvalue locations.
  385. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  386. for targets without pointers. }
  387. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  388. { routines migrated from ncgutil }
  389. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  390. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  391. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  392. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  393. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  394. { Retrieve the location of the data pointed to in location l, when the location is
  395. a register it is expected to contain the address of the data }
  396. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  397. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  398. procedure gen_proc_symbol(list:TAsmList);virtual;
  399. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  400. procedure gen_initialize_code(list:TAsmList);virtual;
  401. procedure gen_finalize_code(list:TAsmList);virtual;
  402. procedure gen_entry_code(list:TAsmList);virtual;
  403. procedure gen_exit_code(list:TAsmList);virtual;
  404. protected
  405. { helpers called by gen_initialize_code/gen_finalize_code }
  406. procedure inittempvariables(list:TAsmList);virtual;
  407. procedure initialize_data(p:TObject;arg:pointer);virtual;
  408. procedure finalizetempvariables(list:TAsmList);virtual;
  409. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  410. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  411. { generates the code for finalisation of local variables }
  412. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  413. { generates the code for finalization of static symtable and
  414. all local (static) typed consts }
  415. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  416. { generates the code for decrementing the reference count of parameters }
  417. procedure final_paras(p:TObject;arg:pointer);
  418. public
  419. procedure gen_load_para_value(list:TAsmList);virtual;
  420. protected
  421. { helpers called by gen_load_para_value }
  422. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  423. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  424. procedure init_paras(p:TObject;arg:pointer);
  425. protected
  426. { Some targets have to put "something" in the function result
  427. location if it's not initialised by the Pascal code, e.g.
  428. stack-based architectures. By default it does nothing }
  429. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  430. public
  431. { load a tlocation into a cgpara }
  432. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  433. { load a cgpara into a tlocation }
  434. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  435. { load the function return value into the ABI-defined function return location }
  436. procedure gen_load_return_value(list:TAsmList);virtual;
  437. { extras refactored from other units }
  438. { queue the code/data generated for a procedure for writing out to
  439. the assembler/object file }
  440. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  441. { generate a call to a routine in the system unit }
  442. procedure g_call_system_proc(list: TAsmList; const procname: string);
  443. { Generate code to exit an unwind-protected region. The default implementation
  444. produces a simple jump to destination label. }
  445. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  446. end;
  447. var
  448. {# Main high level code generator class }
  449. hlcg : thlcgobj;
  450. procedure destroy_hlcodegen;
  451. implementation
  452. uses
  453. globals,options,systems,
  454. fmodule,export,
  455. verbose,defutil,paramgr,
  456. symbase,symsym,symtable,
  457. ncon,nld,pass_1,pass_2,
  458. cpuinfo,cgobj,tgobj,cutils,procinfo,
  459. ncgutil,ngenutil;
  460. procedure destroy_hlcodegen;
  461. begin
  462. hlcg.free;
  463. hlcg:=nil;
  464. destroy_codegen;
  465. end;
  466. { thlcgobj }
  467. constructor thlcgobj.create;
  468. begin
  469. end;
  470. procedure thlcgobj.init_register_allocators;
  471. begin
  472. cg.init_register_allocators;
  473. end;
  474. procedure thlcgobj.done_register_allocators;
  475. begin
  476. cg.done_register_allocators;
  477. end;
  478. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  479. begin
  480. cg.set_regalloc_live_range_direction(dir);
  481. end;
  482. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  483. begin
  484. result:=cg.getintregister(list,def_cgsize(size));
  485. end;
  486. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  487. begin
  488. result:=cg.getaddressregister(list);
  489. end;
  490. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  491. begin
  492. result:=cg.getfpuregister(list,def_cgsize(size));
  493. end;
  494. (*
  495. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  496. begin
  497. result:=cg.getmmregister(list,def_cgsize(size));
  498. end;
  499. *)
  500. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  501. begin
  502. result:=cg.getflagregister(list,def_cgsize(size));
  503. end;
  504. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  505. begin
  506. case def2regtyp(size) of
  507. R_INTREGISTER:
  508. result:=getintregister(list,size);
  509. R_ADDRESSREGISTER:
  510. result:=getaddressregister(list,size);
  511. R_FPUREGISTER:
  512. result:=getfpuregister(list,size);
  513. (*
  514. R_MMREGISTER:
  515. result:=getmmregister(list,size);
  516. *)
  517. else
  518. internalerror(2010122901);
  519. end;
  520. end;
  521. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  522. begin
  523. result:=cg.uses_registers(rt);
  524. end;
  525. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  526. begin
  527. cg.do_register_allocation(list,headertai);
  528. end;
  529. procedure thlcgobj.translate_register(var reg: tregister);
  530. begin
  531. cg.translate_register(reg);
  532. end;
  533. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  534. begin
  535. case def.typ of
  536. enumdef,
  537. orddef,
  538. recorddef,
  539. setdef:
  540. result:=R_INTREGISTER;
  541. stringdef,
  542. pointerdef,
  543. classrefdef,
  544. objectdef,
  545. procvardef,
  546. procdef,
  547. arraydef,
  548. formaldef:
  549. result:=R_ADDRESSREGISTER;
  550. floatdef:
  551. if use_vectorfpu(def) then
  552. result:=R_MMREGISTER
  553. else if cs_fp_emulation in current_settings.moduleswitches then
  554. result:=R_INTREGISTER
  555. else
  556. result:=R_FPUREGISTER;
  557. filedef,
  558. variantdef:
  559. internalerror(2010120507);
  560. else
  561. internalerror(2010120506);
  562. end;
  563. end;
  564. class function thlcgobj.tcgsize2orddef(size: tcgsize): torddef;
  565. begin
  566. case size of
  567. OS_8:
  568. result:=torddef(u8inttype);
  569. OS_S8:
  570. result:=torddef(s8inttype);
  571. OS_16:
  572. result:=torddef(u16inttype);
  573. OS_S16:
  574. result:=torddef(s16inttype);
  575. OS_32:
  576. result:=torddef(u32inttype);
  577. OS_S32:
  578. result:=torddef(s32inttype);
  579. OS_64:
  580. result:=torddef(u64inttype);
  581. OS_S64:
  582. result:=torddef(s64inttype);
  583. else
  584. internalerror(2012050401);
  585. end;
  586. end;
  587. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  588. begin
  589. cg.a_label(list,l);
  590. end;
  591. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  592. begin
  593. cg.a_reg_alloc(list,r);
  594. end;
  595. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  596. begin
  597. cg.a_reg_dealloc(list,r);
  598. end;
  599. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  600. begin
  601. cg.a_reg_sync(list,r);
  602. end;
  603. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  604. var
  605. ref: treference;
  606. begin
  607. cgpara.check_simple_location;
  608. paramanager.alloccgpara(list,cgpara);
  609. case cgpara.location^.loc of
  610. LOC_REGISTER,LOC_CREGISTER:
  611. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  612. LOC_REFERENCE,LOC_CREFERENCE:
  613. begin
  614. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  615. a_load_reg_ref(list,size,cgpara.def,r,ref);
  616. end;
  617. (*
  618. LOC_MMREGISTER,LOC_CMMREGISTER:
  619. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  620. *)
  621. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  622. begin
  623. tg.gethltemp(list,size,size.size,tt_normal,ref);
  624. a_load_reg_ref(list,size,size,r,ref);
  625. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  626. tg.ungettemp(list,ref);
  627. end
  628. else
  629. internalerror(2010120415);
  630. end;
  631. end;
  632. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  633. var
  634. ref : treference;
  635. begin
  636. cgpara.check_simple_location;
  637. paramanager.alloccgpara(list,cgpara);
  638. case cgpara.location^.loc of
  639. LOC_REGISTER,LOC_CREGISTER:
  640. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  641. LOC_REFERENCE,LOC_CREFERENCE:
  642. begin
  643. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  644. a_load_const_ref(list,cgpara.def,a,ref);
  645. end
  646. else
  647. internalerror(2010120416);
  648. end;
  649. end;
  650. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  651. var
  652. ref: treference;
  653. begin
  654. cgpara.check_simple_location;
  655. paramanager.alloccgpara(list,cgpara);
  656. case cgpara.location^.loc of
  657. LOC_REGISTER,LOC_CREGISTER:
  658. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  659. LOC_REFERENCE,LOC_CREFERENCE:
  660. begin
  661. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  662. a_load_ref_ref(list,size,cgpara.def,r,ref);
  663. end
  664. (*
  665. LOC_MMREGISTER,LOC_CMMREGISTER:
  666. begin
  667. case location^.size of
  668. OS_F32,
  669. OS_F64,
  670. OS_F128:
  671. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  672. OS_M8..OS_M128,
  673. OS_MS8..OS_MS128:
  674. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  675. else
  676. internalerror(2010120417);
  677. end;
  678. end
  679. *)
  680. else
  681. internalerror(2010120418);
  682. end;
  683. end;
  684. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  685. begin
  686. case l.loc of
  687. LOC_REGISTER,
  688. LOC_CREGISTER :
  689. a_load_reg_cgpara(list,size,l.register,cgpara);
  690. LOC_CONSTANT :
  691. a_load_const_cgpara(list,size,l.value,cgpara);
  692. LOC_CREFERENCE,
  693. LOC_REFERENCE :
  694. a_load_ref_cgpara(list,size,l.reference,cgpara);
  695. else
  696. internalerror(2010120419);
  697. end;
  698. end;
  699. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  700. var
  701. hr : tregister;
  702. begin
  703. cgpara.check_simple_location;
  704. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  705. begin
  706. paramanager.allocparaloc(list,cgpara.location);
  707. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  708. end
  709. else
  710. begin
  711. hr:=getaddressregister(list,tosize);
  712. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  713. a_load_reg_cgpara(list,tosize,hr,cgpara);
  714. end;
  715. end;
  716. procedure thlcgobj.a_call_ref(list: TAsmList; pd: tabstractprocdef; const ref: treference);
  717. var
  718. reg: tregister;
  719. size: tdef;
  720. begin
  721. { the loaded data is always a pointer to a procdef. A procvardef is
  722. implicitly a pointer already, but a procdef isn't -> create one }
  723. if pd.typ=procvardef then
  724. size:=pd
  725. else
  726. size:=getpointerdef(pd);
  727. reg:=getaddressregister(list,size);
  728. a_load_ref_reg(list,size,size,ref,reg);
  729. a_call_reg(list,pd,reg);
  730. end;
  731. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr);
  732. begin
  733. a_call_name(list,pd,s,false);
  734. end;
  735. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  736. begin
  737. a_call_name(list,pd,s,false);
  738. end;
  739. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  740. var
  741. tmpreg: tregister;
  742. begin
  743. tmpreg:=getintregister(list,tosize);
  744. a_load_const_reg(list,tosize,a,tmpreg);
  745. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  746. end;
  747. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  748. begin
  749. case loc.loc of
  750. LOC_REFERENCE,LOC_CREFERENCE:
  751. a_load_const_ref(list,tosize,a,loc.reference);
  752. LOC_REGISTER,LOC_CREGISTER:
  753. a_load_const_reg(list,tosize,a,loc.register);
  754. LOC_SUBSETREG,LOC_CSUBSETREG:
  755. a_load_const_subsetreg(list,tosize,a,loc.sreg);
  756. LOC_SUBSETREF,LOC_CSUBSETREF:
  757. a_load_const_subsetref(list,tosize,a,loc.sref);
  758. else
  759. internalerror(2010120401);
  760. end;
  761. end;
  762. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  763. begin
  764. a_load_reg_ref(list,fromsize,tosize,register,ref);
  765. end;
  766. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  767. begin
  768. case loc.loc of
  769. LOC_REFERENCE,LOC_CREFERENCE:
  770. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  771. LOC_REGISTER,LOC_CREGISTER:
  772. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  773. LOC_SUBSETREG,LOC_CSUBSETREG:
  774. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  775. LOC_SUBSETREF,LOC_CSUBSETREF:
  776. a_load_reg_subsetref(list,fromsize,tosize,reg,loc.sref);
  777. { we don't have enough type information to handle these here
  778. LOC_MMREGISTER,LOC_CMMREGISTER:
  779. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  780. }
  781. else
  782. internalerror(2010120402);
  783. end;
  784. end;
  785. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  786. begin
  787. a_load_ref_reg(list,fromsize,tosize,ref,register);
  788. end;
  789. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  790. var
  791. tmpreg: tregister;
  792. begin
  793. { verify if we have the same reference }
  794. if references_equal(sref,dref) then
  795. exit;
  796. tmpreg:=getintregister(list,tosize);
  797. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  798. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  799. end;
  800. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  801. begin
  802. case loc.loc of
  803. LOC_REFERENCE,LOC_CREFERENCE:
  804. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  805. LOC_REGISTER,LOC_CREGISTER:
  806. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  807. LOC_CONSTANT:
  808. a_load_const_reg(list,tosize,loc.value,reg);
  809. LOC_SUBSETREG,LOC_CSUBSETREG:
  810. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  811. LOC_SUBSETREF,LOC_CSUBSETREF:
  812. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  813. else
  814. internalerror(2010120201);
  815. end;
  816. end;
  817. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  818. begin
  819. case loc.loc of
  820. LOC_REFERENCE,LOC_CREFERENCE:
  821. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  822. LOC_REGISTER,LOC_CREGISTER:
  823. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  824. LOC_CONSTANT:
  825. a_load_const_ref(list,tosize,loc.value,ref);
  826. LOC_SUBSETREG,LOC_CSUBSETREG:
  827. a_load_subsetreg_ref(list,fromsize,tosize,loc.sreg,ref);
  828. LOC_SUBSETREF,LOC_CSUBSETREF:
  829. a_load_subsetref_ref(list,fromsize,tosize,loc.sref,ref);
  830. else
  831. internalerror(2010120403);
  832. end;
  833. end;
  834. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  835. begin
  836. case loc.loc of
  837. LOC_REFERENCE,LOC_CREFERENCE:
  838. a_load_ref_subsetreg(list,fromsize,tosubsetsize,loc.reference,sreg);
  839. LOC_REGISTER,LOC_CREGISTER:
  840. a_load_reg_subsetreg(list,fromsize,tosubsetsize,loc.register,sreg);
  841. LOC_CONSTANT:
  842. a_load_const_subsetreg(list,tosubsetsize,loc.value,sreg);
  843. LOC_SUBSETREG,LOC_CSUBSETREG:
  844. a_load_subsetreg_subsetreg(list,fromsize,tosubsetsize,loc.sreg,sreg);
  845. LOC_SUBSETREF,LOC_CSUBSETREF:
  846. a_load_subsetref_subsetreg(list,fromsize,tosubsetsize,loc.sref,sreg);
  847. else
  848. internalerror(2010120404);
  849. end;
  850. end;
  851. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  852. begin
  853. case loc.loc of
  854. LOC_REFERENCE,LOC_CREFERENCE:
  855. a_load_ref_subsetref(list,fromsize,tosubsetsize,loc.reference,sref);
  856. LOC_REGISTER,LOC_CREGISTER:
  857. a_load_reg_subsetref(list,fromsize,tosubsetsize,loc.register,sref);
  858. LOC_CONSTANT:
  859. a_load_const_subsetref(list,tosubsetsize,loc.value,sref);
  860. LOC_SUBSETREG,LOC_CSUBSETREG:
  861. a_load_subsetreg_subsetref(list,fromsize,tosubsetsize,loc.sreg,sref);
  862. LOC_SUBSETREF,LOC_CSUBSETREF:
  863. a_load_subsetref_subsetref(list,fromsize,tosubsetsize,loc.sref,sref);
  864. else
  865. internalerror(2010120405);
  866. end;
  867. end;
  868. procedure thlcgobj.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  869. var
  870. subsetregdef: torddef;
  871. bitmask: aword;
  872. tmpreg,
  873. subsetsizereg: tregister;
  874. stopbit: byte;
  875. begin
  876. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  877. tmpreg:=getintregister(list,subsetregdef);
  878. if is_signed(subsetsize) then
  879. begin
  880. { sign extend in case the value has a bitsize mod 8 <> 0 }
  881. { both instructions will be optimized away if not }
  882. a_op_const_reg_reg(list,OP_SHL,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  883. a_op_const_reg(list,OP_SAR,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  884. end
  885. else
  886. begin
  887. a_op_const_reg_reg(list,OP_SHR,subsetregdef,sreg.startbit,sreg.subsetreg,tmpreg);
  888. stopbit:=sreg.startbit+sreg.bitlen;
  889. // on x86(64), 1 shl 32(64) = 1 instead of 0
  890. // use aword to prevent overflow with 1 shl 31
  891. if (stopbit-sreg.startbit<>AIntBits) then
  892. bitmask:=(aword(1) shl (stopbit-sreg.startbit))-1
  893. else
  894. bitmask:=high(aword);
  895. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),tmpreg);
  896. end;
  897. subsetsizereg:=getintregister(list,subsetsize);
  898. a_load_reg_reg(list,subsetregdef,subsetsize,tmpreg,subsetsizereg);
  899. a_load_reg_reg(list,subsetsize,tosize,subsetsizereg,destreg);
  900. end;
  901. procedure thlcgobj.a_load_reg_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister);
  902. begin
  903. a_load_regconst_subsetreg_intern(list,fromsize,tosubsetsize,fromreg,sreg,SL_REG);
  904. end;
  905. procedure thlcgobj.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  906. var
  907. fromsubsetregdef,
  908. tosubsetregdef: torddef;
  909. tmpreg: tregister;
  910. bitmask: aword;
  911. stopbit: byte;
  912. begin
  913. if (fromsreg.bitlen>=tosreg.bitlen) then
  914. begin
  915. fromsubsetregdef:=tcgsize2orddef(fromsreg.subsetregsize);
  916. tosubsetregdef:=tcgsize2orddef(tosreg.subsetregsize);
  917. tmpreg:=getintregister(list,tosubsetregdef);
  918. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,fromsreg.subsetreg,tmpreg);
  919. if (fromsreg.startbit<=tosreg.startbit) then
  920. a_op_const_reg(list,OP_SHL,tosubsetregdef,tosreg.startbit-fromsreg.startbit,tmpreg)
  921. else
  922. a_op_const_reg(list,OP_SHR,tosubsetregdef,fromsreg.startbit-tosreg.startbit,tmpreg);
  923. stopbit:=tosreg.startbit + tosreg.bitlen;
  924. // on x86(64), 1 shl 32(64) = 1 instead of 0
  925. if (stopbit<>AIntBits) then
  926. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  927. else
  928. bitmask:=(aword(1) shl tosreg.startbit) - 1;
  929. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(bitmask),tosreg.subsetreg);
  930. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(not(bitmask)),tmpreg);
  931. a_op_reg_reg(list,OP_OR,tosubsetregdef,tmpreg,tosreg.subsetreg);
  932. end
  933. else
  934. begin
  935. tmpreg:=getintregister(list,tosubsetsize);
  936. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  937. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  938. end;
  939. end;
  940. procedure thlcgobj.a_load_subsetreg_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference);
  941. var
  942. tmpreg: tregister;
  943. begin
  944. tmpreg:=getintregister(list,tosize);
  945. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,tmpreg);
  946. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  947. end;
  948. procedure thlcgobj.a_load_ref_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister);
  949. var
  950. tmpreg: tregister;
  951. begin
  952. tmpreg := getintregister(list,tosubsetsize);
  953. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  954. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,sreg);
  955. end;
  956. procedure thlcgobj.a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister);
  957. var
  958. subsetregdef: torddef;
  959. bitmask: aword;
  960. stopbit: byte;
  961. begin
  962. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  963. stopbit:=sreg.startbit+sreg.bitlen;
  964. // on x86(64), 1 shl 32(64) = 1 instead of 0
  965. if (stopbit<>AIntBits) then
  966. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  967. else
  968. bitmask:=(aword(1) shl sreg.startbit)-1;
  969. if (((aword(a) shl sreg.startbit) and not bitmask)<>not bitmask) then
  970. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  971. a_op_const_reg(list,OP_OR,subsetregdef,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  972. end;
  973. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  974. begin
  975. case loc.loc of
  976. LOC_REFERENCE,LOC_CREFERENCE:
  977. a_load_subsetreg_ref(list,fromsubsetsize,tosize,sreg,loc.reference);
  978. LOC_REGISTER,LOC_CREGISTER:
  979. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,loc.register);
  980. LOC_SUBSETREG,LOC_CSUBSETREG:
  981. a_load_subsetreg_subsetreg(list,fromsubsetsize,tosize,sreg,loc.sreg);
  982. LOC_SUBSETREF,LOC_CSUBSETREF:
  983. a_load_subsetreg_subsetref(list,fromsubsetsize,tosize,sreg,loc.sref);
  984. else
  985. internalerror(2010120406);
  986. end;
  987. end;
  988. procedure thlcgobj.a_load_subsetref_reg(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister);
  989. var
  990. tmpref: treference;
  991. valuereg,extra_value_reg: tregister;
  992. tosreg: tsubsetregister;
  993. loadsize: torddef;
  994. loadbitsize: byte;
  995. extra_load: boolean;
  996. begin
  997. get_subsetref_load_info(sref,loadsize,extra_load);
  998. loadbitsize:=loadsize.size*8;
  999. { load the (first part) of the bit sequence }
  1000. valuereg:=getintregister(list,osuinttype);
  1001. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1002. if not extra_load then
  1003. begin
  1004. { everything is guaranteed to be in a single register of loadsize }
  1005. if (sref.bitindexreg=NR_NO) then
  1006. begin
  1007. { use subsetreg routine, it may have been overridden with an optimized version }
  1008. tosreg.subsetreg:=valuereg;
  1009. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1010. { subsetregs always count bits from right to left }
  1011. if (target_info.endian=endian_big) then
  1012. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1013. else
  1014. tosreg.startbit:=sref.startbit;
  1015. tosreg.bitlen:=sref.bitlen;
  1016. a_load_subsetreg_reg(list,fromsubsetsize,tosize,tosreg,destreg);
  1017. exit;
  1018. end
  1019. else
  1020. begin
  1021. if (sref.startbit<>0) then
  1022. internalerror(2006081510);
  1023. if (target_info.endian=endian_big) then
  1024. begin
  1025. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1026. if is_signed(fromsubsetsize) then
  1027. begin
  1028. { sign extend to entire register }
  1029. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1030. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1031. end
  1032. else
  1033. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1034. end
  1035. else
  1036. begin
  1037. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1038. if is_signed(fromsubsetsize) then
  1039. begin
  1040. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1041. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1042. end
  1043. end;
  1044. { mask other bits/sign extend }
  1045. if not is_signed(fromsubsetsize) then
  1046. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1047. end
  1048. end
  1049. else
  1050. begin
  1051. { load next value as well }
  1052. extra_value_reg:=getintregister(list,osuinttype);
  1053. if (sref.bitindexreg=NR_NO) then
  1054. begin
  1055. tmpref:=sref.ref;
  1056. inc(tmpref.offset,loadbitsize div 8);
  1057. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1058. { can be overridden to optimize }
  1059. a_load_subsetref_regs_noindex(list,fromsubsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1060. end
  1061. else
  1062. begin
  1063. if (sref.startbit<>0) then
  1064. internalerror(2006080610);
  1065. a_load_subsetref_regs_index(list,fromsubsetsize,loadbitsize,sref,valuereg);
  1066. end;
  1067. end;
  1068. { store in destination }
  1069. {$ifndef cpuhighleveltarget}
  1070. { avoid unnecessary sign extension and zeroing }
  1071. valuereg:=cg.makeregsize(list,valuereg,OS_INT);
  1072. destreg:=cg.makeregsize(list,destreg,OS_INT);
  1073. cg.a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1074. destreg:=cg.makeregsize(list,destreg,def_cgsize(tosize));
  1075. {$else}
  1076. { can't juggle with register sizes, they are actually typed entities
  1077. here }
  1078. a_load_reg_reg(list,osuinttype,tosize,valuereg,destreg);
  1079. {$endif}
  1080. end;
  1081. procedure thlcgobj.a_load_reg_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference);
  1082. begin
  1083. a_load_regconst_subsetref_intern(list,fromsize,tosubsetsize,fromreg,sref,SL_REG);
  1084. end;
  1085. procedure thlcgobj.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref, tosref: tsubsetreference);
  1086. var
  1087. tmpreg: tregister;
  1088. begin
  1089. tmpreg:=getintregister(list,tosubsetsize);
  1090. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1091. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1092. end;
  1093. procedure thlcgobj.a_load_subsetref_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference);
  1094. var
  1095. tmpreg: tregister;
  1096. begin
  1097. tmpreg:=getintregister(list,tosize);
  1098. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,tmpreg);
  1099. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1100. end;
  1101. procedure thlcgobj.a_load_ref_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference);
  1102. var
  1103. tmpreg: tregister;
  1104. begin
  1105. tmpreg := getintregister(list,tosubsetsize);
  1106. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1107. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,sref);
  1108. end;
  1109. procedure thlcgobj.a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  1110. var
  1111. tmpreg: tregister;
  1112. slopt: tsubsetloadopt;
  1113. begin
  1114. { perform masking of the source value in advance }
  1115. slopt:=SL_REGNOSRCMASK;
  1116. if (sref.bitlen<>AIntBits) then
  1117. a:=tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1118. if (
  1119. { broken x86 "x shl regbitsize = x" }
  1120. ((sref.bitlen<>AIntBits) and
  1121. ((aword(a) and ((aword(1) shl sref.bitlen)-1))=(aword(1) shl sref.bitlen)-1)) or
  1122. ((sref.bitlen=AIntBits) and
  1123. (a=-1))
  1124. ) then
  1125. slopt:=SL_SETMAX
  1126. else if (a=0) then
  1127. slopt:=SL_SETZERO;
  1128. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1129. begin
  1130. tmpreg:=getintregister(list,tosubsetsize);
  1131. a_load_const_reg(list,tosubsetsize,a,tmpreg);
  1132. end
  1133. else
  1134. tmpreg:=NR_NO;
  1135. a_load_regconst_subsetref_intern(list,tosubsetsize,tosubsetsize,tmpreg,sref,slopt);
  1136. end;
  1137. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  1138. begin
  1139. case loc.loc of
  1140. LOC_REFERENCE,LOC_CREFERENCE:
  1141. a_load_subsetref_ref(list,fromsubsetsize,tosize,sref,loc.reference);
  1142. LOC_REGISTER,LOC_CREGISTER:
  1143. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,loc.register);
  1144. LOC_SUBSETREG,LOC_CSUBSETREG:
  1145. a_load_subsetref_subsetreg(list,fromsubsetsize,tosize,sref,loc.sreg);
  1146. LOC_SUBSETREF,LOC_CSUBSETREF:
  1147. a_load_subsetref_subsetref(list,fromsubsetsize,tosize,sref,loc.sref);
  1148. else
  1149. internalerror(2010120407);
  1150. end;
  1151. end;
  1152. procedure thlcgobj.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1153. var
  1154. tmpreg: tregister;
  1155. begin
  1156. tmpreg:=getintregister(list,tosubsetsize);
  1157. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1158. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1159. end;
  1160. procedure thlcgobj.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1161. var
  1162. tmpreg: tregister;
  1163. begin
  1164. tmpreg := getintregister(list,tosubsetsize);
  1165. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1166. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1167. end;
  1168. procedure thlcgobj.a_bit_test_reg_reg_reg(list: TAsmList; bitnumbersize, valuesize, destsize: tdef; bitnumber, value, destreg: tregister);
  1169. var
  1170. tmpvalue: tregister;
  1171. begin
  1172. tmpvalue:=getintregister(list,valuesize);
  1173. if (target_info.endian=endian_little) then
  1174. begin
  1175. { rotate value register "bitnumber" bits to the right }
  1176. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1177. { extract the bit we want }
  1178. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1179. end
  1180. else
  1181. begin
  1182. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1183. { bit in uppermost position, then move it to the lowest position }
  1184. { "and" is not necessary since combination of shl/shr will clear }
  1185. { all other bits }
  1186. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1187. a_op_const_reg(list,OP_SHR,valuesize,valuesize.size*8-1,tmpvalue);
  1188. end;
  1189. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1190. end;
  1191. procedure thlcgobj.a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister);
  1192. begin
  1193. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_const_ref_sref(bitnumber,fromsize,ref),destreg);
  1194. end;
  1195. procedure thlcgobj.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister);
  1196. begin
  1197. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1198. end;
  1199. procedure thlcgobj.a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister);
  1200. var
  1201. tmpsreg: tsubsetregister;
  1202. begin
  1203. { the first parameter is used to calculate the bit offset in }
  1204. { case of big endian, and therefore must be the size of the }
  1205. { set and not of the whole subsetreg }
  1206. tmpsreg:=get_bit_const_reg_sreg(fromsubsetsize,bitnumber,setreg.subsetreg);
  1207. { now fix the size of the subsetreg }
  1208. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1209. { correct offset of the set in the subsetreg }
  1210. inc(tmpsreg.startbit,setreg.startbit);
  1211. a_load_subsetreg_reg(list,fromsubsetsize,destsize,tmpsreg,destreg);
  1212. end;
  1213. procedure thlcgobj.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister);
  1214. begin
  1215. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_reg_ref_sref(list,bitnumbersize,refsize,bitnumber,ref),destreg);
  1216. end;
  1217. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1218. var
  1219. tmpreg: tregister;
  1220. begin
  1221. case loc.loc of
  1222. LOC_REFERENCE,LOC_CREFERENCE:
  1223. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  1224. LOC_REGISTER,LOC_CREGISTER,
  1225. LOC_SUBSETREG,LOC_CSUBSETREG,
  1226. LOC_CONSTANT:
  1227. begin
  1228. case loc.loc of
  1229. LOC_REGISTER,LOC_CREGISTER:
  1230. tmpreg:=loc.register;
  1231. LOC_SUBSETREG,LOC_CSUBSETREG:
  1232. begin
  1233. tmpreg:=getintregister(list,locsize);
  1234. a_load_subsetreg_reg(list,locsize,locsize,loc.sreg,tmpreg);
  1235. end;
  1236. LOC_CONSTANT:
  1237. begin
  1238. tmpreg:=getintregister(list,locsize);
  1239. a_load_const_reg(list,locsize,loc.value,tmpreg);
  1240. end;
  1241. end;
  1242. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  1243. end;
  1244. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1245. else
  1246. internalerror(2010120411);
  1247. end;
  1248. end;
  1249. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  1250. begin
  1251. case loc.loc of
  1252. LOC_REFERENCE,LOC_CREFERENCE:
  1253. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  1254. LOC_REGISTER,LOC_CREGISTER:
  1255. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  1256. LOC_SUBSETREG,LOC_CSUBSETREG:
  1257. a_bit_test_const_subsetreg_reg(list,locsize,destsize,bitnumber,loc.sreg,destreg);
  1258. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1259. else
  1260. internalerror(2010120410);
  1261. end;
  1262. end;
  1263. procedure thlcgobj.a_bit_set_reg_reg(list: TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber, dest: tregister);
  1264. var
  1265. tmpvalue: tregister;
  1266. begin
  1267. tmpvalue:=getintregister(list,destsize);
  1268. if (target_info.endian=endian_little) then
  1269. begin
  1270. a_load_const_reg(list,destsize,1,tmpvalue);
  1271. { rotate bit "bitnumber" bits to the left }
  1272. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1273. end
  1274. else
  1275. begin
  1276. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  1277. { shr bitnumber" results in correct mask }
  1278. a_load_const_reg(list,destsize,1 shl (destsize.size*8-1),tmpvalue);
  1279. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  1280. end;
  1281. { set/clear the bit we want }
  1282. if doset then
  1283. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  1284. else
  1285. begin
  1286. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  1287. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  1288. end;
  1289. end;
  1290. procedure thlcgobj.a_bit_set_const_ref(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const ref: treference);
  1291. begin
  1292. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_const_ref_sref(bitnumber,destsize,ref));
  1293. end;
  1294. procedure thlcgobj.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister);
  1295. begin
  1296. a_load_const_subsetreg(list,u8inttype,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  1297. end;
  1298. procedure thlcgobj.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister);
  1299. var
  1300. tmpsreg: tsubsetregister;
  1301. begin
  1302. { the first parameter is used to calculate the bit offset in }
  1303. { case of big endian, and therefore must be the size of the }
  1304. { set and not of the whole subsetreg }
  1305. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  1306. { now fix the size of the subsetreg }
  1307. tmpsreg.subsetregsize:=destreg.subsetregsize;
  1308. { correct offset of the set in the subsetreg }
  1309. inc(tmpsreg.startbit,destreg.startbit);
  1310. a_load_const_subsetreg(list,u8inttype,ord(doset),tmpsreg);
  1311. end;
  1312. procedure thlcgobj.a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference);
  1313. begin
  1314. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_reg_ref_sref(list,fromsize,tosize,bitnumber,ref));
  1315. end;
  1316. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  1317. var
  1318. tmpreg: tregister;
  1319. begin
  1320. case loc.loc of
  1321. LOC_REFERENCE:
  1322. a_bit_set_reg_ref(list,doset,regsize,tosize,bitnumber,loc.reference);
  1323. LOC_CREGISTER:
  1324. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,loc.register);
  1325. { e.g. a 2-byte set in a record regvar }
  1326. LOC_CSUBSETREG:
  1327. begin
  1328. { hard to do in-place in a generic way, so operate on a copy }
  1329. tmpreg:=getintregister(list,tosize);
  1330. a_load_subsetreg_reg(list,tosize,tosize,loc.sreg,tmpreg);
  1331. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,tmpreg);
  1332. a_load_reg_subsetreg(list,tosize,tosize,tmpreg,loc.sreg);
  1333. end;
  1334. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1335. else
  1336. internalerror(2010120408)
  1337. end;
  1338. end;
  1339. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  1340. begin
  1341. case loc.loc of
  1342. LOC_REFERENCE:
  1343. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  1344. LOC_CREGISTER:
  1345. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  1346. LOC_CSUBSETREG:
  1347. a_bit_set_const_subsetreg(list,doset,tosize,bitnumber,loc.sreg);
  1348. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1349. else
  1350. internalerror(2010120409)
  1351. end;
  1352. end;
  1353. (*
  1354. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1355. in memory. They are like a regular reference, but contain an extra bit
  1356. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1357. and a bit length (always constant).
  1358. Bit packed values are stored differently in memory depending on whether we
  1359. are on a big or a little endian system (compatible with at least GPC). The
  1360. size of the basic working unit is always the smallest power-of-2 byte size
  1361. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1362. bytes, 17..32 bits -> 4 bytes etc).
  1363. On a big endian, 5-bit: values are stored like this:
  1364. 11111222 22333334 44445555 56666677 77788888
  1365. The leftmost bit of each 5-bit value corresponds to the most significant
  1366. bit.
  1367. On little endian, it goes like this:
  1368. 22211111 43333322 55554444 77666665 88888777
  1369. In this case, per byte the left-most bit is more significant than those on
  1370. the right, but the bits in the next byte are all more significant than
  1371. those in the previous byte (e.g., the 222 in the first byte are the low
  1372. three bits of that value, while the 22 in the second byte are the upper
  1373. two bits.
  1374. Big endian, 9 bit values:
  1375. 11111111 12222222 22333333 33344444 ...
  1376. Little endian, 9 bit values:
  1377. 11111111 22222221 33333322 44444333 ...
  1378. This is memory representation and the 16 bit values are byteswapped.
  1379. Similarly as in the previous case, the 2222222 string contains the lower
  1380. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1381. registers (two 16 bit registers in the current implementation, although a
  1382. single 32 bit register would be possible too, in particular if 32 bit
  1383. alignment can be guaranteed), this becomes:
  1384. 22222221 11111111 44444333 33333322 ...
  1385. (l)ow u l l u l u
  1386. The startbit/bitindex in a subsetreference always refers to
  1387. a) on big endian: the most significant bit of the value
  1388. (bits counted from left to right, both memory an registers)
  1389. b) on little endian: the least significant bit when the value
  1390. is loaded in a register (bit counted from right to left)
  1391. Although a) results in more complex code for big endian systems, it's
  1392. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1393. Apple's universal interfaces which depend on these layout differences).
  1394. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1395. make sure the appropriate alignment is guaranteed, at least in case of
  1396. {$defined cpurequiresproperalignment}.
  1397. *)
  1398. procedure thlcgobj.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  1399. var
  1400. intloadsize: tcgint;
  1401. begin
  1402. intloadsize:=packedbitsloadsize(sref.bitlen);
  1403. if (intloadsize=0) then
  1404. internalerror(2006081310);
  1405. if (intloadsize>sizeof(aint)) then
  1406. intloadsize:=sizeof(aint);
  1407. loadsize:=tcgsize2orddef(int_cgsize(intloadsize));
  1408. if (sref.bitlen>sizeof(aint)*8) then
  1409. internalerror(2006081312);
  1410. extra_load:=
  1411. (sref.bitlen<>1) and
  1412. ((sref.bitindexreg<>NR_NO) or
  1413. (byte(sref.startbit+sref.bitlen)>byte(intloadsize*8)));
  1414. end;
  1415. procedure thlcgobj.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1416. var
  1417. restbits: byte;
  1418. begin
  1419. if (target_info.endian=endian_big) then
  1420. begin
  1421. { valuereg contains the upper bits, extra_value_reg the lower }
  1422. restbits:=(sref.bitlen-(loadbitsize-sref.startbit));
  1423. if is_signed(subsetsize) then
  1424. begin
  1425. { sign extend }
  1426. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize+sref.startbit,valuereg);
  1427. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1428. end
  1429. else
  1430. begin
  1431. a_op_const_reg(list,OP_SHL,osuinttype,restbits,valuereg);
  1432. { mask other bits }
  1433. if (sref.bitlen<>AIntBits) then
  1434. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1435. end;
  1436. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-restbits,extra_value_reg)
  1437. end
  1438. else
  1439. begin
  1440. { valuereg contains the lower bits, extra_value_reg the upper }
  1441. a_op_const_reg(list,OP_SHR,osuinttype,sref.startbit,valuereg);
  1442. if is_signed(subsetsize) then
  1443. begin
  1444. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1445. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,extra_value_reg);
  1446. end
  1447. else
  1448. begin
  1449. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.startbit,extra_value_reg);
  1450. { mask other bits }
  1451. if (sref.bitlen <> AIntBits) then
  1452. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1453. end;
  1454. end;
  1455. { merge }
  1456. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1457. end;
  1458. procedure thlcgobj.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1459. var
  1460. hl: tasmlabel;
  1461. tmpref: treference;
  1462. extra_value_reg,
  1463. tmpreg: tregister;
  1464. begin
  1465. tmpreg:=getintregister(list,osuinttype);
  1466. tmpref:=sref.ref;
  1467. inc(tmpref.offset,loadbitsize div 8);
  1468. extra_value_reg:=getintregister(list,osuinttype);
  1469. if (target_info.endian=endian_big) then
  1470. begin
  1471. { since this is a dynamic index, it's possible that the value }
  1472. { is entirely in valuereg. }
  1473. { get the data in valuereg in the right place }
  1474. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1475. if is_signed(subsetsize) then
  1476. begin
  1477. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1478. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg)
  1479. end
  1480. else
  1481. begin
  1482. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1483. if (loadbitsize<>AIntBits) then
  1484. { mask left over bits }
  1485. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1486. end;
  1487. tmpreg := getintregister(list,osuinttype);
  1488. { ensure we don't load anything past the end of the array }
  1489. current_asmdata.getjumplabel(hl);
  1490. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1491. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1492. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1493. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1494. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1495. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1496. { load next "loadbitsize" bits of the array }
  1497. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1498. a_op_reg_reg(list,OP_SHR,osuinttype,tmpreg,extra_value_reg);
  1499. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1500. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1501. { => extra_value_reg is now 0 }
  1502. { merge }
  1503. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1504. { no need to mask, necessary masking happened earlier on }
  1505. a_label(list,hl);
  1506. end
  1507. else
  1508. begin
  1509. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1510. { ensure we don't load anything past the end of the array }
  1511. current_asmdata.getjumplabel(hl);
  1512. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1513. { Y-x = -(Y-x) }
  1514. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1515. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1516. { load next "loadbitsize" bits of the array }
  1517. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1518. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1519. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1520. { merge }
  1521. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1522. a_label(list,hl);
  1523. { sign extend or mask other bits }
  1524. if is_signed(subsetsize) then
  1525. begin
  1526. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1527. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1528. end
  1529. else
  1530. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1531. end;
  1532. end;
  1533. procedure thlcgobj.a_load_regconst_subsetref_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1534. var
  1535. hl: tasmlabel;
  1536. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1537. tosreg, fromsreg: tsubsetregister;
  1538. tmpref: treference;
  1539. bitmask: aword;
  1540. loadsize: torddef;
  1541. loadbitsize: byte;
  1542. extra_load: boolean;
  1543. begin
  1544. { the register must be able to contain the requested value }
  1545. if (fromsize.size*8<sref.bitlen) then
  1546. internalerror(2006081613);
  1547. get_subsetref_load_info(sref,loadsize,extra_load);
  1548. loadbitsize:=loadsize.size*8;
  1549. { load the (first part) of the bit sequence }
  1550. valuereg:=getintregister(list,osuinttype);
  1551. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1552. { constant offset of bit sequence? }
  1553. if not extra_load then
  1554. begin
  1555. if (sref.bitindexreg=NR_NO) then
  1556. begin
  1557. { use subsetreg routine, it may have been overridden with an optimized version }
  1558. tosreg.subsetreg:=valuereg;
  1559. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1560. { subsetregs always count bits from right to left }
  1561. if (target_info.endian=endian_big) then
  1562. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1563. else
  1564. tosreg.startbit:=sref.startbit;
  1565. tosreg.bitlen:=sref.bitlen;
  1566. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1567. end
  1568. else
  1569. begin
  1570. if (sref.startbit<>0) then
  1571. internalerror(2006081710);
  1572. { should be handled by normal code and will give wrong result }
  1573. { on x86 for the '1 shl bitlen' below }
  1574. if (sref.bitlen=AIntBits) then
  1575. internalerror(2006081711);
  1576. { zero the bits we have to insert }
  1577. if (slopt<>SL_SETMAX) then
  1578. begin
  1579. maskreg:=getintregister(list,osuinttype);
  1580. if (target_info.endian = endian_big) then
  1581. begin
  1582. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1583. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1584. end
  1585. else
  1586. begin
  1587. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1588. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1589. end;
  1590. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1591. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1592. end;
  1593. { insert the value }
  1594. if (slopt<>SL_SETZERO) then
  1595. begin
  1596. tmpreg:=getintregister(list,osuinttype);
  1597. if (slopt<>SL_SETMAX) then
  1598. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1599. else if (sref.bitlen<>AIntBits) then
  1600. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1601. else
  1602. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1603. if (target_info.endian=endian_big) then
  1604. begin
  1605. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1606. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1607. begin
  1608. if (loadbitsize<>AIntBits) then
  1609. bitmask:=(((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1610. else
  1611. bitmask:=(high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1612. a_op_const_reg(list,OP_AND,osuinttype,bitmask,tmpreg);
  1613. end;
  1614. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1615. end
  1616. else
  1617. begin
  1618. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1619. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1620. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1621. end;
  1622. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1623. end;
  1624. end;
  1625. { store back to memory }
  1626. tmpreg:=getintregister(list,loadsize);
  1627. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1628. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1629. exit;
  1630. end
  1631. else
  1632. begin
  1633. { load next value }
  1634. extra_value_reg:=getintregister(list,osuinttype);
  1635. tmpref:=sref.ref;
  1636. inc(tmpref.offset,loadbitsize div 8);
  1637. { should maybe be taken out too, can be done more efficiently }
  1638. { on e.g. i386 with shld/shrd }
  1639. if (sref.bitindexreg = NR_NO) then
  1640. begin
  1641. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1642. fromsreg.subsetreg:=fromreg;
  1643. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1644. tosreg.subsetreg:=valuereg;
  1645. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1646. { transfer first part }
  1647. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1648. tosreg.bitlen:=fromsreg.bitlen;
  1649. if (target_info.endian=endian_big) then
  1650. begin
  1651. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1652. { upper bits of the value ... }
  1653. fromsreg.startbit:=sref.bitlen-(loadbitsize-sref.startbit);
  1654. { ... to bit 0 }
  1655. tosreg.startbit:=0
  1656. end
  1657. else
  1658. begin
  1659. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1660. { lower bits of the value ... }
  1661. fromsreg.startbit:=0;
  1662. { ... to startbit }
  1663. tosreg.startbit:=sref.startbit;
  1664. end;
  1665. case slopt of
  1666. SL_SETZERO,
  1667. SL_SETMAX:
  1668. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1669. else
  1670. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1671. end;
  1672. {$ifndef cpuhighleveltarget}
  1673. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1674. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1675. {$else}
  1676. tmpreg:=getintregister(list,loadsize);
  1677. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1678. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1679. {$endif}
  1680. { transfer second part }
  1681. if (target_info.endian = endian_big) then
  1682. begin
  1683. { extra_value_reg must contain the lower bits of the value at bits }
  1684. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1685. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1686. { - bitlen - startbit }
  1687. fromsreg.startbit:=0;
  1688. tosreg.startbit:=2*loadbitsize-sref.bitlen-sref.startbit
  1689. end
  1690. else
  1691. begin
  1692. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1693. fromsreg.startbit:=fromsreg.bitlen;
  1694. tosreg.startbit:=0;
  1695. end;
  1696. tosreg.subsetreg:=extra_value_reg;
  1697. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1698. tosreg.bitlen:=fromsreg.bitlen;
  1699. case slopt of
  1700. SL_SETZERO,
  1701. SL_SETMAX:
  1702. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1703. else
  1704. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1705. end;
  1706. tmpreg:=getintregister(list,loadsize);
  1707. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1708. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1709. exit;
  1710. end
  1711. else
  1712. begin
  1713. if (sref.startbit <> 0) then
  1714. internalerror(2006081812);
  1715. { should be handled by normal code and will give wrong result }
  1716. { on x86 for the '1 shl bitlen' below }
  1717. if (sref.bitlen = AIntBits) then
  1718. internalerror(2006081713);
  1719. { generate mask to zero the bits we have to insert }
  1720. if (slopt <> SL_SETMAX) then
  1721. begin
  1722. maskreg := getintregister(list,osuinttype);
  1723. if (target_info.endian = endian_big) then
  1724. begin
  1725. a_load_const_reg(list,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  1726. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1727. end
  1728. else
  1729. begin
  1730. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1731. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1732. end;
  1733. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1734. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1735. end;
  1736. { insert the value }
  1737. if (slopt <> SL_SETZERO) then
  1738. begin
  1739. tmpreg := getintregister(list,osuinttype);
  1740. if (slopt <> SL_SETMAX) then
  1741. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1742. else if (sref.bitlen <> AIntBits) then
  1743. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1744. else
  1745. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1746. if (target_info.endian = endian_big) then
  1747. begin
  1748. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1749. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1750. { mask left over bits }
  1751. a_op_const_reg(list,OP_AND,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  1752. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1753. end
  1754. else
  1755. begin
  1756. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1757. { mask left over bits }
  1758. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1759. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1760. end;
  1761. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1762. end;
  1763. tmpreg:=getintregister(list,loadsize);
  1764. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1765. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1766. { make sure we do not read/write past the end of the array }
  1767. current_asmdata.getjumplabel(hl);
  1768. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1769. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1770. tmpindexreg:=getintregister(list,osuinttype);
  1771. { load current array value }
  1772. if (slopt<>SL_SETZERO) then
  1773. begin
  1774. tmpreg:=getintregister(list,osuinttype);
  1775. if (slopt<>SL_SETMAX) then
  1776. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1777. else if (sref.bitlen<>AIntBits) then
  1778. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1779. else
  1780. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1781. end;
  1782. { generate mask to zero the bits we have to insert }
  1783. if (slopt<>SL_SETMAX) then
  1784. begin
  1785. maskreg:=getintregister(list,osuinttype);
  1786. if (target_info.endian=endian_big) then
  1787. begin
  1788. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1789. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1790. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1791. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,maskreg);
  1792. end
  1793. else
  1794. begin
  1795. { Y-x = -(x-Y) }
  1796. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  1797. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1798. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1799. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  1800. end;
  1801. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1802. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  1803. end;
  1804. if (slopt<>SL_SETZERO) then
  1805. begin
  1806. if (target_info.endian=endian_big) then
  1807. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,tmpreg)
  1808. else
  1809. begin
  1810. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1811. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1812. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  1813. end;
  1814. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  1815. end;
  1816. {$ifndef cpuhighleveltarget}
  1817. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  1818. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1819. {$else}
  1820. tmpreg:=getintregister(list,loadsize);
  1821. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1822. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1823. {$endif}
  1824. a_label(list,hl);
  1825. end;
  1826. end;
  1827. end;
  1828. procedure thlcgobj.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  1829. var
  1830. bitmask: aword;
  1831. tmpreg: tregister;
  1832. subsetregdef: torddef;
  1833. stopbit: byte;
  1834. begin
  1835. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  1836. stopbit:=sreg.startbit+sreg.bitlen;
  1837. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1838. if (stopbit<>AIntBits) then
  1839. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1840. else
  1841. bitmask:=not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  1842. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1843. begin
  1844. tmpreg:=getintregister(list,subsetregdef);
  1845. a_load_reg_reg(list,fromsize,subsetregdef,fromreg,tmpreg);
  1846. a_op_const_reg(list,OP_SHL,subsetregdef,sreg.startbit,tmpreg);
  1847. if (slopt<>SL_REGNOSRCMASK) then
  1848. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(not(bitmask)),tmpreg);
  1849. end;
  1850. if (slopt<>SL_SETMAX) then
  1851. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1852. case slopt of
  1853. SL_SETZERO : ;
  1854. SL_SETMAX :
  1855. if (sreg.bitlen<>AIntBits) then
  1856. a_op_const_reg(list,OP_OR,subsetregdef,
  1857. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  1858. sreg.subsetreg)
  1859. else
  1860. a_load_const_reg(list,subsetregdef,-1,sreg.subsetreg);
  1861. else
  1862. a_op_reg_reg(list,OP_OR,subsetregdef,tmpreg,sreg.subsetreg);
  1863. end;
  1864. end;
  1865. function thlcgobj.get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  1866. begin
  1867. result.ref:=ref;
  1868. inc(result.ref.offset,bitnumber div 8);
  1869. result.bitindexreg:=NR_NO;
  1870. result.startbit:=bitnumber mod 8;
  1871. result.bitlen:=1;
  1872. end;
  1873. function thlcgobj.get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  1874. begin
  1875. result.subsetreg:=setreg;
  1876. result.subsetregsize:=def_cgsize(setregsize);
  1877. { subsetregs always count from the least significant to the most significant bit }
  1878. if (target_info.endian=endian_big) then
  1879. result.startbit:=(setregsize.size*8)-bitnumber-1
  1880. else
  1881. result.startbit:=bitnumber;
  1882. result.bitlen:=1;
  1883. end;
  1884. function thlcgobj.get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  1885. var
  1886. tmpreg: tregister;
  1887. begin
  1888. result.ref:=ref;
  1889. result.startbit:=0;
  1890. result.bitlen:=1;
  1891. tmpreg:=getintregister(list,ptruinttype);
  1892. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  1893. a_op_const_reg(list,OP_SHR,ptruinttype,3,tmpreg);
  1894. { don't assign to ref.base, that one is for pointers and this is an index
  1895. (important for platforms like LLVM) }
  1896. if (result.ref.index=NR_NO) then
  1897. result.ref.index:=tmpreg
  1898. else
  1899. begin
  1900. a_op_reg_reg(list,OP_ADD,ptruinttype,result.ref.index,tmpreg);
  1901. result.ref.index:=tmpreg;
  1902. end;
  1903. tmpreg:=getintregister(list,ptruinttype);
  1904. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  1905. a_op_const_reg(list,OP_AND,ptruinttype,7,tmpreg);
  1906. result.bitindexreg:=tmpreg;
  1907. end;
  1908. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1909. var
  1910. reg: tregister;
  1911. regsize: tdef;
  1912. begin
  1913. if (fromsize.size>=tosize.size) then
  1914. regsize:=fromsize
  1915. else
  1916. regsize:=tosize;
  1917. reg:=getfpuregister(list,regsize);
  1918. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1919. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1920. end;
  1921. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  1922. begin
  1923. case loc.loc of
  1924. LOC_REFERENCE, LOC_CREFERENCE:
  1925. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  1926. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1927. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  1928. else
  1929. internalerror(2010120412);
  1930. end;
  1931. end;
  1932. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  1933. begin
  1934. case loc.loc of
  1935. LOC_REFERENCE, LOC_CREFERENCE:
  1936. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  1937. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1938. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  1939. else
  1940. internalerror(2010120413);
  1941. end;
  1942. end;
  1943. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  1944. var
  1945. ref : treference;
  1946. begin
  1947. paramanager.alloccgpara(list,cgpara);
  1948. case cgpara.location^.loc of
  1949. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1950. begin
  1951. cgpara.check_simple_location;
  1952. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  1953. end;
  1954. LOC_REFERENCE,LOC_CREFERENCE:
  1955. begin
  1956. cgpara.check_simple_location;
  1957. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1958. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  1959. end;
  1960. LOC_REGISTER,LOC_CREGISTER:
  1961. begin
  1962. { paramfpu_ref does the check_simpe_location check here if necessary }
  1963. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  1964. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  1965. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  1966. tg.Ungettemp(list,ref);
  1967. end;
  1968. else
  1969. internalerror(2010120422);
  1970. end;
  1971. end;
  1972. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  1973. var
  1974. href : treference;
  1975. // hsize: tcgsize;
  1976. begin
  1977. case cgpara.location^.loc of
  1978. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1979. begin
  1980. cgpara.check_simple_location;
  1981. paramanager.alloccgpara(list,cgpara);
  1982. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  1983. end;
  1984. LOC_REFERENCE,LOC_CREFERENCE:
  1985. begin
  1986. cgpara.check_simple_location;
  1987. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1988. { concatcopy should choose the best way to copy the data }
  1989. g_concatcopy(list,fromsize,ref,href);
  1990. end;
  1991. (* not yet supported
  1992. LOC_REGISTER,LOC_CREGISTER:
  1993. begin
  1994. { force integer size }
  1995. hsize:=int_cgsize(tcgsize2size[size]);
  1996. {$ifndef cpu64bitalu}
  1997. if (hsize in [OS_S64,OS_64]) then
  1998. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1999. else
  2000. {$endif not cpu64bitalu}
  2001. begin
  2002. cgpara.check_simple_location;
  2003. a_load_ref_cgpara(list,hsize,ref,cgpara)
  2004. end;
  2005. end
  2006. *)
  2007. else
  2008. internalerror(2010120423);
  2009. end;
  2010. end;
  2011. (*
  2012. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  2013. begin
  2014. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  2015. end;
  2016. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2017. begin
  2018. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  2019. end;
  2020. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2021. begin
  2022. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  2023. end;
  2024. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  2025. begin
  2026. case loc.loc of
  2027. LOC_MMREGISTER,LOC_CMMREGISTER:
  2028. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2029. LOC_REFERENCE,LOC_CREFERENCE:
  2030. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  2031. LOC_REGISTER,LOC_CREGISTER:
  2032. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2033. else
  2034. internalerror(2010120414);
  2035. end;
  2036. end;
  2037. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  2038. begin
  2039. case loc.loc of
  2040. LOC_MMREGISTER,LOC_CMMREGISTER:
  2041. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  2042. LOC_REFERENCE,LOC_CREFERENCE:
  2043. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  2044. else
  2045. internalerror(2010120415);
  2046. end;
  2047. end;
  2048. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  2049. var
  2050. href : treference;
  2051. begin
  2052. cgpara.check_simple_location;
  2053. paramanager.alloccgpara(list,cgpara);
  2054. case cgpara.location^.loc of
  2055. LOC_MMREGISTER,LOC_CMMREGISTER:
  2056. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  2057. LOC_REFERENCE,LOC_CREFERENCE:
  2058. begin
  2059. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2060. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  2061. end;
  2062. LOC_REGISTER,LOC_CREGISTER:
  2063. begin
  2064. if assigned(shuffle) and
  2065. not shufflescalar(shuffle) then
  2066. internalerror(2009112510);
  2067. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  2068. end
  2069. else
  2070. internalerror(2010120427);
  2071. end;
  2072. end;
  2073. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  2074. var
  2075. hr : tregister;
  2076. hs : tmmshuffle;
  2077. begin
  2078. cgpara.check_simple_location;
  2079. hr:=cg.getmmregister(list,cgpara.size);
  2080. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  2081. if realshuffle(shuffle) then
  2082. begin
  2083. hs:=shuffle^;
  2084. removeshuffles(hs);
  2085. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  2086. end
  2087. else
  2088. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  2089. end;
  2090. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  2091. begin
  2092. {$ifdef extdebug}
  2093. if def_cgsize(fromsize)<>loc.size then
  2094. internalerror(2010112105);
  2095. {$endif}
  2096. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  2097. end;
  2098. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  2099. begin
  2100. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  2101. end;
  2102. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2103. begin
  2104. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  2105. end;
  2106. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  2107. begin
  2108. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  2109. end;
  2110. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2111. begin
  2112. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  2113. end;
  2114. *)
  2115. (*
  2116. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2117. begin
  2118. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  2119. end;
  2120. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  2121. begin
  2122. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  2123. end;
  2124. *)
  2125. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  2126. var
  2127. tmpreg : tregister;
  2128. begin
  2129. tmpreg:=getintregister(list,size);
  2130. a_load_ref_reg(list,size,size,ref,tmpreg);
  2131. a_op_const_reg(list,op,size,a,tmpreg);
  2132. a_load_reg_ref(list,size,size,tmpreg,ref);
  2133. end;
  2134. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  2135. var
  2136. tmpreg: tregister;
  2137. begin
  2138. tmpreg:=getintregister(list,size);
  2139. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  2140. a_op_const_reg(list,op,size,a,tmpreg);
  2141. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  2142. end;
  2143. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  2144. var
  2145. tmpreg: tregister;
  2146. begin
  2147. tmpreg:=getintregister(list,size);
  2148. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  2149. a_op_const_reg(list,op,size,a,tmpreg);
  2150. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  2151. end;
  2152. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  2153. begin
  2154. case loc.loc of
  2155. LOC_REGISTER, LOC_CREGISTER:
  2156. a_op_const_reg(list,op,size,a,loc.register);
  2157. LOC_REFERENCE, LOC_CREFERENCE:
  2158. a_op_const_ref(list,op,size,a,loc.reference);
  2159. LOC_SUBSETREG, LOC_CSUBSETREG:
  2160. a_op_const_subsetreg(list,op,size,size,a,loc.sreg);
  2161. LOC_SUBSETREF, LOC_CSUBSETREF:
  2162. a_op_const_subsetref(list,op,size,size,a,loc.sref);
  2163. else
  2164. internalerror(2010120428);
  2165. end;
  2166. end;
  2167. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  2168. var
  2169. tmpreg: tregister;
  2170. begin
  2171. tmpreg:=getintregister(list,size);
  2172. a_load_ref_reg(list,size,size,ref,tmpreg);
  2173. case op of
  2174. OP_NOT,OP_NEG:
  2175. begin
  2176. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  2177. end;
  2178. else
  2179. begin
  2180. a_op_reg_reg(list,op,size,reg,tmpreg);
  2181. end;
  2182. end;
  2183. a_load_reg_ref(list,size,size,tmpreg,ref);
  2184. end;
  2185. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  2186. var
  2187. tmpreg: tregister;
  2188. begin
  2189. case op of
  2190. OP_NOT,OP_NEG:
  2191. { handle it as "load ref,reg; op reg" }
  2192. begin
  2193. a_load_ref_reg(list,size,size,ref,reg);
  2194. a_op_reg_reg(list,op,size,reg,reg);
  2195. end;
  2196. else
  2197. begin
  2198. tmpreg:=getintregister(list,size);
  2199. a_load_ref_reg(list,size,size,ref,tmpreg);
  2200. a_op_reg_reg(list,op,size,tmpreg,reg);
  2201. end;
  2202. end;
  2203. end;
  2204. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  2205. var
  2206. tmpreg: tregister;
  2207. begin
  2208. tmpreg:=getintregister(list,opsize);
  2209. a_load_subsetreg_reg(list,destsubsetsize,opsize,sreg,tmpreg);
  2210. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2211. a_load_reg_subsetreg(list,opsize,destsubsetsize,tmpreg,sreg);
  2212. end;
  2213. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  2214. var
  2215. tmpreg: tregister;
  2216. begin
  2217. tmpreg:=getintregister(list,opsize);
  2218. a_load_subsetref_reg(list,destsubsetsize,opsize,sref,tmpreg);
  2219. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2220. a_load_reg_subsetref(list,opsize,destsubsetsize,tmpreg,sref);
  2221. end;
  2222. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  2223. begin
  2224. case loc.loc of
  2225. LOC_REGISTER, LOC_CREGISTER:
  2226. a_op_reg_reg(list,op,size,reg,loc.register);
  2227. LOC_REFERENCE, LOC_CREFERENCE:
  2228. a_op_reg_ref(list,op,size,reg,loc.reference);
  2229. LOC_SUBSETREG, LOC_CSUBSETREG:
  2230. a_op_reg_subsetreg(list,op,size,size,reg,loc.sreg);
  2231. LOC_SUBSETREF, LOC_CSUBSETREF:
  2232. a_op_reg_subsetref(list,op,size,size,reg,loc.sref);
  2233. else
  2234. internalerror(2010120429);
  2235. end;
  2236. end;
  2237. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  2238. var
  2239. tmpreg: tregister;
  2240. begin
  2241. case loc.loc of
  2242. LOC_REGISTER,LOC_CREGISTER:
  2243. a_op_ref_reg(list,op,size,ref,loc.register);
  2244. LOC_REFERENCE,LOC_CREFERENCE:
  2245. begin
  2246. tmpreg:=getintregister(list,size);
  2247. a_load_ref_reg(list,size,size,ref,tmpreg);
  2248. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  2249. end;
  2250. LOC_SUBSETREG, LOC_CSUBSETREG:
  2251. begin
  2252. tmpreg:=getintregister(list,size);
  2253. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2254. a_op_ref_reg(list,op,size,ref,tmpreg);
  2255. a_load_reg_subsetreg(list,size,size,tmpreg,loc.sreg);
  2256. end;
  2257. LOC_SUBSETREF, LOC_CSUBSETREF:
  2258. begin
  2259. tmpreg:=getintregister(list,size);
  2260. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2261. a_op_ref_reg(list,op,size,ref,tmpreg);
  2262. a_load_reg_subsetref(list,size,size,tmpreg,loc.sref);
  2263. end;
  2264. else
  2265. internalerror(2010120429);
  2266. end;
  2267. end;
  2268. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  2269. begin
  2270. a_load_reg_reg(list,size,size,src,dst);
  2271. a_op_const_reg(list,op,size,a,dst);
  2272. end;
  2273. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  2274. var
  2275. tmpreg: tregister;
  2276. begin
  2277. if (dst<>src1) then
  2278. begin
  2279. a_load_reg_reg(list,size,size,src2,dst);
  2280. a_op_reg_reg(list,op,size,src1,dst);
  2281. end
  2282. else
  2283. begin
  2284. { can we do a direct operation on the target register ? }
  2285. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  2286. a_op_reg_reg(list,op,size,src2,dst)
  2287. else
  2288. begin
  2289. tmpreg:=getintregister(list,size);
  2290. a_load_reg_reg(list,size,size,src2,tmpreg);
  2291. a_op_reg_reg(list,op,size,src1,tmpreg);
  2292. a_load_reg_reg(list,size,size,tmpreg,dst);
  2293. end;
  2294. end;
  2295. end;
  2296. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2297. begin
  2298. if not setflags then
  2299. a_op_const_reg_reg(list,op,size,a,src,dst)
  2300. else
  2301. internalerror(2010122910);
  2302. end;
  2303. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2304. begin
  2305. if not setflags then
  2306. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  2307. else
  2308. internalerror(2010122911);
  2309. end;
  2310. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  2311. var
  2312. tmpreg: tregister;
  2313. begin
  2314. tmpreg:=getintregister(list,size);
  2315. a_load_const_reg(list,size,a,tmpreg);
  2316. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2317. end;
  2318. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  2319. var
  2320. tmpreg: tregister;
  2321. begin
  2322. tmpreg:=getintregister(list,size);
  2323. a_load_ref_reg(list,size,size,ref,tmpreg);
  2324. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2325. end;
  2326. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  2327. var
  2328. tmpreg: tregister;
  2329. begin
  2330. case loc.loc of
  2331. LOC_REGISTER,LOC_CREGISTER:
  2332. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2333. LOC_REFERENCE,LOC_CREFERENCE:
  2334. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2335. LOC_SUBSETREG, LOC_CSUBSETREG:
  2336. begin
  2337. tmpreg:=getintregister(list,size);
  2338. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2339. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2340. end;
  2341. LOC_SUBSETREF, LOC_CSUBSETREF:
  2342. begin
  2343. tmpreg:=getintregister(list,size);
  2344. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2345. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2346. end;
  2347. else
  2348. internalerror(2010120430);
  2349. end;
  2350. end;
  2351. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  2352. var
  2353. tmpreg: tregister;
  2354. begin
  2355. tmpreg:=getintregister(list,size);
  2356. a_load_ref_reg(list,size,size,ref,tmpreg);
  2357. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2358. end;
  2359. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  2360. var
  2361. tmpreg: tregister;
  2362. begin
  2363. tmpreg:=getintregister(list,size);
  2364. a_load_ref_reg(list,size,size,ref,tmpreg);
  2365. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2366. end;
  2367. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  2368. var
  2369. tmpreg: tregister;
  2370. begin
  2371. tmpreg:=getintregister(list,cmpsize);
  2372. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  2373. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2374. end;
  2375. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  2376. var
  2377. tmpreg: tregister;
  2378. begin
  2379. tmpreg:=getintregister(list,cmpsize);
  2380. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  2381. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2382. end;
  2383. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  2384. begin
  2385. case loc.loc of
  2386. LOC_REGISTER,
  2387. LOC_CREGISTER:
  2388. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2389. LOC_REFERENCE,
  2390. LOC_CREFERENCE :
  2391. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2392. LOC_CONSTANT:
  2393. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2394. LOC_SUBSETREG,
  2395. LOC_CSUBSETREG:
  2396. a_cmp_subsetreg_reg_label(list,size,size,cmp_op,loc.sreg,reg,l);
  2397. LOC_SUBSETREF,
  2398. LOC_CSUBSETREF:
  2399. a_cmp_subsetref_reg_label(list,size,size,cmp_op,loc.sref,reg,l);
  2400. else
  2401. internalerror(2010120431);
  2402. end;
  2403. end;
  2404. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  2405. begin
  2406. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2407. end;
  2408. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  2409. var
  2410. tmpreg: tregister;
  2411. begin
  2412. case loc.loc of
  2413. LOC_REGISTER,LOC_CREGISTER:
  2414. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2415. LOC_REFERENCE,LOC_CREFERENCE:
  2416. begin
  2417. tmpreg:=getintregister(list,size);
  2418. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2419. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2420. end;
  2421. LOC_CONSTANT:
  2422. begin
  2423. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  2424. end;
  2425. LOC_SUBSETREG, LOC_CSUBSETREG:
  2426. begin
  2427. tmpreg:=getintregister(list,size);
  2428. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2429. a_cmp_subsetreg_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2430. end;
  2431. LOC_SUBSETREF, LOC_CSUBSETREF:
  2432. begin
  2433. tmpreg:=getintregister(list,size);
  2434. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2435. a_cmp_subsetref_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2436. end;
  2437. else
  2438. internalerror(2010120432);
  2439. end;
  2440. end;
  2441. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  2442. begin
  2443. {
  2444. if use_vectorfpu(size) then
  2445. a_loadmm_ref_ref()
  2446. else
  2447. }
  2448. if size.typ<>floatdef then
  2449. a_load_ref_ref(list,size,size,source,dest)
  2450. else
  2451. a_loadfpu_ref_ref(list,size,size,source,dest);
  2452. end;
  2453. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  2454. begin
  2455. g_concatcopy(list,size,source,dest);
  2456. end;
  2457. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  2458. var
  2459. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  2460. aintmax: aint;
  2461. {$else}
  2462. aintmax: longint;
  2463. {$endif}
  2464. neglabel : tasmlabel;
  2465. hreg : tregister;
  2466. lto,hto,
  2467. lfrom,hfrom : TConstExprInt;
  2468. fromsize, tosize: cardinal;
  2469. maxdef: tdef;
  2470. from_signed, to_signed: boolean;
  2471. begin
  2472. { range checking on and range checkable value? }
  2473. if not(cs_check_range in current_settings.localswitches) or
  2474. not(fromdef.typ in [orddef,enumdef]) or
  2475. { C-style booleans can't really fail range checks, }
  2476. { all values are always valid }
  2477. is_cbool(todef) then
  2478. exit;
  2479. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  2480. { handle 64bit rangechecks separate for 32bit processors }
  2481. if is_64bit(fromdef) or is_64bit(todef) then
  2482. begin
  2483. cg64.g_rangecheck64(list,l,fromdef,todef);
  2484. exit;
  2485. end;
  2486. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  2487. { only check when assigning to scalar, subranges are different, }
  2488. { when todef=fromdef then the check is always generated }
  2489. getrange(fromdef,lfrom,hfrom);
  2490. getrange(todef,lto,hto);
  2491. from_signed := is_signed(fromdef);
  2492. to_signed := is_signed(todef);
  2493. { check the rangedef of the array, not the array itself }
  2494. { (only change now, since getrange needs the arraydef) }
  2495. if (todef.typ = arraydef) then
  2496. todef := tarraydef(todef).rangedef;
  2497. { no range check if from and to are equal and are both longint/dword }
  2498. { (if we have a 32bit processor) or int64/qword, since such }
  2499. { operations can at most cause overflows (JM) }
  2500. { Note that these checks are mostly processor independent, they only }
  2501. { have to be changed once we introduce 64bit subrange types }
  2502. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  2503. if (fromdef=todef) and
  2504. (fromdef.typ=orddef) and
  2505. (((((torddef(fromdef).ordtype=s64bit) and
  2506. (lfrom = low(int64)) and
  2507. (hfrom = high(int64))) or
  2508. ((torddef(fromdef).ordtype=u64bit) and
  2509. (lfrom = low(qword)) and
  2510. (hfrom = high(qword))) or
  2511. ((torddef(fromdef).ordtype=scurrency) and
  2512. (lfrom = low(int64)) and
  2513. (hfrom = high(int64)))))) then
  2514. exit;
  2515. {$endif cpuhighleveltarget or cpu64bitalu}
  2516. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  2517. targets }
  2518. {$ifdef cpu32bitaddr}
  2519. if (fromdef = todef) and
  2520. (fromdef.typ=orddef) and
  2521. (((((torddef(fromdef).ordtype = s32bit) and
  2522. (lfrom = int64(low(longint))) and
  2523. (hfrom = int64(high(longint)))) or
  2524. ((torddef(fromdef).ordtype = u32bit) and
  2525. (lfrom = low(cardinal)) and
  2526. (hfrom = high(cardinal)))))) then
  2527. exit;
  2528. {$endif cpu32bitaddr}
  2529. { optimize some range checks away in safe cases }
  2530. fromsize := fromdef.size;
  2531. tosize := todef.size;
  2532. if ((from_signed = to_signed) or
  2533. (not from_signed)) and
  2534. (lto<=lfrom) and (hto>=hfrom) and
  2535. (fromsize <= tosize) then
  2536. begin
  2537. { if fromsize < tosize, and both have the same signed-ness or }
  2538. { fromdef is unsigned, then all bit patterns from fromdef are }
  2539. { valid for todef as well }
  2540. if (fromsize < tosize) then
  2541. exit;
  2542. if (fromsize = tosize) and
  2543. (from_signed = to_signed) then
  2544. { only optimize away if all bit patterns which fit in fromsize }
  2545. { are valid for the todef }
  2546. begin
  2547. {$ifopt Q+}
  2548. {$define overflowon}
  2549. {$Q-}
  2550. {$endif}
  2551. {$ifopt R+}
  2552. {$define rangeon}
  2553. {$R-}
  2554. {$endif}
  2555. if to_signed then
  2556. begin
  2557. { calculation of the low/high ranges must not overflow 64 bit
  2558. otherwise we end up comparing with zero for 64 bit data types on
  2559. 64 bit processors }
  2560. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2561. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2562. exit
  2563. end
  2564. else
  2565. begin
  2566. { calculation of the low/high ranges must not overflow 64 bit
  2567. otherwise we end up having all zeros for 64 bit data types on
  2568. 64 bit processors }
  2569. if (lto = 0) and
  2570. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2571. exit
  2572. end;
  2573. {$ifdef overflowon}
  2574. {$Q+}
  2575. {$undef overflowon}
  2576. {$endif}
  2577. {$ifdef rangeon}
  2578. {$R+}
  2579. {$undef rangeon}
  2580. {$endif}
  2581. end
  2582. end;
  2583. { depending on the types involved, we perform the range check for 64 or
  2584. for 32 bit }
  2585. if fromsize=8 then
  2586. maxdef:=fromdef
  2587. else
  2588. maxdef:=todef;
  2589. {$if sizeof(aintmax) = 8}
  2590. if maxdef.size=8 then
  2591. aintmax:=high(int64)
  2592. else
  2593. {$endif}
  2594. begin
  2595. aintmax:=high(longint);
  2596. maxdef:=u32inttype;
  2597. end;
  2598. { generate the rangecheck code for the def where we are going to }
  2599. { store the result }
  2600. { use the trick that }
  2601. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2602. { To be able to do that, we have to make sure however that either }
  2603. { fromdef and todef are both signed or unsigned, or that we leave }
  2604. { the parts < 0 and > maxlongint out }
  2605. if from_signed xor to_signed then
  2606. begin
  2607. if from_signed then
  2608. { from is signed, to is unsigned }
  2609. begin
  2610. { if high(from) < 0 -> always range error }
  2611. if (hfrom < 0) or
  2612. { if low(to) > maxlongint also range error }
  2613. (lto > aintmax) then
  2614. begin
  2615. g_call_system_proc(list,'fpc_rangeerror');
  2616. exit
  2617. end;
  2618. { from is signed and to is unsigned -> when looking at to }
  2619. { as an signed value, it must be < maxaint (otherwise }
  2620. { it will become negative, which is invalid since "to" is unsigned) }
  2621. if hto > aintmax then
  2622. hto := aintmax;
  2623. end
  2624. else
  2625. { from is unsigned, to is signed }
  2626. begin
  2627. if (lfrom > aintmax) or
  2628. (hto < 0) then
  2629. begin
  2630. g_call_system_proc(list,'fpc_rangeerror');
  2631. exit
  2632. end;
  2633. { from is unsigned and to is signed -> when looking at to }
  2634. { as an unsigned value, it must be >= 0 (since negative }
  2635. { values are the same as values > maxlongint) }
  2636. if lto < 0 then
  2637. lto := 0;
  2638. end;
  2639. end;
  2640. hreg:=getintregister(list,maxdef);
  2641. a_load_loc_reg(list,fromdef,maxdef,l,hreg);
  2642. a_op_const_reg(list,OP_SUB,maxdef,tcgint(int64(lto)),hreg);
  2643. current_asmdata.getjumplabel(neglabel);
  2644. {
  2645. if from_signed then
  2646. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2647. else
  2648. }
  2649. if qword(hto-lto)>qword(aintmax) then
  2650. a_cmp_const_reg_label(list,maxdef,OC_BE,aintmax,hreg,neglabel)
  2651. else
  2652. a_cmp_const_reg_label(list,maxdef,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  2653. g_call_system_proc(list,'fpc_rangeerror');
  2654. a_label(list,neglabel);
  2655. end;
  2656. procedure thlcgobj.g_profilecode(list: TAsmList);
  2657. begin
  2658. end;
  2659. procedure thlcgobj.g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  2660. begin
  2661. case regtyp of
  2662. R_INTREGISTER:
  2663. toreg:=getintregister(list,regsize);
  2664. R_ADDRESSREGISTER:
  2665. toreg:=getaddressregister(list,regsize);
  2666. R_FPUREGISTER:
  2667. toreg:=getfpuregister(list,regsize);
  2668. end;
  2669. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  2670. end;
  2671. procedure thlcgobj.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  2672. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  2673. begin
  2674. case regtyp of
  2675. R_INTREGISTER:
  2676. toreg:=getintregister(list,regsize);
  2677. R_ADDRESSREGISTER:
  2678. toreg:=getaddressregister(list,regsize);
  2679. R_FPUREGISTER:
  2680. toreg:=getfpuregister(list,regsize);
  2681. end;
  2682. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  2683. end;
  2684. begin
  2685. toloc:=fromloc;
  2686. case fromloc.loc of
  2687. { volatile location, can't get a permanent reference }
  2688. LOC_REGISTER,
  2689. LOC_FPUREGISTER:
  2690. internalerror(2012012702);
  2691. LOC_CONSTANT:
  2692. { finished }
  2693. ;
  2694. LOC_CREGISTER:
  2695. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  2696. LOC_CFPUREGISTER:
  2697. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  2698. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  2699. reference to such a location for multiple reading }
  2700. LOC_CREFERENCE,
  2701. LOC_REFERENCE:
  2702. begin
  2703. if (fromloc.reference.base<>NR_NO) and
  2704. (fromloc.reference.base<>current_procinfo.framepointer) and
  2705. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  2706. handle_reg_move(voidpointertype,fromloc.reference.base,toloc.reference.base,getregtype(fromloc.reference.base));
  2707. if (fromloc.reference.index<>NR_NO) and
  2708. (fromloc.reference.index<>current_procinfo.framepointer) and
  2709. (fromloc.reference.index<>NR_STACK_POINTER_REG) then
  2710. handle_reg_move(voidpointertype,fromloc.reference.index,toloc.reference.index,getregtype(fromloc.reference.index));
  2711. end;
  2712. else
  2713. internalerror(2012012701);
  2714. end;
  2715. end;
  2716. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  2717. var
  2718. hregister,
  2719. hregister2: tregister;
  2720. hl : tasmlabel;
  2721. oldloc : tlocation;
  2722. begin
  2723. oldloc:=l;
  2724. hregister:=getregisterfordef(list,dst_size);
  2725. { load value in new register }
  2726. case l.loc of
  2727. {$ifdef cpuflags}
  2728. LOC_FLAGS :
  2729. g_flags2reg(list,dst_size,l.resflags,hregister);
  2730. {$endif cpuflags}
  2731. LOC_JUMP :
  2732. begin
  2733. a_label(list,current_procinfo.CurrTrueLabel);
  2734. a_load_const_reg(list,dst_size,1,hregister);
  2735. current_asmdata.getjumplabel(hl);
  2736. a_jmp_always(list,hl);
  2737. a_label(list,current_procinfo.CurrFalseLabel);
  2738. a_load_const_reg(list,dst_size,0,hregister);
  2739. a_label(list,hl);
  2740. end;
  2741. else
  2742. begin
  2743. { load_loc_reg can only handle size >= l.size, when the
  2744. new size is smaller then we need to adjust the size
  2745. of the orignal and maybe recalculate l.register for i386 }
  2746. if (dst_size.size<src_size.size) then
  2747. begin
  2748. hregister2:=getregisterfordef(list,src_size);
  2749. { prevent problems with memory locations -- at this high
  2750. level we cannot twiddle with the reference offset, since
  2751. that may not mean anything (e.g., it refers to fixed-sized
  2752. stack slots on Java) }
  2753. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  2754. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  2755. end
  2756. else
  2757. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  2758. end;
  2759. end;
  2760. if (l.loc <> LOC_CREGISTER) or
  2761. not maybeconst then
  2762. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  2763. else
  2764. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  2765. l.register:=hregister;
  2766. { Release temp if it was a reference }
  2767. if oldloc.loc=LOC_REFERENCE then
  2768. location_freetemp(list,oldloc);
  2769. end;
  2770. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  2771. var
  2772. reg : tregister;
  2773. begin
  2774. if (l.loc<>LOC_FPUREGISTER) and
  2775. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  2776. begin
  2777. { if it's in an mm register, store to memory first }
  2778. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  2779. internalerror(2011012903);
  2780. reg:=getfpuregister(list,size);
  2781. a_loadfpu_loc_reg(list,size,size,l,reg);
  2782. location_freetemp(list,l);
  2783. location_reset(l,LOC_FPUREGISTER,l.size);
  2784. l.register:=reg;
  2785. end;
  2786. end;
  2787. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  2788. var
  2789. r : treference;
  2790. forcesize: aint;
  2791. begin
  2792. case l.loc of
  2793. LOC_FPUREGISTER,
  2794. LOC_CFPUREGISTER :
  2795. begin
  2796. tg.gethltemp(list,size,size.size,tt_normal,r);
  2797. a_loadfpu_reg_ref(list,size,size,l.register,r);
  2798. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  2799. l.reference:=r;
  2800. end;
  2801. (*
  2802. LOC_MMREGISTER,
  2803. LOC_CMMREGISTER:
  2804. begin
  2805. tg.gethltemp(list,size,size.size,tt_normal,r);
  2806. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  2807. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  2808. l.reference:=r;
  2809. end;
  2810. *)
  2811. LOC_CONSTANT,
  2812. LOC_REGISTER,
  2813. LOC_CREGISTER,
  2814. LOC_SUBSETREG,
  2815. LOC_CSUBSETREG,
  2816. LOC_SUBSETREF,
  2817. LOC_CSUBSETREF:
  2818. begin
  2819. if not is_dynamic_array(size) and
  2820. not is_open_array(size) then
  2821. forcesize:=size.size
  2822. else
  2823. forcesize:=voidpointertype.size;
  2824. tg.gethltemp(list,size,forcesize,tt_normal,r);
  2825. a_load_loc_ref(list,size,size,l,r);
  2826. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  2827. l.reference:=r;
  2828. end;
  2829. LOC_CREFERENCE,
  2830. LOC_REFERENCE : ;
  2831. else
  2832. internalerror(2011010304);
  2833. end;
  2834. end;
  2835. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  2836. begin
  2837. case l.loc of
  2838. LOC_REGISTER,
  2839. LOC_CREGISTER :
  2840. begin
  2841. if not loadref then
  2842. internalerror(200410231);
  2843. reference_reset_base(ref,l.register,0,alignment);
  2844. end;
  2845. LOC_REFERENCE,
  2846. LOC_CREFERENCE :
  2847. begin
  2848. if loadref then
  2849. begin
  2850. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  2851. { it's a pointer to def }
  2852. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  2853. end
  2854. else
  2855. ref:=l.reference;
  2856. end;
  2857. else
  2858. internalerror(200309181);
  2859. end;
  2860. end;
  2861. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  2862. {
  2863. produces jumps to true respectively false labels using boolean expressions
  2864. depending on whether the loading of regvars is currently being
  2865. synchronized manually (such as in an if-node) or automatically (most of
  2866. the other cases where this procedure is called), loadregvars can be
  2867. "lr_load_regvars" or "lr_dont_load_regvars"
  2868. }
  2869. var
  2870. storepos : tfileposinfo;
  2871. begin
  2872. if nf_error in p.flags then
  2873. exit;
  2874. storepos:=current_filepos;
  2875. current_filepos:=p.fileinfo;
  2876. if is_boolean(p.resultdef) then
  2877. begin
  2878. if is_constboolnode(p) then
  2879. begin
  2880. if Tordconstnode(p).value.uvalue<>0 then
  2881. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  2882. else
  2883. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  2884. end
  2885. else
  2886. begin
  2887. case p.location.loc of
  2888. LOC_SUBSETREG,LOC_CSUBSETREG,
  2889. LOC_SUBSETREF,LOC_CSUBSETREF,
  2890. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  2891. begin
  2892. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  2893. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  2894. end;
  2895. LOC_JUMP:
  2896. ;
  2897. {$ifdef cpuflags}
  2898. LOC_FLAGS :
  2899. begin
  2900. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  2901. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  2902. end;
  2903. {$endif cpuflags}
  2904. else
  2905. begin
  2906. printnode(output,p);
  2907. internalerror(2011010418);
  2908. end;
  2909. end;
  2910. end;
  2911. end
  2912. else
  2913. internalerror(2011010419);
  2914. current_filepos:=storepos;
  2915. end;
  2916. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  2917. var
  2918. item,
  2919. previtem : TCmdStrListItem;
  2920. begin
  2921. previtem:=nil;
  2922. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  2923. while assigned(item) do
  2924. begin
  2925. {$ifdef arm}
  2926. if current_settings.cputype in cpu_thumb2 then
  2927. list.concat(tai_thumb_func.create);
  2928. {$endif arm}
  2929. { "double link" all procedure entry symbols via .reference }
  2930. { directives on darwin, because otherwise the linker }
  2931. { sometimes strips the procedure if only on of the symbols }
  2932. { is referenced }
  2933. if assigned(previtem) and
  2934. (target_info.system in systems_darwin) then
  2935. list.concat(tai_directive.create(asd_reference,item.str));
  2936. if (cs_profile in current_settings.moduleswitches) or
  2937. (po_global in current_procinfo.procdef.procoptions) then
  2938. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  2939. else
  2940. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  2941. if assigned(previtem) and
  2942. (target_info.system in systems_darwin) then
  2943. list.concat(tai_directive.create(asd_reference,previtem.str));
  2944. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  2945. list.concat(Tai_function_name.create(item.str));
  2946. previtem:=item;
  2947. item := TCmdStrListItem(item.next);
  2948. end;
  2949. current_procinfo.procdef.procstarttai:=tai(list.last);
  2950. end;
  2951. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  2952. begin
  2953. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  2954. current_procinfo.procdef.procendtai:=tai(list.last);
  2955. if (current_module.islibrary) then
  2956. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  2957. { setinitname may generate a new section -> don't add to the
  2958. current list, because we assume this remains a text section }
  2959. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  2960. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2961. begin
  2962. if (target_info.system in (systems_darwin+[system_powerpc_macos]+systems_aix)) and
  2963. not(current_module.islibrary) then
  2964. begin
  2965. new_section(list,sec_code,'',4);
  2966. list.concat(tai_symbol.createname_global(
  2967. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  2968. { keep argc, argv and envp properly on the stack }
  2969. if not(target_info.system in systems_aix) then
  2970. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN')
  2971. else
  2972. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN',false)
  2973. end;
  2974. end;
  2975. end;
  2976. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  2977. begin
  2978. { initialize local data like ansistrings }
  2979. case current_procinfo.procdef.proctypeoption of
  2980. potype_unitinit:
  2981. begin
  2982. { this is also used for initialization of variables in a
  2983. program which does not have a globalsymtable }
  2984. if assigned(current_module.globalsymtable) then
  2985. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  2986. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  2987. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2988. end;
  2989. { units have seperate code for initilization and finalization }
  2990. potype_unitfinalize: ;
  2991. { program init/final is generated in separate procedure }
  2992. potype_proginit:
  2993. begin
  2994. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2995. end;
  2996. else
  2997. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  2998. end;
  2999. { initialises temp. ansi/wide string data }
  3000. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3001. inittempvariables(list);
  3002. {$ifdef OLDREGVARS}
  3003. load_regvars(list,nil);
  3004. {$endif OLDREGVARS}
  3005. end;
  3006. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  3007. var
  3008. old_current_procinfo: tprocinfo;
  3009. begin
  3010. old_current_procinfo:=current_procinfo;
  3011. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3012. begin
  3013. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  3014. exit;
  3015. current_procinfo:=current_procinfo.parent;
  3016. end;
  3017. {$ifdef OLDREGVARS}
  3018. cleanup_regvars(list);
  3019. {$endif OLDREGVARS}
  3020. { finalize temporary data }
  3021. finalizetempvariables(list);
  3022. { finalize local data like ansistrings}
  3023. case current_procinfo.procdef.proctypeoption of
  3024. potype_unitfinalize:
  3025. begin
  3026. { this is also used for initialization of variables in a
  3027. program which does not have a globalsymtable }
  3028. if assigned(current_module.globalsymtable) then
  3029. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3030. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3031. end;
  3032. { units/progs have separate code for initialization and finalization }
  3033. potype_unitinit: ;
  3034. { program init/final is generated in separate procedure }
  3035. potype_proginit: ;
  3036. else
  3037. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  3038. end;
  3039. { finalize paras data }
  3040. if assigned(current_procinfo.procdef.parast) and
  3041. not(po_assembler in current_procinfo.procdef.procoptions) then
  3042. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  3043. current_procinfo:=old_current_procinfo;
  3044. end;
  3045. procedure thlcgobj.gen_entry_code(list: TAsmList);
  3046. begin
  3047. { the actual profile code can clobber some registers,
  3048. therefore if the context must be saved, do it before
  3049. the actual call to the profile code
  3050. }
  3051. if (cs_profile in current_settings.moduleswitches) and
  3052. not(po_assembler in current_procinfo.procdef.procoptions) then
  3053. begin
  3054. { non-win32 can call mcout even in main }
  3055. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  3056. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3057. begin
  3058. g_profilecode(list);
  3059. end;
  3060. end;
  3061. { TODO: create high level version (create compilerprocs in system unit,
  3062. look up procdef, use hlcgobj.a_call_name()) }
  3063. { call startup helpers from main program }
  3064. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3065. begin
  3066. { initialize units }
  3067. cg.allocallcpuregisters(list);
  3068. if not(current_module.islibrary) then
  3069. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  3070. else
  3071. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  3072. cg.deallocallcpuregisters(list);
  3073. end;
  3074. list.concat(Tai_force_line.Create);
  3075. {$ifdef OLDREGVARS}
  3076. load_regvars(list,nil);
  3077. {$endif OLDREGVARS}
  3078. end;
  3079. procedure thlcgobj.gen_exit_code(list: TAsmList);
  3080. begin
  3081. { TODO: create high level version (create compilerproc in system unit,
  3082. look up procdef, use hlcgobj.a_call_name()) }
  3083. { call __EXIT for main program }
  3084. if (not DLLsource) and
  3085. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3086. cg.a_call_name(list,'FPC_DO_EXIT',false);
  3087. end;
  3088. procedure thlcgobj.inittempvariables(list: TAsmList);
  3089. var
  3090. hp : ptemprecord;
  3091. href : treference;
  3092. begin
  3093. hp:=tg.templist;
  3094. while assigned(hp) do
  3095. begin
  3096. if assigned(hp^.def) and
  3097. is_managed_type(hp^.def) then
  3098. begin
  3099. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3100. g_initialize(list,hp^.def,href);
  3101. end;
  3102. hp:=hp^.next;
  3103. end;
  3104. end;
  3105. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  3106. var
  3107. OldAsmList : TAsmList;
  3108. hp : tnode;
  3109. begin
  3110. if (tsym(p).typ = localvarsym) and
  3111. { local (procedure or unit) variables only need initialization if
  3112. they are used }
  3113. ((tabstractvarsym(p).refs>0) or
  3114. { managed return symbols must be inited }
  3115. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  3116. ) and
  3117. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  3118. not(vo_is_external in tabstractvarsym(p).varoptions) and
  3119. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3120. (is_managed_type(tabstractvarsym(p).vardef) or
  3121. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  3122. ) then
  3123. begin
  3124. OldAsmList:=current_asmdata.CurrAsmList;
  3125. current_asmdata.CurrAsmList:=TAsmList(arg);
  3126. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  3127. firstpass(hp);
  3128. secondpass(hp);
  3129. hp.free;
  3130. current_asmdata.CurrAsmList:=OldAsmList;
  3131. end;
  3132. end;
  3133. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  3134. var
  3135. hp : ptemprecord;
  3136. href : treference;
  3137. begin
  3138. hp:=tg.templist;
  3139. while assigned(hp) do
  3140. begin
  3141. if assigned(hp^.def) and
  3142. is_managed_type(hp^.def) then
  3143. begin
  3144. include(current_procinfo.flags,pi_needs_implicit_finally);
  3145. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3146. g_finalize(list,hp^.def,href);
  3147. end;
  3148. hp:=hp^.next;
  3149. end;
  3150. end;
  3151. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  3152. var
  3153. href : treference;
  3154. begin
  3155. if (tsym(p).typ=staticvarsym) then
  3156. begin
  3157. { Static variables can have the initialloc only set to LOC_CxREGISTER
  3158. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  3159. case tstaticvarsym(p).initialloc.loc of
  3160. LOC_CREGISTER :
  3161. begin
  3162. {$ifndef cpu64bitalu}
  3163. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  3164. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  3165. else
  3166. {$endif not cpu64bitalu}
  3167. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  3168. tstaticvarsym(p).initialloc.register);
  3169. end;
  3170. (*
  3171. LOC_CMMREGISTER :
  3172. { clear the whole register }
  3173. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  3174. tstaticvarsym(p).initialloc.register,
  3175. tstaticvarsym(p).initialloc.register,
  3176. nil);
  3177. *)
  3178. LOC_CFPUREGISTER :
  3179. begin
  3180. { initialize fpu regvar by loading from memory }
  3181. reference_reset_symbol(href,
  3182. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  3183. var_align(tstaticvarsym(p).vardef.alignment));
  3184. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  3185. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  3186. end;
  3187. LOC_INVALID :
  3188. ;
  3189. else
  3190. internalerror(200410124);
  3191. end;
  3192. end;
  3193. end;
  3194. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  3195. var
  3196. hp : tnode;
  3197. OldAsmList : TAsmList;
  3198. begin
  3199. include(current_procinfo.flags,pi_needs_implicit_finally);
  3200. OldAsmList:=current_asmdata.CurrAsmList;
  3201. current_asmdata.CurrAsmList:=asmlist;
  3202. hp:=cloadnode.create(sym,sym.owner);
  3203. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  3204. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  3205. hp:=cnodeutils.finalize_data_node(hp);
  3206. firstpass(hp);
  3207. secondpass(hp);
  3208. hp.free;
  3209. current_asmdata.CurrAsmList:=OldAsmList;
  3210. end;
  3211. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  3212. begin
  3213. if (tsym(p).typ=localvarsym) and
  3214. (tlocalvarsym(p).refs>0) and
  3215. not(vo_is_external in tlocalvarsym(p).varoptions) and
  3216. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  3217. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3218. is_managed_type(tlocalvarsym(p).vardef) then
  3219. finalize_sym(TAsmList(arg),tsym(p));
  3220. end;
  3221. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  3222. var
  3223. i : longint;
  3224. pd : tprocdef;
  3225. begin
  3226. case tsym(p).typ of
  3227. staticvarsym :
  3228. begin
  3229. { local (procedure or unit) variables only need finalization
  3230. if they are used
  3231. }
  3232. if ((tstaticvarsym(p).refs>0) or
  3233. { global (unit) variables always need finalization, since
  3234. they may also be used in another unit
  3235. }
  3236. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  3237. (
  3238. (tstaticvarsym(p).varspez<>vs_const) or
  3239. (vo_force_finalize in tstaticvarsym(p).varoptions)
  3240. ) and
  3241. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  3242. not(vo_is_external in tstaticvarsym(p).varoptions) and
  3243. is_managed_type(tstaticvarsym(p).vardef) then
  3244. finalize_sym(TAsmList(arg),tsym(p));
  3245. end;
  3246. procsym :
  3247. begin
  3248. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  3249. begin
  3250. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  3251. if assigned(pd.localst) and
  3252. (pd.procsym=tprocsym(p)) and
  3253. (pd.localst.symtabletype<>staticsymtable) then
  3254. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  3255. end;
  3256. end;
  3257. end;
  3258. end;
  3259. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  3260. var
  3261. list : TAsmList;
  3262. href : treference;
  3263. hsym : tparavarsym;
  3264. eldef : tdef;
  3265. highloc : tlocation;
  3266. begin
  3267. if not(tsym(p).typ=paravarsym) then
  3268. exit;
  3269. list:=TAsmList(arg);
  3270. if is_managed_type(tparavarsym(p).vardef) then
  3271. begin
  3272. if (tparavarsym(p).varspez=vs_value) then
  3273. begin
  3274. include(current_procinfo.flags,pi_needs_implicit_finally);
  3275. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3276. if is_open_array(tparavarsym(p).vardef) then
  3277. begin
  3278. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3279. begin
  3280. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3281. if not assigned(hsym) then
  3282. internalerror(201003032);
  3283. highloc:=hsym.initialloc
  3284. end
  3285. else
  3286. highloc.loc:=LOC_INVALID;
  3287. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3288. g_array_rtti_helper(list,eldef,href,highloc,'FPC_FINALIZE_ARRAY');
  3289. end
  3290. else
  3291. g_finalize(list,tparavarsym(p).vardef,href);
  3292. end;
  3293. end;
  3294. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  3295. if (tparavarsym(p).varspez=vs_value) and
  3296. (is_open_array(tparavarsym(p).vardef) or
  3297. is_array_of_const(tparavarsym(p).vardef)) then
  3298. begin
  3299. { cdecl functions don't have a high pointer so it is not possible to generate
  3300. a local copy }
  3301. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3302. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  3303. end;
  3304. end;
  3305. { generates the code for incrementing the reference count of parameters and
  3306. initialize out parameters }
  3307. { generates the code for incrementing the reference count of parameters and
  3308. initialize out parameters }
  3309. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  3310. var
  3311. href : treference;
  3312. hsym : tparavarsym;
  3313. eldef : tdef;
  3314. list : TAsmList;
  3315. highloc : tlocation;
  3316. needs_inittable (*,
  3317. do_trashing *) : boolean;
  3318. begin
  3319. list:=TAsmList(arg);
  3320. if (tsym(p).typ=paravarsym) then
  3321. begin
  3322. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  3323. (*
  3324. do_trashing:=
  3325. (localvartrashing <> -1) and
  3326. (not assigned(tparavarsym(p).defaultconstsym)) and
  3327. not needs_inittable;
  3328. *)
  3329. case tparavarsym(p).varspez of
  3330. vs_value :
  3331. if needs_inittable then
  3332. begin
  3333. { variants are already handled by the call to fpc_variant_copy_overwrite if
  3334. they are passed by reference }
  3335. if not((tparavarsym(p).vardef.typ=variantdef) and
  3336. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3337. begin
  3338. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3339. if is_open_array(tparavarsym(p).vardef) then
  3340. begin
  3341. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3342. begin
  3343. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3344. if not assigned(hsym) then
  3345. internalerror(201003032);
  3346. highloc:=hsym.initialloc
  3347. end
  3348. else
  3349. highloc.loc:=LOC_INVALID;
  3350. { open arrays do not contain correct element count in their rtti,
  3351. the actual count must be passed separately. }
  3352. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3353. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  3354. end
  3355. else
  3356. g_incrrefcount(list,tparavarsym(p).vardef,href);
  3357. end;
  3358. end;
  3359. vs_out :
  3360. begin
  3361. if needs_inittable (*or
  3362. do_trashing*) then
  3363. begin
  3364. { we have no idea about the alignment at the callee side,
  3365. and the user also cannot specify "unaligned" here, so
  3366. assume worst case }
  3367. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3368. (*
  3369. if do_trashing and
  3370. { needs separate implementation to trash open arrays }
  3371. { since their size is only known at run time }
  3372. not is_special_array(tparavarsym(p).vardef) then
  3373. { may be an open string, even if is_open_string() returns }
  3374. { false (for some helpers in the system unit) }
  3375. if not is_shortstring(tparavarsym(p).vardef) then
  3376. trash_reference(list,href,tparavarsym(p).vardef.size)
  3377. else
  3378. trash_reference(list,href,2);
  3379. *)
  3380. if needs_inittable then
  3381. begin
  3382. if is_open_array(tparavarsym(p).vardef) then
  3383. begin
  3384. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3385. begin
  3386. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3387. if not assigned(hsym) then
  3388. internalerror(201003032);
  3389. highloc:=hsym.initialloc
  3390. end
  3391. else
  3392. highloc.loc:=LOC_INVALID;
  3393. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3394. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  3395. end
  3396. else
  3397. g_initialize(list,tparavarsym(p).vardef,href);
  3398. end;
  3399. end;
  3400. end;
  3401. (*
  3402. else if do_trashing and
  3403. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  3404. begin
  3405. { should always have standard alignment. If a function is assigned
  3406. to a non-aligned variable, the optimisation to pass this variable
  3407. directly as hidden function result must/cannot be performed
  3408. (see tcallnode.funcret_can_be_reused)
  3409. }
  3410. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  3411. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  3412. { may be an open string, even if is_open_string() returns }
  3413. { false (for some helpers in the system unit) }
  3414. if not is_shortstring(tparavarsym(p).vardef) then
  3415. trash_reference(list,href,tparavarsym(p).vardef.size)
  3416. else
  3417. { an open string has at least size 2 }
  3418. trash_reference(list,href,2);
  3419. end
  3420. *)
  3421. end;
  3422. end;
  3423. end;
  3424. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  3425. var
  3426. i: longint;
  3427. currpara: tparavarsym;
  3428. begin
  3429. if (po_assembler in current_procinfo.procdef.procoptions) or
  3430. { exceptfilters have a single hidden 'parentfp' parameter, which
  3431. is handled by tcg.g_proc_entry. }
  3432. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3433. exit;
  3434. { Copy parameters to local references/registers }
  3435. for i:=0 to current_procinfo.procdef.paras.count-1 do
  3436. begin
  3437. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  3438. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  3439. end;
  3440. { generate copies of call by value parameters, must be done before
  3441. the initialization and body is parsed because the refcounts are
  3442. incremented using the local copies }
  3443. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  3444. if not(po_assembler in current_procinfo.procdef.procoptions) then
  3445. begin
  3446. { has to be done here rather than in gen_initialize_code, because
  3447. the initialisation code is generated a) later and b) with
  3448. rad_backwards, so the register allocator would generate
  3449. information as if this code comes before loading the parameters
  3450. from their original registers to their local location }
  3451. // if (localvartrashing <> -1) then
  3452. // current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  3453. { initialize refcounted paras, and trash others. Needed here
  3454. instead of in gen_initialize_code, because when a reference is
  3455. intialised or trashed while the pointer to that reference is kept
  3456. in a regvar, we add a register move and that one again has to
  3457. come after the parameter loading code as far as the register
  3458. allocator is concerned }
  3459. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  3460. end;
  3461. end;
  3462. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  3463. var
  3464. href : treference;
  3465. hreg : tregister;
  3466. list : TAsmList;
  3467. hsym : tparavarsym;
  3468. l : longint;
  3469. highloc,
  3470. localcopyloc : tlocation;
  3471. begin
  3472. list:=TAsmList(arg);
  3473. if (tsym(p).typ=paravarsym) and
  3474. (tparavarsym(p).varspez=vs_value) and
  3475. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3476. begin
  3477. { we have no idea about the alignment at the caller side }
  3478. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3479. if is_open_array(tparavarsym(p).vardef) or
  3480. is_array_of_const(tparavarsym(p).vardef) then
  3481. begin
  3482. { cdecl functions don't have a high pointer so it is not possible to generate
  3483. a local copy }
  3484. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3485. begin
  3486. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3487. begin
  3488. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3489. if not assigned(hsym) then
  3490. internalerror(2011020506);
  3491. highloc:=hsym.initialloc
  3492. end
  3493. else
  3494. highloc.loc:=LOC_INVALID;
  3495. hreg:=getaddressregister(list,voidpointertype);
  3496. if not is_packed_array(tparavarsym(p).vardef) then
  3497. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  3498. else
  3499. internalerror(2011020507);
  3500. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  3501. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  3502. end;
  3503. end
  3504. else
  3505. begin
  3506. { Allocate space for the local copy }
  3507. l:=tparavarsym(p).getsize;
  3508. localcopyloc.loc:=LOC_REFERENCE;
  3509. localcopyloc.size:=int_cgsize(l);
  3510. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  3511. { Copy data }
  3512. if is_shortstring(tparavarsym(p).vardef) then
  3513. begin
  3514. { this code is only executed before the code for the body and the entry/exit code is generated
  3515. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3516. }
  3517. include(current_procinfo.flags,pi_do_call);
  3518. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  3519. end
  3520. else if tparavarsym(p).vardef.typ=variantdef then
  3521. begin
  3522. { this code is only executed before the code for the body and the entry/exit code is generated
  3523. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3524. }
  3525. include(current_procinfo.flags,pi_do_call);
  3526. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  3527. end
  3528. else
  3529. begin
  3530. { pass proper alignment info }
  3531. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  3532. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  3533. end;
  3534. { update localloc of varsym }
  3535. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  3536. tparavarsym(p).localloc:=localcopyloc;
  3537. tparavarsym(p).initialloc:=localcopyloc;
  3538. end;
  3539. end;
  3540. end;
  3541. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  3542. begin
  3543. case l.loc of
  3544. (*
  3545. LOC_MMREGISTER,
  3546. LOC_CMMREGISTER:
  3547. case cgpara.location^.loc of
  3548. LOC_REFERENCE,
  3549. LOC_CREFERENCE,
  3550. LOC_MMREGISTER,
  3551. LOC_CMMREGISTER,
  3552. LOC_REGISTER,
  3553. LOC_CREGISTER :
  3554. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  3555. LOC_FPUREGISTER,
  3556. LOC_CFPUREGISTER:
  3557. begin
  3558. tmploc:=l;
  3559. location_force_fpureg(list,tmploc,false);
  3560. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  3561. end;
  3562. else
  3563. internalerror(200204249);
  3564. end;
  3565. *)
  3566. LOC_FPUREGISTER,
  3567. LOC_CFPUREGISTER:
  3568. case cgpara.location^.loc of
  3569. (*
  3570. LOC_MMREGISTER,
  3571. LOC_CMMREGISTER:
  3572. begin
  3573. tmploc:=l;
  3574. location_force_mmregscalar(list,tmploc,false);
  3575. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  3576. end;
  3577. *)
  3578. { Some targets pass floats in normal registers }
  3579. LOC_REGISTER,
  3580. LOC_CREGISTER,
  3581. LOC_REFERENCE,
  3582. LOC_CREFERENCE,
  3583. LOC_FPUREGISTER,
  3584. LOC_CFPUREGISTER:
  3585. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  3586. else
  3587. internalerror(2011010210);
  3588. end;
  3589. LOC_REFERENCE,
  3590. LOC_CREFERENCE:
  3591. case cgpara.location^.loc of
  3592. (*
  3593. LOC_MMREGISTER,
  3594. LOC_CMMREGISTER:
  3595. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  3596. *)
  3597. { Some targets pass floats in normal registers }
  3598. LOC_REGISTER,
  3599. LOC_CREGISTER,
  3600. LOC_REFERENCE,
  3601. LOC_CREFERENCE,
  3602. LOC_FPUREGISTER,
  3603. LOC_CFPUREGISTER:
  3604. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  3605. else
  3606. internalerror(2011010211);
  3607. end;
  3608. LOC_REGISTER,
  3609. LOC_CREGISTER :
  3610. a_load_loc_cgpara(list,size,l,cgpara);
  3611. else
  3612. internalerror(2011010212);
  3613. end;
  3614. end;
  3615. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  3616. begin
  3617. { do nothing by default }
  3618. end;
  3619. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  3620. begin
  3621. { Handle Floating point types differently
  3622. This doesn't depend on emulator settings, emulator settings should
  3623. be handled by cpupara }
  3624. if (vardef.typ=floatdef) or
  3625. { some ABIs return certain records in an fpu register }
  3626. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  3627. (assigned(cgpara.location) and
  3628. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  3629. begin
  3630. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  3631. exit;
  3632. end;
  3633. case l.loc of
  3634. LOC_CONSTANT,
  3635. LOC_REGISTER,
  3636. LOC_CREGISTER,
  3637. LOC_REFERENCE,
  3638. LOC_CREFERENCE :
  3639. begin
  3640. a_load_loc_cgpara(list,vardef,l,cgpara);
  3641. end;
  3642. (*
  3643. LOC_MMREGISTER,
  3644. LOC_CMMREGISTER:
  3645. begin
  3646. case l.size of
  3647. OS_F32,
  3648. OS_F64:
  3649. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  3650. else
  3651. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  3652. end;
  3653. end;
  3654. *)
  3655. else
  3656. internalerror(2011010213);
  3657. end;
  3658. end;
  3659. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  3660. var
  3661. href : treference;
  3662. begin
  3663. para.check_simple_location;
  3664. { skip e.g. empty records }
  3665. if (para.location^.loc = LOC_VOID) then
  3666. exit;
  3667. case destloc.loc of
  3668. LOC_REFERENCE :
  3669. begin
  3670. { If the parameter location is reused we don't need to copy
  3671. anything }
  3672. if not reusepara then
  3673. begin
  3674. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  3675. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  3676. end;
  3677. end;
  3678. { TODO other possible locations }
  3679. else
  3680. internalerror(2011010308);
  3681. end;
  3682. end;
  3683. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  3684. var
  3685. ressym : tabstractnormalvarsym;
  3686. funcretloc : TCGPara;
  3687. begin
  3688. { Is the loading needed? }
  3689. if is_void(current_procinfo.procdef.returndef) or
  3690. (
  3691. (po_assembler in current_procinfo.procdef.procoptions) and
  3692. (not(assigned(current_procinfo.procdef.funcretsym)) or
  3693. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  3694. ) then
  3695. exit;
  3696. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  3697. { constructors return self }
  3698. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  3699. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  3700. else
  3701. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  3702. if (ressym.refs>0) or
  3703. is_managed_type(ressym.vardef) then
  3704. begin
  3705. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  3706. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  3707. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  3708. end
  3709. else
  3710. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  3711. end;
  3712. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  3713. begin
  3714. { add the procedure to the al_procedures }
  3715. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  3716. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  3717. current_asmdata.asmlists[al_procedures].concatlist(code);
  3718. { save local data (casetable) also in the same file }
  3719. if assigned(data) and
  3720. (not data.empty) then
  3721. current_asmdata.asmlists[al_procedures].concatlist(data);
  3722. end;
  3723. procedure thlcgobj.g_call_system_proc(list: TAsmList; const procname: string);
  3724. var
  3725. srsym: tsym;
  3726. pd: tprocdef;
  3727. begin
  3728. srsym:=tsym(systemunit.find(procname));
  3729. if not assigned(srsym) and
  3730. (cs_compilesystem in current_settings.moduleswitches) then
  3731. srsym:=tsym(systemunit.Find(upper(procname)));
  3732. if not assigned(srsym) or
  3733. (srsym.typ<>procsym) then
  3734. Message1(cg_f_unknown_compilerproc,procname);
  3735. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3736. a_call_name(list,pd,pd.mangledname,false);
  3737. end;
  3738. end.