hlcgobj.pas 172 KB

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