hlcgobj.pas 224 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264
  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,symbase,symtype,symsym,symdef,
  32. node,nutils,
  33. tgobj
  34. ;
  35. type
  36. tsubsetloadopt = (SL_REG,SL_REGNOSRCMASK,SL_SETZERO,SL_SETMAX);
  37. preplaceregrec = ^treplaceregrec;
  38. treplaceregrec = record
  39. old, new: tregister;
  40. oldhi, newhi: tregister;
  41. ressym: tsym;
  42. { moved sym }
  43. sym : tabstractnormalvarsym;
  44. end;
  45. {# @abstract(Abstract high level code generator)
  46. This class implements an abstract instruction generator. All
  47. methods of this class are generic and are mapped to low level code
  48. generator methods by default. They have to be overridden for higher
  49. level targets
  50. }
  51. { thlcgobj }
  52. thlcgobj = class
  53. public
  54. {************************************************}
  55. { basic routines }
  56. constructor create;
  57. {# Initialize the register allocators needed for the codegenerator.}
  58. procedure init_register_allocators;virtual;
  59. {# Clean up the register allocators needed for the codegenerator.}
  60. procedure done_register_allocators;virtual;
  61. {# 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. }
  62. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  63. {# Gets a register suitable to do integer operations on.}
  64. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  65. {# Gets a register suitable to do integer operations on.}
  66. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  67. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  68. { warning: only works correctly for fpu types currently }
  69. function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  70. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  71. function gettempregister(list:TAsmList;size:tdef):Tregister;virtual;
  72. function getregisterfordef(list: TAsmList;size:tdef):Tregister;virtual;
  73. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  74. the cpu specific child cg object have such a method?}
  75. function uses_registers(rt:Tregistertype):boolean; inline;
  76. {# Get a specific register.}
  77. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  78. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  79. {# Get multiple registers specified.}
  80. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  81. {# Free multiple registers specified.}
  82. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  83. procedure allocallcpuregisters(list:TAsmList);virtual;
  84. procedure deallocallcpuregisters(list:TAsmList);virtual;
  85. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  86. procedure translate_register(var reg : tregister); inline;
  87. {# Returns the kind of register this type should be loaded in (it does not
  88. check whether this is actually possible, but if it's loaded in a register
  89. by the compiler for any purpose other than parameter passing/function
  90. result loading, this is the register type used) }
  91. class function def2regtyp(def: tdef): tregistertype; virtual;
  92. {# Returns a reference with its base address set from a pointer that
  93. has been loaded in a register.
  94. A generic version is provided. This routine should be overridden
  95. on platforms which support pointers with different sizes (for
  96. example i8086 near and far pointers) or require some other sort of
  97. special consideration when converting a pointer in a register to a
  98. reference.
  99. @param(ref where the result is returned)
  100. @param(regsize the type of the pointer, contained in the reg parameter)
  101. @param(reg register containing the value of a pointer)
  102. }
  103. procedure reference_reset_base(var ref: treference; regsize: tdef; reg: tregister; offset, alignment: longint); virtual;
  104. {# Returns a reference corresponding to a temp }
  105. procedure temp_to_ref(p: ptemprecord; out ref: treference); virtual;
  106. {# Emit a label to the instruction stream. }
  107. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  108. {# Allocates register r by inserting a pai_realloc record }
  109. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  110. {# Deallocates register r by inserting a pa_regdealloc record}
  111. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  112. { Synchronize register, make sure it is still valid }
  113. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  114. {# Pass a parameter, which is located in a register, to a routine.
  115. This routine should push/send the parameter to the routine, as
  116. required by the specific processor ABI and routine modifiers.
  117. It must generate register allocation information for the cgpara in
  118. case it consists of cpuregisters.
  119. @param(size size of the operand in the register)
  120. @param(r register source of the operand)
  121. @param(cgpara where the parameter will be stored)
  122. }
  123. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  124. {# Pass a parameter, which is a constant, to a routine.
  125. A generic version is provided. This routine should
  126. be overridden for optimization purposes if the cpu
  127. permits directly sending this type of parameter.
  128. It must generate register allocation information for the cgpara in
  129. case it consists of cpuregisters.
  130. @param(size size of the operand in constant)
  131. @param(a value of constant to send)
  132. @param(cgpara where the parameter will be stored)
  133. }
  134. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);virtual;
  135. {# Pass the value of a parameter, which is located in memory, to a routine.
  136. A generic version is provided. This routine should
  137. be overridden for optimization purposes if the cpu
  138. permits directly sending this type of parameter.
  139. It must generate register allocation information for the cgpara in
  140. case it consists of cpuregisters.
  141. @param(size size of the operand in constant)
  142. @param(r Memory reference of value to send)
  143. @param(cgpara where the parameter will be stored)
  144. }
  145. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  146. {# Pass the value of a parameter, which can be located either in a register or memory location,
  147. to a routine.
  148. A generic version is provided.
  149. @param(l location of the operand to send)
  150. @param(nr parameter number (starting from one) of routine (from left to right))
  151. @param(cgpara where the parameter will be stored)
  152. }
  153. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  154. {# Pass the address of a reference to a routine. This routine
  155. will calculate the address of the reference, and pass this
  156. calculated address as a parameter.
  157. It must generate register allocation information for the cgpara in
  158. case it consists of cpuregisters.
  159. A generic version is provided. This routine should
  160. be overridden for optimization purposes if the cpu
  161. permits directly sending this type of parameter.
  162. @param(fromsize type of the reference we are taking the address of)
  163. @param(tosize type of the pointer that we get as a result)
  164. @param(r reference to get address from)
  165. }
  166. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  167. { Remarks:
  168. * If a method specifies a size you have only to take care
  169. of that number of bits, i.e. load_const_reg with OP_8 must
  170. only load the lower 8 bit of the specified register
  171. the rest of the register can be undefined
  172. if necessary the compiler will call a method
  173. to zero or sign extend the register
  174. * The a_load_XX_XX with OP_64 needn't to be
  175. implemented for 32 bit
  176. processors, the code generator takes care of that
  177. * the addr size is for work with the natural pointer
  178. size
  179. * the procedures without fpu/mm are only for integer usage
  180. * normally the first location is the source and the
  181. second the destination
  182. }
  183. {# Emits instruction to call the method specified by symbol name.
  184. Returns the function result location.
  185. This routine must be overridden for each new target cpu.
  186. }
  187. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;virtual;abstract;
  188. function a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister; const paras: array of pcgpara): tcgpara;virtual;abstract;
  189. { same as a_call_name, might be overridden on certain architectures to emit
  190. static calls without usage of a got trampoline }
  191. function a_call_name_static(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef): tcgpara;virtual;
  192. { same as a_call_name, might be overridden on certain architectures to emit
  193. special static calls for inherited methods }
  194. function a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara): tcgpara;virtual;
  195. { move instructions }
  196. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);virtual;abstract;
  197. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);virtual;
  198. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : tcgint;const loc : tlocation);virtual;
  199. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  200. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  201. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  202. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  203. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  204. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  205. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  206. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  207. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  208. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  209. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  210. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  211. { For subsetreg/ref, the def is the size of the packed element. The
  212. size of the register that holds the data is a tcgsize, and hence
  213. always must be an orddef of the corresponding size in practice }
  214. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual;
  215. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual;
  216. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual;
  217. procedure a_load_subsetreg_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual;
  218. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual;
  219. procedure a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sreg: tsubsetregister); virtual;
  220. procedure a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  221. procedure a_load_subsetref_reg(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual;
  222. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual;
  223. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual;
  224. procedure a_load_subsetref_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual;
  225. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual;
  226. procedure a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference); virtual;
  227. procedure a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  228. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  229. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  230. { bit test instructions }
  231. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual;
  232. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual;
  233. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual;
  234. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual;
  235. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual;
  236. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  237. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  238. { bit set/clear instructions }
  239. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual;
  240. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: tcgint; const ref: treference); virtual;
  241. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: tcgint; destreg: tregister); virtual;
  242. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: tcgint; const destreg: tsubsetregister); virtual;
  243. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual;
  244. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  245. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: tcgint; const loc: tlocation);virtual;
  246. function get_call_result_cgpara(pd: tabstractprocdef; forceresdef: tdef): tcgpara; virtual;
  247. protected
  248. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  249. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  250. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  251. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  252. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  253. { return a subsetref that represents bit "bitnumber" in ref, if ref
  254. has the type "refdef". The subsetref must be addressable via
  255. (unsigned) 8 bit access, unless all the *_bit_* methods are
  256. overloaded and use something else. }
  257. function get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  258. function get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  259. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  260. public
  261. { bit scan instructions (still need transformation to thlcgobj) }
  262. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); virtual; abstract;
  263. { fpu move instructions }
  264. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  265. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  266. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  267. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  268. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  269. procedure a_loadfpu_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);virtual;
  270. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  271. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  272. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  273. { vector register move instructions }
  274. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  275. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  276. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  277. procedure a_loadmm_ref_ref(list: TAsmList; fromsize, tosize: tdef; const fromref, toref: treference; shuffle: pmmshuffle); virtual;
  278. { required for subsetreg/ref; still tcgsize rather than tdef because of reason mentioned above }
  279. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle : pmmshuffle);virtual;
  280. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  281. procedure a_loadmm_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference; shuffle : pmmshuffle);virtual;
  282. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  283. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  284. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  285. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual; abstract;
  286. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  287. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  288. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  289. { requires a temp that is interpreted in two different ways, and we
  290. don't have a way (yet) to tag a treference with tdef information so
  291. targets like LLVM can insert the necessary bitcast
  292. }
  293. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual; abstract;
  294. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual; abstract;
  295. { basic arithmetic operations }
  296. { note: for operators which require only one argument (not, neg), use }
  297. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  298. { that in this case the *second* operand is used as both source and }
  299. { destination (JM) }
  300. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); virtual; abstract;
  301. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); virtual;
  302. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : tcgint; const sreg: tsubsetregister); virtual;
  303. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : tcgint; const sref: tsubsetreference); virtual;
  304. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: tcgint; const loc: tlocation);virtual;
  305. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  306. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  307. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  308. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  309. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  310. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  311. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  312. { trinary operations for processors that support them, 'emulated' }
  313. { on others. None with "ref" arguments since I don't think there }
  314. { are any processors that support it (JM) }
  315. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); virtual;
  316. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  317. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  318. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  319. { comparison operations }
  320. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : tcgint;reg : tregister;
  321. l : tasmlabel);virtual;
  322. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : tcgint;const ref : treference;
  323. l : tasmlabel); virtual;
  324. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  325. l : tasmlabel);virtual;
  326. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  327. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  328. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  329. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  330. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  331. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  332. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  333. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  334. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  335. {$ifdef cpuflags}
  336. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  337. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  338. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  339. }
  340. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  341. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  342. {$endif cpuflags}
  343. {#
  344. This routine is used in exception management nodes. It should
  345. save the exception reason currently in the reg. The
  346. save should be done either to a temp (pointed to by href).
  347. or on the stack (pushing the value on the stack).
  348. }
  349. procedure g_exception_reason_save(list : TAsmList; fromsize, tosize: tdef; reg: tregister; const href : treference);virtual;
  350. {#
  351. This routine is used in exception management nodes. It should
  352. save the exception reason constant. The
  353. save should be done either to a temp (pointed to by href).
  354. or on the stack (pushing the value on the stack).
  355. The size of the value to save is OS_S32. The default version
  356. saves the exception reason to a temp. memory area.
  357. }
  358. procedure g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference);virtual;
  359. {#
  360. This routine is used in exception management nodes. It should
  361. load the exception reason to reg. The saved value
  362. should either be in the temp. area (pointed to by href , href should
  363. *NOT* be freed) or on the stack (the value should be popped).
  364. The size of the value to save is OS_S32. The default version
  365. saves the exception reason to a temp. memory area.
  366. }
  367. procedure g_exception_reason_load(list : TAsmList; fromsize, tosize: tdef; const href : treference; reg: tregister);virtual;
  368. {#
  369. This routine is called when the current exception reason can be
  370. discarded. On platforms that use push/pop, it causes the current
  371. value to be popped. On other platforms it doesn't do anything
  372. }
  373. procedure g_exception_reason_discard(list : TAsmList; size: tdef; href: treference); virtual;
  374. procedure g_maybe_testself(list : TAsmList; selftype: tdef; reg:tregister);
  375. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  376. {# This should emit the opcode to copy len bytes from the source
  377. to destination.
  378. It must be overridden for each new target processor.
  379. @param(source Source reference of copy)
  380. @param(dest Destination reference of copy)
  381. }
  382. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  383. {# This should emit the opcode to copy len bytes from the an unaligned source
  384. to destination.
  385. It must be overridden for each new target processor.
  386. @param(source Source reference of copy)
  387. @param(dest Destination reference of copy)
  388. }
  389. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  390. {# This should emit the opcode to a shortrstring from the source
  391. to destination.
  392. @param(source Source reference of copy)
  393. @param(dest Destination reference of copy)
  394. }
  395. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;
  396. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;
  397. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;
  398. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;
  399. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;
  400. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  401. const name: string);virtual;
  402. {# Generates range checking code. It is to note
  403. that this routine does not need to be overridden,
  404. as it takes care of everything.
  405. @param(p Node which contains the value to check)
  406. @param(todef Type definition of node to range check)
  407. }
  408. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  409. {# Generates overflow checking code for a node }
  410. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  411. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  412. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;
  413. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;
  414. {# Emits instructions when compilation is done in profile
  415. mode (this is set as a command line option). The default
  416. behavior does nothing, should be overridden as required.
  417. }
  418. procedure g_profilecode(list : TAsmList);virtual;
  419. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  420. @param(size Number of bytes to allocate)
  421. }
  422. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  423. {# Emits instruction for allocating the locals in entry
  424. code of a routine. This is one of the first
  425. routine called in @var(genentrycode).
  426. @param(localsize Number of bytes to allocate as locals)
  427. }
  428. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  429. {# Emits instructions for returning from a subroutine.
  430. Should also restore the framepointer and stack.
  431. @param(parasize Number of bytes of parameters to deallocate from stack)
  432. }
  433. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  434. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  435. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  436. { generate a stub which only purpose is to pass control the given external method,
  437. setting up any additional environment before doing so (if required).
  438. The default implementation issues a jump instruction to the external name. }
  439. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  440. protected
  441. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  442. public
  443. { create "safe copy" of a tlocation that can be used later: all
  444. registers used in the tlocation are copied to new ones, so that
  445. even if the original ones change, things stay the same (except if
  446. the original location was already a register, then the register is
  447. kept). Must only be used on lvalue locations.
  448. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  449. for targets without pointers. }
  450. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  451. { typecasts the pointer in reg to a new pointer. By default it does
  452. nothing, only required for type-aware platforms like LLVM.
  453. fromdef/todef are not typed as pointerdef, because they may also be
  454. a procvardef or classrefdef. Replaces reg with a new register if
  455. necessary }
  456. procedure g_ptrtypecast_reg(list: TAsmList; fromdef, todef: tdef; var reg: tregister); virtual;
  457. { same but for a treference (considers the reference itself, not the
  458. value stored at that place in memory). Replaces ref with a new
  459. reference if necessary. fromdef needs to be a pointerdef because
  460. it may have to be passed as fromdef to a_loadaddr_ref_reg, which
  461. needs the "pointeddef" of fromdef }
  462. procedure g_ptrtypecast_ref(list: TAsmList; fromdef, todef: tdef; var ref: treference); virtual;
  463. { update a reference pointing to the start address of a record/object/
  464. class (contents) so it refers to the indicated field }
  465. procedure g_set_addr_nonbitpacked_field_ref(list: TAsmList; recdef: tabstractrecorddef; field: tfieldvarsym; var recref: treference); virtual;
  466. { load a register/constant into a record field by name }
  467. protected
  468. procedure g_setup_load_field_by_name(list: TAsmList; recdef: trecorddef; const name: TIDString; const recref: treference; out fref: treference; out fielddef: tdef);
  469. public
  470. procedure g_load_reg_field_by_name(list: TAsmList; regsize: tdef; recdef: trecorddef; reg: tregister; const name: TIDString; const recref: treference);
  471. procedure g_load_const_field_by_name(list: TAsmList; recdef: trecorddef; a: tcgint; const name: TIDString; const recref: treference);
  472. { laod a named field into a register }
  473. procedure g_load_field_reg_by_name(list: TAsmList; recdef: trecorddef; regsize: tdef; const name: TIDString; const recref: treference; reg: tregister);
  474. { same as above, but allocates the register and determines the def
  475. based on the type of the field }
  476. procedure g_force_field_reg_by_name(list: TAsmList; recdef: trecorddef; const name: TIDString; const recref: treference; out regdef: tdef; out reg: tregister);
  477. { routines migrated from ncgutil }
  478. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  479. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  480. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  481. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;
  482. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  483. { Retrieve the location of the data pointed to in location l, when the location is
  484. a register it is expected to contain the address of the data }
  485. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  486. { if p is a boolean expression, turns p.location into a LOC_JUMP with
  487. jumps to generated true and false labels; otherwise internalerrors }
  488. procedure maketojumpbool(list: TAsmList; p: tnode);
  489. { same as above, but using predefined true/false labels instead of
  490. by generating new ones }
  491. procedure maketojumpboollabels(list: TAsmList; p: tnode; truelabel, falselabel: tasmlabel);virtual;
  492. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding
  493. loadn and change its location to a new register (= SSA). In case reload
  494. is true, transfer the old to the new register }
  495. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); virtual;
  496. private
  497. function do_replace_node_regs(var n: tnode; para: pointer): foreachnoderesult; virtual;
  498. public
  499. procedure gen_proc_symbol(list:TAsmList);virtual;
  500. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  501. procedure handle_external_proc(list: TAsmList; pd: tprocdef; const importname: TSymStr); virtual;
  502. procedure gen_initialize_code(list:TAsmList);virtual;
  503. procedure gen_finalize_code(list:TAsmList);virtual;
  504. procedure gen_entry_code(list:TAsmList);virtual;
  505. procedure gen_exit_code(list:TAsmList);virtual;
  506. protected
  507. { helpers called by gen_initialize_code/gen_finalize_code }
  508. procedure inittempvariables(list:TAsmList);virtual;
  509. procedure finalizetempvariables(list:TAsmList);virtual;
  510. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  511. { generates the code for decrementing the reference count of parameters }
  512. procedure final_paras(p:TObject;arg:pointer);
  513. public
  514. { helper called by gen_alloc_symtable }
  515. procedure varsym_set_localloc(list: TAsmList; vs:tabstractnormalvarsym); virtual;
  516. { helper called by gen_alloc_symtable }
  517. procedure paravarsym_set_initialloc_to_paraloc(vs: tparavarsym); virtual;
  518. procedure gen_load_para_value(list:TAsmList);virtual;
  519. { helpers called by gen_load_para_value }
  520. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  521. protected
  522. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  523. procedure init_paras(p:TObject;arg:pointer);
  524. protected
  525. { Some targets have to put "something" in the function result
  526. location if it's not initialised by the Pascal code, e.g.
  527. stack-based architectures. By default it does nothing }
  528. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  529. procedure gen_load_loc_function_result(list: TAsmList; vardef: tdef; const l: tlocation);virtual;
  530. public
  531. { load a tlocation into a cgpara }
  532. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  533. { load a cgpara into a tlocation }
  534. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  535. { load the function return value into the ABI-defined function return location }
  536. procedure gen_load_return_value(list:TAsmList);virtual;
  537. { extras refactored from other units }
  538. procedure gen_stack_check_size_para(list:TAsmList); virtual;
  539. procedure gen_stack_check_call(list:TAsmList); virtual;
  540. { queue the code/data generated for a procedure for writing out to
  541. the assembler/object file }
  542. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  543. { generate a call to a routine in the system unit }
  544. function g_call_system_proc(list: TAsmList; const procname: string; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  545. function g_call_system_proc(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  546. protected
  547. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; virtual;
  548. public
  549. { Generate code to exit an unwind-protected region. The default implementation
  550. produces a simple jump to destination label. }
  551. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  552. end;
  553. thlcgobjclass = class of thlcgobj;
  554. var
  555. {# Main high level code generator class }
  556. hlcg : thlcgobj;
  557. { class type of high level code generator class (also valid when hlcg is
  558. nil, in order to be able to call its virtual class methods) }
  559. chlcgobj: thlcgobjclass;
  560. procedure destroy_hlcodegen;
  561. implementation
  562. uses
  563. globals,systems,
  564. fmodule,export,
  565. verbose,defutil,paramgr,
  566. symtable,
  567. nbas,ncon,nld,ncgrtti,pass_2,
  568. cpuinfo,cgobj,cutils,procinfo,
  569. {$ifdef x86}
  570. cgx86,
  571. {$endif x86}
  572. ncgutil,ngenutil;
  573. procedure destroy_hlcodegen;
  574. begin
  575. hlcg.free;
  576. hlcg:=nil;
  577. destroy_codegen;
  578. end;
  579. { thlcgobj }
  580. constructor thlcgobj.create;
  581. begin
  582. end;
  583. procedure thlcgobj.init_register_allocators;
  584. begin
  585. cg.init_register_allocators;
  586. end;
  587. procedure thlcgobj.done_register_allocators;
  588. begin
  589. cg.done_register_allocators;
  590. end;
  591. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  592. begin
  593. cg.set_regalloc_live_range_direction(dir);
  594. end;
  595. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  596. begin
  597. result:=cg.getintregister(list,def_cgsize(size));
  598. end;
  599. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  600. begin
  601. result:=cg.getaddressregister(list);
  602. end;
  603. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  604. begin
  605. result:=cg.getfpuregister(list,def_cgsize(size));
  606. end;
  607. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  608. begin
  609. result:=cg.getmmregister(list,def_cgsize(size));
  610. end;
  611. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  612. begin
  613. result:=cg.getflagregister(list,def_cgsize(size));
  614. end;
  615. function thlcgobj.gettempregister(list: TAsmList; size: tdef): Tregister;
  616. begin
  617. { doesn't make sense to try to translate this size to tcgsize, temps
  618. can have any size }
  619. result:=cg.gettempregister(list);
  620. end;
  621. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  622. begin
  623. case def2regtyp(size) of
  624. R_INTREGISTER:
  625. result:=getintregister(list,size);
  626. R_ADDRESSREGISTER:
  627. result:=getaddressregister(list,size);
  628. R_FPUREGISTER:
  629. result:=getfpuregister(list,size);
  630. R_MMREGISTER:
  631. result:=getmmregister(list,size);
  632. else
  633. internalerror(2010122901);
  634. end;
  635. end;
  636. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  637. begin
  638. result:=cg.uses_registers(rt);
  639. end;
  640. procedure thlcgobj.getcpuregister(list: TAsmList; r: Tregister);
  641. begin
  642. cg.getcpuregister(list,r);
  643. end;
  644. procedure thlcgobj.ungetcpuregister(list: TAsmList; r: Tregister);
  645. begin
  646. cg.ungetcpuregister(list,r);
  647. end;
  648. procedure thlcgobj.alloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  649. begin
  650. cg.alloccpuregisters(list,rt,r);
  651. end;
  652. procedure thlcgobj.dealloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  653. begin
  654. cg.dealloccpuregisters(list,rt,r);
  655. end;
  656. procedure thlcgobj.allocallcpuregisters(list: TAsmList);
  657. begin
  658. cg.allocallcpuregisters(list);
  659. end;
  660. procedure thlcgobj.deallocallcpuregisters(list: TAsmList);
  661. begin
  662. cg.deallocallcpuregisters(list);
  663. end;
  664. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  665. begin
  666. cg.do_register_allocation(list,headertai);
  667. end;
  668. procedure thlcgobj.translate_register(var reg: tregister);
  669. begin
  670. cg.translate_register(reg);
  671. end;
  672. class function thlcgobj.def2regtyp(def: tdef): tregistertype;
  673. begin
  674. case def.typ of
  675. enumdef,
  676. orddef,
  677. recorddef,
  678. setdef:
  679. result:=R_INTREGISTER;
  680. stringdef,
  681. pointerdef,
  682. classrefdef,
  683. objectdef,
  684. procvardef,
  685. procdef,
  686. arraydef,
  687. formaldef:
  688. result:=R_ADDRESSREGISTER;
  689. floatdef:
  690. if use_vectorfpu(def) then
  691. result:=R_MMREGISTER
  692. else if cs_fp_emulation in current_settings.moduleswitches then
  693. result:=R_INTREGISTER
  694. else
  695. result:=R_FPUREGISTER;
  696. filedef,
  697. variantdef,
  698. forwarddef,
  699. undefineddef:
  700. result:=R_INVALIDREGISTER;
  701. else
  702. internalerror(2010120506);
  703. end;
  704. end;
  705. procedure thlcgobj.reference_reset_base(var ref: treference; regsize: tdef;
  706. reg: tregister; offset, alignment: longint);
  707. begin
  708. reference_reset(ref,alignment);
  709. ref.base:=reg;
  710. ref.offset:=offset;
  711. end;
  712. procedure thlcgobj.temp_to_ref(p: ptemprecord; out ref: treference);
  713. begin
  714. reference_reset_base(ref,voidstackpointertype,current_procinfo.framepointer,p^.pos,p^.alignment);
  715. end;
  716. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  717. begin
  718. cg.a_label(list,l);
  719. end;
  720. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  721. begin
  722. cg.a_reg_alloc(list,r);
  723. end;
  724. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  725. begin
  726. cg.a_reg_dealloc(list,r);
  727. end;
  728. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  729. begin
  730. cg.a_reg_sync(list,r);
  731. end;
  732. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  733. var
  734. ref: treference;
  735. tmpreg : tregister;
  736. begin
  737. cgpara.check_simple_location;
  738. paramanager.alloccgpara(list,cgpara);
  739. if cgpara.location^.shiftval<0 then
  740. begin
  741. tmpreg:=getintregister(list,cgpara.location^.def);
  742. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.def,-cgpara.location^.shiftval,r,tmpreg);
  743. r:=tmpreg;
  744. end;
  745. case cgpara.location^.loc of
  746. LOC_REGISTER,LOC_CREGISTER:
  747. a_load_reg_reg(list,size,cgpara.location^.def,r,cgpara.location^.register);
  748. LOC_REFERENCE,LOC_CREFERENCE:
  749. begin
  750. reference_reset_base(ref,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  751. a_load_reg_ref(list,size,cgpara.location^.def,r,ref);
  752. end;
  753. LOC_MMREGISTER,LOC_CMMREGISTER:
  754. a_loadmm_intreg_reg(list,size,cgpara.location^.def,r,cgpara.location^.register,mms_movescalar);
  755. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  756. begin
  757. tg.gethltemp(list,size,size.size,tt_normal,ref);
  758. a_load_reg_ref(list,size,cgpara.location^.def,r,ref);
  759. a_loadfpu_ref_cgpara(list,cgpara.location^.def,ref,cgpara);
  760. tg.ungettemp(list,ref);
  761. end
  762. else
  763. internalerror(2010120415);
  764. end;
  765. end;
  766. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  767. var
  768. ref : treference;
  769. begin
  770. cgpara.check_simple_location;
  771. paramanager.alloccgpara(list,cgpara);
  772. if cgpara.location^.shiftval<0 then
  773. a:=a shl -cgpara.location^.shiftval;
  774. case cgpara.location^.loc of
  775. LOC_REGISTER,LOC_CREGISTER:
  776. a_load_const_reg(list,cgpara.location^.def,a,cgpara.location^.register);
  777. LOC_REFERENCE,LOC_CREFERENCE:
  778. begin
  779. reference_reset_base(ref,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  780. a_load_const_ref(list,cgpara.location^.def,a,ref);
  781. end
  782. else
  783. internalerror(2010120416);
  784. end;
  785. end;
  786. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  787. var
  788. tmpref, ref: treference;
  789. tmpreg: tregister;
  790. location: pcgparalocation;
  791. orgsizeleft,
  792. sizeleft: tcgint;
  793. reghasvalue: boolean;
  794. begin
  795. location:=cgpara.location;
  796. tmpref:=r;
  797. sizeleft:=cgpara.intsize;
  798. while assigned(location) do
  799. begin
  800. paramanager.allocparaloc(list,location);
  801. case location^.loc of
  802. LOC_REGISTER,LOC_CREGISTER:
  803. begin
  804. { Parameter locations are often allocated in multiples of
  805. entire registers. If a parameter only occupies a part of
  806. such a register (e.g. a 16 bit int on a 32 bit
  807. architecture), the size of this parameter can only be
  808. determined by looking at the "size" parameter of this
  809. method -> if the size parameter is <= sizeof(aint), then
  810. we check that there is only one parameter location and
  811. then use this "size" to load the value into the parameter
  812. location }
  813. if (def_cgsize(size)<>OS_NO) and
  814. (size.size<=sizeof(aint)) then
  815. begin
  816. cgpara.check_simple_location;
  817. a_load_ref_reg(list,size,location^.def,tmpref,location^.register);
  818. if location^.shiftval<0 then
  819. a_op_const_reg(list,OP_SHL,location^.def,-location^.shiftval,location^.register);
  820. end
  821. { there's a lot more data left, and the current paraloc's
  822. register is entirely filled with part of that data }
  823. else if (sizeleft>sizeof(aint)) then
  824. begin
  825. a_load_ref_reg(list,location^.def,location^.def,tmpref,location^.register);
  826. end
  827. { we're at the end of the data, and it can be loaded into
  828. the current location's register with a single regular
  829. load }
  830. else if sizeleft in [1,2,4,8] then
  831. begin
  832. { don't use cgsize_orddef(int_cgsize(sizeleft)) as fromdef,
  833. because that may be larger than location^.register in
  834. case of padding at the end of a record }
  835. a_load_ref_reg(list,location^.def,location^.def,tmpref,location^.register);
  836. if location^.shiftval<0 then
  837. a_op_const_reg(list,OP_SHL,location^.def,-location^.shiftval,location^.register);
  838. end
  839. { we're at the end of the data, and we need multiple loads
  840. to get it in the register because it's an irregular size }
  841. else
  842. begin
  843. { should be the last part }
  844. if assigned(location^.next) then
  845. internalerror(2010052907);
  846. { load the value piecewise to get it into the register }
  847. orgsizeleft:=sizeleft;
  848. reghasvalue:=false;
  849. {$ifdef cpu64bitalu}
  850. if sizeleft>=4 then
  851. begin
  852. a_load_ref_reg(list,u32inttype,location^.def,tmpref,location^.register);
  853. dec(sizeleft,4);
  854. if target_info.endian=endian_big then
  855. a_op_const_reg(list,OP_SHL,location^.def,sizeleft*8,location^.register);
  856. inc(tmpref.offset,4);
  857. reghasvalue:=true;
  858. end;
  859. {$endif cpu64bitalu}
  860. if sizeleft>=2 then
  861. begin
  862. tmpreg:=getintregister(list,location^.def);
  863. a_load_ref_reg(list,u16inttype,location^.def,tmpref,tmpreg);
  864. dec(sizeleft,2);
  865. if reghasvalue then
  866. begin
  867. if target_info.endian=endian_big then
  868. a_op_const_reg(list,OP_SHL,location^.def,sizeleft*8,tmpreg)
  869. else
  870. a_op_const_reg(list,OP_SHL,location^.def,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  871. a_op_reg_reg(list,OP_OR,location^.def,tmpreg,location^.register);
  872. end
  873. else
  874. begin
  875. if target_info.endian=endian_big then
  876. a_op_const_reg_reg(list,OP_SHL,location^.def,sizeleft*8,tmpreg,location^.register)
  877. else
  878. a_load_reg_reg(list,location^.def,location^.def,tmpreg,location^.register);
  879. end;
  880. inc(tmpref.offset,2);
  881. reghasvalue:=true;
  882. end;
  883. if sizeleft=1 then
  884. begin
  885. tmpreg:=getintregister(list,location^.def);
  886. a_load_ref_reg(list,u8inttype,location^.def,tmpref,tmpreg);
  887. dec(sizeleft,1);
  888. if reghasvalue then
  889. begin
  890. if target_info.endian=endian_little then
  891. a_op_const_reg(list,OP_SHL,location^.def,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  892. a_op_reg_reg(list,OP_OR,location^.def,tmpreg,location^.register)
  893. end
  894. else
  895. a_load_reg_reg(list,location^.def,location^.def,tmpreg,location^.register);
  896. inc(tmpref.offset);
  897. end;
  898. if location^.shiftval<0 then
  899. a_op_const_reg(list,OP_SHL,location^.def,-location^.shiftval,location^.register);
  900. { the loop will already adjust the offset and sizeleft }
  901. dec(tmpref.offset,orgsizeleft);
  902. sizeleft:=orgsizeleft;
  903. end;
  904. end;
  905. LOC_REFERENCE,LOC_CREFERENCE:
  906. begin
  907. if assigned(location^.next) then
  908. internalerror(2010052906);
  909. reference_reset_base(ref,voidstackpointertype,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  910. if (def_cgsize(size)<>OS_NO) and
  911. (size.size=sizeleft) and
  912. (sizeleft<=sizeof(aint)) then
  913. a_load_ref_ref(list,size,location^.def,tmpref,ref)
  914. else
  915. { use concatcopy, because the parameter can be larger than }
  916. { what the OS_* constants can handle }
  917. g_concatcopy(list,location^.def,tmpref,ref);
  918. end;
  919. LOC_MMREGISTER,LOC_CMMREGISTER:
  920. begin
  921. case location^.size of
  922. OS_F32,
  923. OS_F64,
  924. OS_F128:
  925. a_loadmm_ref_reg(list,location^.def,location^.def,tmpref,location^.register,mms_movescalar);
  926. OS_M8..OS_M128,
  927. OS_MS8..OS_MS128:
  928. a_loadmm_ref_reg(list,location^.def,location^.def,tmpref,location^.register,nil);
  929. else
  930. internalerror(2010053101);
  931. end;
  932. end
  933. else
  934. internalerror(2014032101);
  935. end;
  936. inc(tmpref.offset,tcgsize2size[location^.size]);
  937. dec(sizeleft,tcgsize2size[location^.size]);
  938. location:=location^.next;
  939. end;
  940. end;
  941. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  942. begin
  943. case l.loc of
  944. LOC_REGISTER,
  945. LOC_CREGISTER :
  946. a_load_reg_cgpara(list,size,l.register,cgpara);
  947. LOC_CONSTANT :
  948. a_load_const_cgpara(list,size,l.value,cgpara);
  949. LOC_CREFERENCE,
  950. LOC_REFERENCE :
  951. a_load_ref_cgpara(list,size,l.reference,cgpara);
  952. else
  953. internalerror(2010120419);
  954. end;
  955. end;
  956. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize: tdef; const r: treference; const cgpara: TCGPara);
  957. var
  958. hr : tregister;
  959. begin
  960. cgpara.check_simple_location;
  961. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  962. begin
  963. paramanager.allocparaloc(list,cgpara.location);
  964. a_loadaddr_ref_reg(list,fromsize,cgpara.location^.def,r,cgpara.location^.register)
  965. end
  966. else
  967. begin
  968. hr:=getaddressregister(list,cgpara.def);
  969. a_loadaddr_ref_reg(list,fromsize,cgpara.location^.def,r,hr);
  970. a_load_reg_cgpara(list,cgpara.def,hr,cgpara);
  971. end;
  972. end;
  973. function thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  974. begin
  975. result:=a_call_name(list,pd,s,paras,forceresdef,false);
  976. end;
  977. function thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara): tcgpara;
  978. begin
  979. result:=a_call_name(list,pd,s,paras,nil,false);
  980. end;
  981. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  982. var
  983. tmpreg: tregister;
  984. begin
  985. tmpreg:=getintregister(list,tosize);
  986. a_load_const_reg(list,tosize,a,tmpreg);
  987. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  988. end;
  989. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: tcgint; const loc: tlocation);
  990. begin
  991. case loc.loc of
  992. LOC_REFERENCE,LOC_CREFERENCE:
  993. a_load_const_ref(list,tosize,a,loc.reference);
  994. LOC_REGISTER,LOC_CREGISTER:
  995. a_load_const_reg(list,tosize,a,loc.register);
  996. LOC_SUBSETREG,LOC_CSUBSETREG:
  997. a_load_const_subsetreg(list,tosize,a,loc.sreg);
  998. LOC_SUBSETREF,LOC_CSUBSETREF:
  999. a_load_const_subsetref(list,tosize,a,loc.sref);
  1000. else
  1001. internalerror(2010120401);
  1002. end;
  1003. end;
  1004. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  1005. begin
  1006. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1007. end;
  1008. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  1009. begin
  1010. case loc.loc of
  1011. LOC_REFERENCE,LOC_CREFERENCE:
  1012. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  1013. LOC_REGISTER,LOC_CREGISTER:
  1014. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  1015. LOC_SUBSETREG,LOC_CSUBSETREG:
  1016. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  1017. LOC_SUBSETREF,LOC_CSUBSETREF:
  1018. a_load_reg_subsetref(list,fromsize,tosize,reg,loc.sref);
  1019. LOC_MMREGISTER,LOC_CMMREGISTER:
  1020. a_loadmm_intreg_reg(list,fromsize,tosize,reg,loc.register,mms_movescalar);
  1021. else
  1022. internalerror(2010120402);
  1023. end;
  1024. end;
  1025. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  1026. begin
  1027. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1028. end;
  1029. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1030. var
  1031. tmpreg: tregister;
  1032. begin
  1033. { verify if we have the same reference }
  1034. if references_equal(sref,dref) then
  1035. exit;
  1036. tmpreg:=getintregister(list,tosize);
  1037. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1038. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1039. end;
  1040. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  1041. begin
  1042. case loc.loc of
  1043. LOC_REFERENCE,LOC_CREFERENCE:
  1044. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  1045. LOC_REGISTER,LOC_CREGISTER:
  1046. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  1047. LOC_CONSTANT:
  1048. a_load_const_reg(list,tosize,loc.value,reg);
  1049. LOC_SUBSETREG,LOC_CSUBSETREG:
  1050. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  1051. LOC_SUBSETREF,LOC_CSUBSETREF:
  1052. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  1053. else
  1054. internalerror(2010120201);
  1055. end;
  1056. end;
  1057. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  1058. begin
  1059. case loc.loc of
  1060. LOC_REFERENCE,LOC_CREFERENCE:
  1061. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  1062. LOC_REGISTER,LOC_CREGISTER:
  1063. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  1064. LOC_CONSTANT:
  1065. a_load_const_ref(list,tosize,loc.value,ref);
  1066. LOC_SUBSETREG,LOC_CSUBSETREG:
  1067. a_load_subsetreg_ref(list,fromsize,tosize,loc.sreg,ref);
  1068. LOC_SUBSETREF,LOC_CSUBSETREF:
  1069. a_load_subsetref_ref(list,fromsize,tosize,loc.sref,ref);
  1070. else
  1071. internalerror(2010120403);
  1072. end;
  1073. end;
  1074. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  1075. begin
  1076. case loc.loc of
  1077. LOC_REFERENCE,LOC_CREFERENCE:
  1078. a_load_ref_subsetreg(list,fromsize,tosubsetsize,loc.reference,sreg);
  1079. LOC_REGISTER,LOC_CREGISTER:
  1080. a_load_reg_subsetreg(list,fromsize,tosubsetsize,loc.register,sreg);
  1081. LOC_CONSTANT:
  1082. a_load_const_subsetreg(list,tosubsetsize,loc.value,sreg);
  1083. LOC_SUBSETREG,LOC_CSUBSETREG:
  1084. a_load_subsetreg_subsetreg(list,fromsize,tosubsetsize,loc.sreg,sreg);
  1085. LOC_SUBSETREF,LOC_CSUBSETREF:
  1086. a_load_subsetref_subsetreg(list,fromsize,tosubsetsize,loc.sref,sreg);
  1087. else
  1088. internalerror(2010120404);
  1089. end;
  1090. end;
  1091. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  1092. begin
  1093. case loc.loc of
  1094. LOC_REFERENCE,LOC_CREFERENCE:
  1095. a_load_ref_subsetref(list,fromsize,tosubsetsize,loc.reference,sref);
  1096. LOC_REGISTER,LOC_CREGISTER:
  1097. a_load_reg_subsetref(list,fromsize,tosubsetsize,loc.register,sref);
  1098. LOC_CONSTANT:
  1099. a_load_const_subsetref(list,tosubsetsize,loc.value,sref);
  1100. LOC_SUBSETREG,LOC_CSUBSETREG:
  1101. a_load_subsetreg_subsetref(list,fromsize,tosubsetsize,loc.sreg,sref);
  1102. LOC_SUBSETREF,LOC_CSUBSETREF:
  1103. a_load_subsetref_subsetref(list,fromsize,tosubsetsize,loc.sref,sref);
  1104. else
  1105. internalerror(2010120405);
  1106. end;
  1107. end;
  1108. {$push}
  1109. {$r-,q-}
  1110. procedure thlcgobj.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  1111. var
  1112. subsetregdef: torddef;
  1113. bitmask: aword;
  1114. tmpreg,
  1115. subsetsizereg: tregister;
  1116. stopbit: byte;
  1117. begin
  1118. subsetregdef:=cgsize_orddef(sreg.subsetregsize);
  1119. tmpreg:=getintregister(list,subsetregdef);
  1120. if is_signed(subsetsize) then
  1121. begin
  1122. { sign extend in case the value has a bitsize mod 8 <> 0 }
  1123. { both instructions will be optimized away if not }
  1124. a_op_const_reg_reg(list,OP_SHL,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  1125. a_op_const_reg(list,OP_SAR,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  1126. end
  1127. else
  1128. begin
  1129. a_op_const_reg_reg(list,OP_SHR,subsetregdef,sreg.startbit,sreg.subsetreg,tmpreg);
  1130. stopbit:=sreg.startbit+sreg.bitlen;
  1131. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1132. // use aword to prevent overflow with 1 shl 31
  1133. if (stopbit-sreg.startbit<>AIntBits) then
  1134. bitmask:=(aword(1) shl (stopbit-sreg.startbit))-1
  1135. else
  1136. bitmask:=high(aword);
  1137. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),tmpreg);
  1138. end;
  1139. subsetsizereg:=getintregister(list,subsetsize);
  1140. a_load_reg_reg(list,subsetregdef,subsetsize,tmpreg,subsetsizereg);
  1141. a_load_reg_reg(list,subsetsize,tosize,subsetsizereg,destreg);
  1142. end;
  1143. procedure thlcgobj.a_load_reg_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister);
  1144. begin
  1145. a_load_regconst_subsetreg_intern(list,fromsize,tosubsetsize,fromreg,sreg,SL_REG);
  1146. end;
  1147. procedure thlcgobj.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  1148. var
  1149. fromsubsetregdef,
  1150. tosubsetregdef: torddef;
  1151. tmpreg, tmpreg2: tregister;
  1152. bitmask: aword;
  1153. stopbit: byte;
  1154. begin
  1155. if (fromsreg.bitlen>=tosreg.bitlen) then
  1156. begin
  1157. fromsubsetregdef:=cgsize_orddef(fromsreg.subsetregsize);
  1158. tosubsetregdef:=cgsize_orddef(tosreg.subsetregsize);
  1159. if (fromsreg.startbit<=tosreg.startbit) then
  1160. begin
  1161. { tosreg may be larger -> use its size to perform the shift }
  1162. tmpreg:=getintregister(list,tosubsetregdef);
  1163. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,fromsreg.subsetreg,tmpreg);
  1164. a_op_const_reg(list,OP_SHL,tosubsetregdef,tosreg.startbit-fromsreg.startbit,tmpreg)
  1165. end
  1166. else
  1167. begin
  1168. { fromsreg may be larger -> use its size to perform the shift }
  1169. tmpreg:=getintregister(list,fromsubsetregdef);
  1170. a_op_const_reg_reg(list,OP_SHR,fromsubsetregdef,fromsreg.startbit-tosreg.startbit,fromsreg.subsetreg,tmpreg);
  1171. tmpreg2:=getintregister(list,tosubsetregdef);
  1172. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,tmpreg,tmpreg2);
  1173. tmpreg:=tmpreg2;
  1174. end;
  1175. stopbit:=tosreg.startbit + tosreg.bitlen;
  1176. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1177. if (stopbit<>AIntBits) then
  1178. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  1179. else
  1180. bitmask:=(aword(1) shl tosreg.startbit) - 1;
  1181. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(bitmask),tosreg.subsetreg);
  1182. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(not(bitmask)),tmpreg);
  1183. a_op_reg_reg(list,OP_OR,tosubsetregdef,tmpreg,tosreg.subsetreg);
  1184. end
  1185. else
  1186. begin
  1187. tmpreg:=getintregister(list,tosubsetsize);
  1188. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1189. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1190. end;
  1191. end;
  1192. procedure thlcgobj.a_load_subsetreg_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference);
  1193. var
  1194. tmpreg: tregister;
  1195. begin
  1196. tmpreg:=getintregister(list,tosize);
  1197. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,tmpreg);
  1198. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1199. end;
  1200. procedure thlcgobj.a_load_ref_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister);
  1201. var
  1202. tmpreg: tregister;
  1203. begin
  1204. tmpreg := getintregister(list,tosubsetsize);
  1205. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1206. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,sreg);
  1207. end;
  1208. procedure thlcgobj.a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sreg: tsubsetregister);
  1209. var
  1210. subsetregdef: torddef;
  1211. bitmask: aword;
  1212. stopbit: byte;
  1213. begin
  1214. subsetregdef:=cgsize_orddef(sreg.subsetregsize);
  1215. stopbit:=sreg.startbit+sreg.bitlen;
  1216. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1217. if (stopbit<>AIntBits) then
  1218. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1219. else
  1220. bitmask:=(aword(1) shl sreg.startbit)-1;
  1221. if (((aword(a) shl sreg.startbit) and not bitmask)<>not bitmask) then
  1222. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1223. a_op_const_reg(list,OP_OR,subsetregdef,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  1224. end;
  1225. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  1226. begin
  1227. case loc.loc of
  1228. LOC_REFERENCE,LOC_CREFERENCE:
  1229. a_load_subsetreg_ref(list,fromsubsetsize,tosize,sreg,loc.reference);
  1230. LOC_REGISTER,LOC_CREGISTER:
  1231. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,loc.register);
  1232. LOC_SUBSETREG,LOC_CSUBSETREG:
  1233. a_load_subsetreg_subsetreg(list,fromsubsetsize,tosize,sreg,loc.sreg);
  1234. LOC_SUBSETREF,LOC_CSUBSETREF:
  1235. a_load_subsetreg_subsetref(list,fromsubsetsize,tosize,sreg,loc.sref);
  1236. else
  1237. internalerror(2010120406);
  1238. end;
  1239. end;
  1240. procedure thlcgobj.a_load_subsetref_reg(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister);
  1241. var
  1242. tmpref: treference;
  1243. valuereg,extra_value_reg: tregister;
  1244. tosreg: tsubsetregister;
  1245. loadsize: torddef;
  1246. loadbitsize: byte;
  1247. extra_load: boolean;
  1248. begin
  1249. get_subsetref_load_info(sref,loadsize,extra_load);
  1250. loadbitsize:=loadsize.size*8;
  1251. { load the (first part) of the bit sequence }
  1252. valuereg:=getintregister(list,osuinttype);
  1253. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1254. if not extra_load then
  1255. begin
  1256. { everything is guaranteed to be in a single register of loadsize }
  1257. if (sref.bitindexreg=NR_NO) then
  1258. begin
  1259. { use subsetreg routine, it may have been overridden with an optimized version }
  1260. tosreg.subsetreg:=valuereg;
  1261. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1262. { subsetregs always count bits from right to left }
  1263. if (target_info.endian=endian_big) then
  1264. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1265. else
  1266. tosreg.startbit:=sref.startbit;
  1267. tosreg.bitlen:=sref.bitlen;
  1268. a_load_subsetreg_reg(list,fromsubsetsize,tosize,tosreg,destreg);
  1269. exit;
  1270. end
  1271. else
  1272. begin
  1273. if (sref.startbit<>0) then
  1274. internalerror(2006081510);
  1275. if (target_info.endian=endian_big) then
  1276. begin
  1277. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1278. if is_signed(fromsubsetsize) then
  1279. begin
  1280. { sign extend to entire register }
  1281. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1282. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1283. end
  1284. else
  1285. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1286. end
  1287. else
  1288. begin
  1289. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1290. if is_signed(fromsubsetsize) then
  1291. begin
  1292. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1293. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1294. end
  1295. end;
  1296. { mask other bits/sign extend }
  1297. if not is_signed(fromsubsetsize) then
  1298. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1299. end
  1300. end
  1301. else
  1302. begin
  1303. { load next value as well }
  1304. extra_value_reg:=getintregister(list,osuinttype);
  1305. if (sref.bitindexreg=NR_NO) then
  1306. begin
  1307. tmpref:=sref.ref;
  1308. inc(tmpref.offset,loadbitsize div 8);
  1309. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1310. { can be overridden to optimize }
  1311. a_load_subsetref_regs_noindex(list,fromsubsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1312. end
  1313. else
  1314. begin
  1315. if (sref.startbit<>0) then
  1316. internalerror(2006080610);
  1317. a_load_subsetref_regs_index(list,fromsubsetsize,loadbitsize,sref,valuereg);
  1318. end;
  1319. end;
  1320. { store in destination }
  1321. {$ifndef cpuhighleveltarget}
  1322. { avoid unnecessary sign extension and zeroing }
  1323. valuereg:=cg.makeregsize(list,valuereg,OS_INT);
  1324. destreg:=cg.makeregsize(list,destreg,OS_INT);
  1325. cg.a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1326. destreg:=cg.makeregsize(list,destreg,def_cgsize(tosize));
  1327. {$else}
  1328. { can't juggle with register sizes, they are actually typed entities
  1329. here }
  1330. a_load_reg_reg(list,osuinttype,tosize,valuereg,destreg);
  1331. {$endif}
  1332. end;
  1333. procedure thlcgobj.a_load_reg_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference);
  1334. begin
  1335. a_load_regconst_subsetref_intern(list,fromsize,tosubsetsize,fromreg,sref,SL_REG);
  1336. end;
  1337. procedure thlcgobj.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref, tosref: tsubsetreference);
  1338. var
  1339. tmpreg: tregister;
  1340. begin
  1341. tmpreg:=getintregister(list,tosubsetsize);
  1342. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1343. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1344. end;
  1345. procedure thlcgobj.a_load_subsetref_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference);
  1346. var
  1347. tmpreg: tregister;
  1348. begin
  1349. tmpreg:=getintregister(list,tosize);
  1350. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,tmpreg);
  1351. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1352. end;
  1353. procedure thlcgobj.a_load_ref_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference);
  1354. var
  1355. tmpreg: tregister;
  1356. begin
  1357. tmpreg := getintregister(list,tosubsetsize);
  1358. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1359. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,sref);
  1360. end;
  1361. procedure thlcgobj.a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  1362. var
  1363. tmpreg: tregister;
  1364. slopt: tsubsetloadopt;
  1365. begin
  1366. { perform masking of the source value in advance }
  1367. slopt:=SL_REGNOSRCMASK;
  1368. if (sref.bitlen<>AIntBits) then
  1369. a:=tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1370. if (
  1371. { broken x86 "x shl regbitsize = x" }
  1372. ((sref.bitlen<>AIntBits) and
  1373. ((aword(a) and ((aword(1) shl sref.bitlen)-1))=(aword(1) shl sref.bitlen)-1)) or
  1374. ((sref.bitlen=AIntBits) and
  1375. (a=-1))
  1376. ) then
  1377. slopt:=SL_SETMAX
  1378. else if (a=0) then
  1379. slopt:=SL_SETZERO;
  1380. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1381. begin
  1382. tmpreg:=getintregister(list,tosubsetsize);
  1383. a_load_const_reg(list,tosubsetsize,a,tmpreg);
  1384. end
  1385. else
  1386. tmpreg:=NR_NO;
  1387. a_load_regconst_subsetref_intern(list,tosubsetsize,tosubsetsize,tmpreg,sref,slopt);
  1388. end;
  1389. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  1390. begin
  1391. case loc.loc of
  1392. LOC_REFERENCE,LOC_CREFERENCE:
  1393. a_load_subsetref_ref(list,fromsubsetsize,tosize,sref,loc.reference);
  1394. LOC_REGISTER,LOC_CREGISTER:
  1395. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,loc.register);
  1396. LOC_SUBSETREG,LOC_CSUBSETREG:
  1397. a_load_subsetref_subsetreg(list,fromsubsetsize,tosize,sref,loc.sreg);
  1398. LOC_SUBSETREF,LOC_CSUBSETREF:
  1399. a_load_subsetref_subsetref(list,fromsubsetsize,tosize,sref,loc.sref);
  1400. else
  1401. internalerror(2010120407);
  1402. end;
  1403. end;
  1404. procedure thlcgobj.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1405. var
  1406. tmpreg: tregister;
  1407. begin
  1408. tmpreg:=getintregister(list,tosubsetsize);
  1409. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1410. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1411. end;
  1412. procedure thlcgobj.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1413. var
  1414. tmpreg: tregister;
  1415. begin
  1416. tmpreg := getintregister(list,tosubsetsize);
  1417. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1418. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1419. end;
  1420. procedure thlcgobj.a_bit_test_reg_reg_reg(list: TAsmList; bitnumbersize, valuesize, destsize: tdef; bitnumber, value, destreg: tregister);
  1421. var
  1422. tmpvalue: tregister;
  1423. begin
  1424. tmpvalue:=getintregister(list,valuesize);
  1425. if (target_info.endian=endian_little) then
  1426. begin
  1427. { rotate value register "bitnumber" bits to the right }
  1428. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1429. { extract the bit we want }
  1430. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1431. end
  1432. else
  1433. begin
  1434. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1435. { bit in uppermost position, then move it to the lowest position }
  1436. { "and" is not necessary since combination of shl/shr will clear }
  1437. { all other bits }
  1438. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1439. a_op_const_reg(list,OP_SHR,valuesize,valuesize.size*8-1,tmpvalue);
  1440. end;
  1441. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1442. end;
  1443. procedure thlcgobj.a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister);
  1444. var
  1445. href: treference;
  1446. begin
  1447. href:=ref;
  1448. g_ptrtypecast_ref(list,cpointerdef.getreusable(fromsize),cpointerdef.getreusable(u8inttype),href);
  1449. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_const_ref_sref(bitnumber,fromsize,href),destreg);
  1450. end;
  1451. procedure thlcgobj.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister);
  1452. begin
  1453. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1454. end;
  1455. procedure thlcgobj.a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister);
  1456. var
  1457. tmpsreg: tsubsetregister;
  1458. begin
  1459. { the first parameter is used to calculate the bit offset in }
  1460. { case of big endian, and therefore must be the size of the }
  1461. { set and not of the whole subsetreg }
  1462. tmpsreg:=get_bit_const_reg_sreg(fromsubsetsize,bitnumber,setreg.subsetreg);
  1463. { now fix the size of the subsetreg }
  1464. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1465. { correct offset of the set in the subsetreg }
  1466. inc(tmpsreg.startbit,setreg.startbit);
  1467. a_load_subsetreg_reg(list,fromsubsetsize,destsize,tmpsreg,destreg);
  1468. end;
  1469. procedure thlcgobj.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister);
  1470. var
  1471. href: treference;
  1472. begin
  1473. href:=ref;
  1474. g_ptrtypecast_ref(list,cpointerdef.getreusable(refsize),cpointerdef.getreusable(u8inttype),href);
  1475. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_reg_ref_sref(list,bitnumbersize,refsize,bitnumber,href),destreg);
  1476. end;
  1477. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1478. var
  1479. tmpreg: tregister;
  1480. begin
  1481. case loc.loc of
  1482. LOC_REFERENCE,LOC_CREFERENCE:
  1483. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  1484. LOC_REGISTER,LOC_CREGISTER,
  1485. LOC_SUBSETREG,LOC_CSUBSETREG,
  1486. LOC_CONSTANT:
  1487. begin
  1488. case loc.loc of
  1489. LOC_REGISTER,LOC_CREGISTER:
  1490. tmpreg:=loc.register;
  1491. LOC_SUBSETREG,LOC_CSUBSETREG:
  1492. begin
  1493. tmpreg:=getintregister(list,locsize);
  1494. a_load_subsetreg_reg(list,locsize,locsize,loc.sreg,tmpreg);
  1495. end;
  1496. LOC_CONSTANT:
  1497. begin
  1498. tmpreg:=getintregister(list,locsize);
  1499. a_load_const_reg(list,locsize,loc.value,tmpreg);
  1500. end;
  1501. else
  1502. internalerror(2013112909);
  1503. end;
  1504. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  1505. end;
  1506. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1507. else
  1508. internalerror(2010120411);
  1509. end;
  1510. end;
  1511. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  1512. begin
  1513. case loc.loc of
  1514. LOC_REFERENCE,LOC_CREFERENCE:
  1515. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  1516. LOC_REGISTER,LOC_CREGISTER:
  1517. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  1518. LOC_SUBSETREG,LOC_CSUBSETREG:
  1519. a_bit_test_const_subsetreg_reg(list,locsize,destsize,bitnumber,loc.sreg,destreg);
  1520. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1521. else
  1522. internalerror(2010120410);
  1523. end;
  1524. end;
  1525. procedure thlcgobj.a_bit_set_reg_reg(list: TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber, dest: tregister);
  1526. var
  1527. tmpvalue: tregister;
  1528. begin
  1529. tmpvalue:=getintregister(list,destsize);
  1530. if (target_info.endian=endian_little) then
  1531. begin
  1532. a_load_const_reg(list,destsize,1,tmpvalue);
  1533. { rotate bit "bitnumber" bits to the left }
  1534. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1535. end
  1536. else
  1537. begin
  1538. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  1539. { shr bitnumber" results in correct mask }
  1540. a_load_const_reg(list,destsize,1 shl (destsize.size*8-1),tmpvalue);
  1541. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  1542. end;
  1543. { set/clear the bit we want }
  1544. if doset then
  1545. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  1546. else
  1547. begin
  1548. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  1549. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  1550. end;
  1551. end;
  1552. procedure thlcgobj.a_bit_set_const_ref(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: tcgint; const ref: treference);
  1553. var
  1554. href: treference;
  1555. begin
  1556. href:=ref;
  1557. g_ptrtypecast_ref(list,cpointerdef.getreusable(destsize),cpointerdef.getreusable(u8inttype),href);
  1558. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_const_ref_sref(bitnumber,destsize,href));
  1559. end;
  1560. procedure thlcgobj.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: tcgint; destreg: tregister);
  1561. begin
  1562. g_ptrtypecast_reg(list,cpointerdef.getreusable(destsize),cpointerdef.getreusable(u8inttype),destreg);
  1563. a_load_const_subsetreg(list,u8inttype,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  1564. end;
  1565. procedure thlcgobj.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: tcgint; const destreg: tsubsetregister);
  1566. var
  1567. tmpsreg: tsubsetregister;
  1568. begin
  1569. { the first parameter is used to calculate the bit offset in }
  1570. { case of big endian, and therefore must be the size of the }
  1571. { set and not of the whole subsetreg }
  1572. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  1573. { now fix the size of the subsetreg }
  1574. tmpsreg.subsetregsize:=destreg.subsetregsize;
  1575. { correct offset of the set in the subsetreg }
  1576. inc(tmpsreg.startbit,destreg.startbit);
  1577. a_load_const_subsetreg(list,u8inttype,ord(doset),tmpsreg);
  1578. end;
  1579. procedure thlcgobj.a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference);
  1580. var
  1581. href: treference;
  1582. begin
  1583. href:=ref;
  1584. g_ptrtypecast_ref(list,cpointerdef.getreusable(tosize),cpointerdef.getreusable(u8inttype),href);
  1585. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_reg_ref_sref(list,fromsize,tosize,bitnumber,href));
  1586. end;
  1587. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  1588. var
  1589. tmpreg: tregister;
  1590. begin
  1591. case loc.loc of
  1592. LOC_REFERENCE:
  1593. a_bit_set_reg_ref(list,doset,regsize,tosize,bitnumber,loc.reference);
  1594. LOC_CREGISTER:
  1595. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,loc.register);
  1596. { e.g. a 2-byte set in a record regvar }
  1597. LOC_CSUBSETREG:
  1598. begin
  1599. { hard to do in-place in a generic way, so operate on a copy }
  1600. tmpreg:=getintregister(list,tosize);
  1601. a_load_subsetreg_reg(list,tosize,tosize,loc.sreg,tmpreg);
  1602. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,tmpreg);
  1603. a_load_reg_subsetreg(list,tosize,tosize,tmpreg,loc.sreg);
  1604. end;
  1605. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1606. else
  1607. internalerror(2010120408)
  1608. end;
  1609. end;
  1610. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: tcgint; const loc: tlocation);
  1611. begin
  1612. case loc.loc of
  1613. LOC_REFERENCE:
  1614. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  1615. LOC_CREGISTER:
  1616. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  1617. LOC_CSUBSETREG:
  1618. a_bit_set_const_subsetreg(list,doset,tosize,bitnumber,loc.sreg);
  1619. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1620. else
  1621. internalerror(2010120409)
  1622. end;
  1623. end;
  1624. function thlcgobj.get_call_result_cgpara(pd: tabstractprocdef; forceresdef: tdef): tcgpara;
  1625. begin
  1626. if not assigned(forceresdef) then
  1627. begin
  1628. pd.init_paraloc_info(callerside);
  1629. result:=pd.funcretloc[callerside];
  1630. end
  1631. else
  1632. result:=paramanager.get_funcretloc(pd,callerside,forceresdef);
  1633. end;
  1634. (*
  1635. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1636. in memory. They are like a regular reference, but contain an extra bit
  1637. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1638. and a bit length (always constant).
  1639. Bit packed values are stored differently in memory depending on whether we
  1640. are on a big or a little endian system (compatible with at least GPC). The
  1641. size of the basic working unit is always the smallest power-of-2 byte size
  1642. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1643. bytes, 17..32 bits -> 4 bytes etc).
  1644. On a big endian, 5-bit: values are stored like this:
  1645. 11111222 22333334 44445555 56666677 77788888
  1646. The leftmost bit of each 5-bit value corresponds to the most significant
  1647. bit.
  1648. On little endian, it goes like this:
  1649. 22211111 43333322 55554444 77666665 88888777
  1650. In this case, per byte the left-most bit is more significant than those on
  1651. the right, but the bits in the next byte are all more significant than
  1652. those in the previous byte (e.g., the 222 in the first byte are the low
  1653. three bits of that value, while the 22 in the second byte are the upper
  1654. two bits.
  1655. Big endian, 9 bit values:
  1656. 11111111 12222222 22333333 33344444 ...
  1657. Little endian, 9 bit values:
  1658. 11111111 22222221 33333322 44444333 ...
  1659. This is memory representation and the 16 bit values are byteswapped.
  1660. Similarly as in the previous case, the 2222222 string contains the lower
  1661. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1662. registers (two 16 bit registers in the current implementation, although a
  1663. single 32 bit register would be possible too, in particular if 32 bit
  1664. alignment can be guaranteed), this becomes:
  1665. 22222221 11111111 44444333 33333322 ...
  1666. (l)ow u l l u l u
  1667. The startbit/bitindex in a subsetreference always refers to
  1668. a) on big endian: the most significant bit of the value
  1669. (bits counted from left to right, both memory an registers)
  1670. b) on little endian: the least significant bit when the value
  1671. is loaded in a register (bit counted from right to left)
  1672. Although a) results in more complex code for big endian systems, it's
  1673. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1674. Apple's universal interfaces which depend on these layout differences).
  1675. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1676. make sure the appropriate alignment is guaranteed, at least in case of
  1677. {$defined cpurequiresproperalignment}.
  1678. *)
  1679. procedure thlcgobj.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  1680. var
  1681. intloadsize: tcgint;
  1682. begin
  1683. intloadsize:=packedbitsloadsize(sref.bitlen);
  1684. if (intloadsize=0) then
  1685. internalerror(2006081310);
  1686. if (intloadsize>sizeof(aint)) then
  1687. intloadsize:=sizeof(aint);
  1688. loadsize:=cgsize_orddef(int_cgsize(intloadsize));
  1689. if (sref.bitlen>sizeof(aint)*8) then
  1690. internalerror(2006081312);
  1691. extra_load:=
  1692. (sref.bitlen<>1) and
  1693. ((sref.bitindexreg<>NR_NO) or
  1694. (byte(sref.startbit+sref.bitlen)>byte(intloadsize*8)));
  1695. end;
  1696. procedure thlcgobj.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1697. var
  1698. restbits: byte;
  1699. begin
  1700. if (target_info.endian=endian_big) then
  1701. begin
  1702. { valuereg contains the upper bits, extra_value_reg the lower }
  1703. restbits:=(sref.bitlen-(loadbitsize-sref.startbit));
  1704. if is_signed(subsetsize) then
  1705. begin
  1706. { sign extend }
  1707. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize+sref.startbit,valuereg);
  1708. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1709. end
  1710. else
  1711. begin
  1712. a_op_const_reg(list,OP_SHL,osuinttype,restbits,valuereg);
  1713. { mask other bits }
  1714. if (sref.bitlen<>AIntBits) then
  1715. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1716. end;
  1717. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-restbits,extra_value_reg)
  1718. end
  1719. else
  1720. begin
  1721. { valuereg contains the lower bits, extra_value_reg the upper }
  1722. a_op_const_reg(list,OP_SHR,osuinttype,sref.startbit,valuereg);
  1723. if is_signed(subsetsize) then
  1724. begin
  1725. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1726. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,extra_value_reg);
  1727. end
  1728. else
  1729. begin
  1730. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.startbit,extra_value_reg);
  1731. { mask other bits }
  1732. if (sref.bitlen <> AIntBits) then
  1733. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1734. end;
  1735. end;
  1736. { merge }
  1737. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1738. end;
  1739. procedure thlcgobj.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1740. var
  1741. hl: tasmlabel;
  1742. tmpref: treference;
  1743. extra_value_reg,
  1744. tmpreg: tregister;
  1745. begin
  1746. tmpreg:=getintregister(list,osuinttype);
  1747. tmpref:=sref.ref;
  1748. inc(tmpref.offset,loadbitsize div 8);
  1749. extra_value_reg:=getintregister(list,osuinttype);
  1750. if (target_info.endian=endian_big) then
  1751. begin
  1752. { since this is a dynamic index, it's possible that the value }
  1753. { is entirely in valuereg. }
  1754. { get the data in valuereg in the right place }
  1755. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1756. if is_signed(subsetsize) then
  1757. begin
  1758. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1759. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg)
  1760. end
  1761. else
  1762. begin
  1763. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1764. if (loadbitsize<>AIntBits) then
  1765. { mask left over bits }
  1766. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1767. end;
  1768. tmpreg := getintregister(list,osuinttype);
  1769. { ensure we don't load anything past the end of the array }
  1770. current_asmdata.getjumplabel(hl);
  1771. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1772. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1773. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1774. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1775. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1776. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1777. { load next "loadbitsize" bits of the array }
  1778. a_load_ref_reg(list,cgsize_orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1779. a_op_reg_reg(list,OP_SHR,osuinttype,tmpreg,extra_value_reg);
  1780. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1781. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1782. { => extra_value_reg is now 0 }
  1783. { merge }
  1784. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1785. { no need to mask, necessary masking happened earlier on }
  1786. a_label(list,hl);
  1787. end
  1788. else
  1789. begin
  1790. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1791. { ensure we don't load anything past the end of the array }
  1792. current_asmdata.getjumplabel(hl);
  1793. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1794. { Y-x = -(Y-x) }
  1795. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1796. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1797. { load next "loadbitsize" bits of the array }
  1798. a_load_ref_reg(list,cgsize_orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1799. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1800. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1801. { merge }
  1802. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1803. a_label(list,hl);
  1804. { sign extend or mask other bits }
  1805. if is_signed(subsetsize) then
  1806. begin
  1807. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1808. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1809. end
  1810. else
  1811. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1812. end;
  1813. end;
  1814. procedure thlcgobj.a_load_regconst_subsetref_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1815. var
  1816. hl: tasmlabel;
  1817. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1818. tosreg, fromsreg: tsubsetregister;
  1819. tmpref: treference;
  1820. bitmask: aword;
  1821. loadsize: torddef;
  1822. loadbitsize: byte;
  1823. extra_load: boolean;
  1824. begin
  1825. { the register must be able to contain the requested value }
  1826. if (fromsize.size*8<sref.bitlen) then
  1827. internalerror(2006081613);
  1828. get_subsetref_load_info(sref,loadsize,extra_load);
  1829. loadbitsize:=loadsize.size*8;
  1830. { load the (first part) of the bit sequence }
  1831. valuereg:=getintregister(list,osuinttype);
  1832. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1833. { constant offset of bit sequence? }
  1834. if not extra_load then
  1835. begin
  1836. if (sref.bitindexreg=NR_NO) then
  1837. begin
  1838. { use subsetreg routine, it may have been overridden with an optimized version }
  1839. tosreg.subsetreg:=valuereg;
  1840. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1841. { subsetregs always count bits from right to left }
  1842. if (target_info.endian=endian_big) then
  1843. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1844. else
  1845. tosreg.startbit:=sref.startbit;
  1846. tosreg.bitlen:=sref.bitlen;
  1847. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1848. end
  1849. else
  1850. begin
  1851. if (sref.startbit<>0) then
  1852. internalerror(2006081710);
  1853. { should be handled by normal code and will give wrong result }
  1854. { on x86 for the '1 shl bitlen' below }
  1855. if (sref.bitlen=AIntBits) then
  1856. internalerror(2006081711);
  1857. { zero the bits we have to insert }
  1858. if (slopt<>SL_SETMAX) then
  1859. begin
  1860. maskreg:=getintregister(list,osuinttype);
  1861. if (target_info.endian = endian_big) then
  1862. begin
  1863. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1864. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1865. end
  1866. else
  1867. begin
  1868. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1869. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1870. end;
  1871. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1872. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1873. end;
  1874. { insert the value }
  1875. if (slopt<>SL_SETZERO) then
  1876. begin
  1877. tmpreg:=getintregister(list,osuinttype);
  1878. if (slopt<>SL_SETMAX) then
  1879. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1880. else if (sref.bitlen<>AIntBits) then
  1881. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1882. else
  1883. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1884. if (target_info.endian=endian_big) then
  1885. begin
  1886. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1887. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1888. begin
  1889. if (loadbitsize<>AIntBits) then
  1890. bitmask:=(((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1891. else
  1892. bitmask:=(high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1893. a_op_const_reg(list,OP_AND,osuinttype,bitmask,tmpreg);
  1894. end;
  1895. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1896. end
  1897. else
  1898. begin
  1899. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1900. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1901. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1902. end;
  1903. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1904. end;
  1905. end;
  1906. { store back to memory }
  1907. tmpreg:=getintregister(list,loadsize);
  1908. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1909. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1910. exit;
  1911. end
  1912. else
  1913. begin
  1914. { load next value }
  1915. extra_value_reg:=getintregister(list,osuinttype);
  1916. tmpref:=sref.ref;
  1917. inc(tmpref.offset,loadbitsize div 8);
  1918. { should maybe be taken out too, can be done more efficiently }
  1919. { on e.g. i386 with shld/shrd }
  1920. if (sref.bitindexreg = NR_NO) then
  1921. begin
  1922. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1923. fromsreg.subsetreg:=fromreg;
  1924. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1925. tosreg.subsetreg:=valuereg;
  1926. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1927. { transfer first part }
  1928. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1929. tosreg.bitlen:=fromsreg.bitlen;
  1930. if (target_info.endian=endian_big) then
  1931. begin
  1932. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1933. { upper bits of the value ... }
  1934. fromsreg.startbit:=sref.bitlen-(loadbitsize-sref.startbit);
  1935. { ... to bit 0 }
  1936. tosreg.startbit:=0
  1937. end
  1938. else
  1939. begin
  1940. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1941. { lower bits of the value ... }
  1942. fromsreg.startbit:=0;
  1943. { ... to startbit }
  1944. tosreg.startbit:=sref.startbit;
  1945. end;
  1946. case slopt of
  1947. SL_SETZERO,
  1948. SL_SETMAX:
  1949. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1950. else
  1951. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1952. end;
  1953. {$ifndef cpuhighleveltarget}
  1954. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1955. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1956. {$else}
  1957. tmpreg:=getintregister(list,loadsize);
  1958. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1959. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1960. {$endif}
  1961. { transfer second part }
  1962. if (target_info.endian = endian_big) then
  1963. begin
  1964. { extra_value_reg must contain the lower bits of the value at bits }
  1965. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1966. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1967. { - bitlen - startbit }
  1968. fromsreg.startbit:=0;
  1969. tosreg.startbit:=2*loadbitsize-sref.bitlen-sref.startbit
  1970. end
  1971. else
  1972. begin
  1973. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1974. fromsreg.startbit:=fromsreg.bitlen;
  1975. tosreg.startbit:=0;
  1976. end;
  1977. tosreg.subsetreg:=extra_value_reg;
  1978. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1979. tosreg.bitlen:=fromsreg.bitlen;
  1980. case slopt of
  1981. SL_SETZERO,
  1982. SL_SETMAX:
  1983. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1984. else
  1985. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1986. end;
  1987. tmpreg:=getintregister(list,loadsize);
  1988. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1989. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1990. exit;
  1991. end
  1992. else
  1993. begin
  1994. if (sref.startbit <> 0) then
  1995. internalerror(2006081812);
  1996. { should be handled by normal code and will give wrong result }
  1997. { on x86 for the '1 shl bitlen' below }
  1998. if (sref.bitlen = AIntBits) then
  1999. internalerror(2006081713);
  2000. { generate mask to zero the bits we have to insert }
  2001. if (slopt <> SL_SETMAX) then
  2002. begin
  2003. maskreg := getintregister(list,osuinttype);
  2004. if (target_info.endian = endian_big) then
  2005. begin
  2006. a_load_const_reg(list,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  2007. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  2008. end
  2009. else
  2010. begin
  2011. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  2012. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  2013. end;
  2014. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  2015. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  2016. end;
  2017. { insert the value }
  2018. if (slopt <> SL_SETZERO) then
  2019. begin
  2020. tmpreg := getintregister(list,osuinttype);
  2021. if (slopt <> SL_SETMAX) then
  2022. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  2023. else if (sref.bitlen <> AIntBits) then
  2024. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  2025. else
  2026. a_load_const_reg(list,osuinttype,-1,tmpreg);
  2027. if (target_info.endian = endian_big) then
  2028. begin
  2029. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  2030. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  2031. { mask left over bits }
  2032. a_op_const_reg(list,OP_AND,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  2033. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  2034. end
  2035. else
  2036. begin
  2037. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  2038. { mask left over bits }
  2039. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  2040. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  2041. end;
  2042. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  2043. end;
  2044. tmpreg:=getintregister(list,loadsize);
  2045. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  2046. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  2047. { make sure we do not read/write past the end of the array }
  2048. current_asmdata.getjumplabel(hl);
  2049. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  2050. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  2051. tmpindexreg:=getintregister(list,osuinttype);
  2052. { load current array value }
  2053. if (slopt<>SL_SETZERO) then
  2054. begin
  2055. tmpreg:=getintregister(list,osuinttype);
  2056. if (slopt<>SL_SETMAX) then
  2057. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  2058. else if (sref.bitlen<>AIntBits) then
  2059. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  2060. else
  2061. a_load_const_reg(list,osuinttype,-1,tmpreg);
  2062. end;
  2063. { generate mask to zero the bits we have to insert }
  2064. if (slopt<>SL_SETMAX) then
  2065. begin
  2066. maskreg:=getintregister(list,osuinttype);
  2067. if (target_info.endian=endian_big) then
  2068. begin
  2069. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  2070. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  2071. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  2072. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,maskreg);
  2073. end
  2074. else
  2075. begin
  2076. { Y-x = -(x-Y) }
  2077. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  2078. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  2079. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  2080. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  2081. end;
  2082. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  2083. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  2084. end;
  2085. if (slopt<>SL_SETZERO) then
  2086. begin
  2087. if (target_info.endian=endian_big) then
  2088. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,tmpreg)
  2089. else
  2090. begin
  2091. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  2092. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  2093. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  2094. end;
  2095. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  2096. end;
  2097. {$ifndef cpuhighleveltarget}
  2098. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  2099. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  2100. {$else}
  2101. tmpreg:=getintregister(list,loadsize);
  2102. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  2103. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  2104. {$endif}
  2105. a_label(list,hl);
  2106. end;
  2107. end;
  2108. end;
  2109. procedure thlcgobj.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  2110. var
  2111. bitmask: aword;
  2112. tmpreg: tregister;
  2113. subsetregdef: torddef;
  2114. stopbit: byte;
  2115. begin
  2116. tmpreg:=NR_NO;
  2117. subsetregdef:=cgsize_orddef(sreg.subsetregsize);
  2118. stopbit:=sreg.startbit+sreg.bitlen;
  2119. // on x86(64), 1 shl 32(64) = 1 instead of 0
  2120. if (stopbit<>AIntBits) then
  2121. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  2122. else
  2123. bitmask:=not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  2124. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  2125. begin
  2126. tmpreg:=getintregister(list,subsetregdef);
  2127. a_load_reg_reg(list,fromsize,subsetregdef,fromreg,tmpreg);
  2128. a_op_const_reg(list,OP_SHL,subsetregdef,sreg.startbit,tmpreg);
  2129. if (slopt<>SL_REGNOSRCMASK) then
  2130. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(not(bitmask)),tmpreg);
  2131. end;
  2132. if (slopt<>SL_SETMAX) and
  2133. { the "and" is not needed if the whole register is modified (except for SL_SETZERO),
  2134. because later on we do a move in this case instead of an or }
  2135. ((sreg.bitlen<>AIntBits) or (slopt=SL_SETZERO)) then
  2136. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  2137. case slopt of
  2138. SL_SETZERO : ;
  2139. SL_SETMAX :
  2140. if (sreg.bitlen<>AIntBits) then
  2141. a_op_const_reg(list,OP_OR,subsetregdef,
  2142. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  2143. sreg.subsetreg)
  2144. else
  2145. a_load_const_reg(list,subsetregdef,-1,sreg.subsetreg);
  2146. { if the whole register is modified, no "or" is needed }
  2147. else if sreg.bitlen=AIntBits then
  2148. a_load_reg_reg(list,subsetregdef,subsetregdef,tmpreg,sreg.subsetreg)
  2149. else
  2150. a_op_reg_reg(list,OP_OR,subsetregdef,tmpreg,sreg.subsetreg)
  2151. end;
  2152. end;
  2153. {$pop}
  2154. function thlcgobj.get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  2155. begin
  2156. result.ref:=ref;
  2157. inc(result.ref.offset,bitnumber div 8);
  2158. result.bitindexreg:=NR_NO;
  2159. result.startbit:=bitnumber mod 8;
  2160. result.bitlen:=1;
  2161. end;
  2162. function thlcgobj.get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  2163. begin
  2164. result.subsetreg:=setreg;
  2165. result.subsetregsize:=def_cgsize(setregsize);
  2166. { subsetregs always count from the least significant to the most significant bit }
  2167. if (target_info.endian=endian_big) then
  2168. result.startbit:=(setregsize.size*8)-bitnumber-1
  2169. else
  2170. result.startbit:=bitnumber;
  2171. result.bitlen:=1;
  2172. end;
  2173. function thlcgobj.get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  2174. var
  2175. refptrdef: tdef;
  2176. tmpreg,
  2177. newbase: tregister;
  2178. begin
  2179. result.ref:=ref;
  2180. result.startbit:=0;
  2181. result.bitlen:=1;
  2182. tmpreg:=getintregister(list,ptruinttype);
  2183. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  2184. a_op_const_reg(list,OP_SHR,ptruinttype,3,tmpreg);
  2185. { don't assign to ref.base, that one is for pointers and this is an index
  2186. (important for platforms like LLVM) }
  2187. if result.ref.index<>NR_NO then
  2188. begin
  2189. { don't just add to ref.index, as it may be scaled }
  2190. refptrdef:=cpointerdef.getreusable(refsize);
  2191. newbase:=getaddressregister(list,refptrdef);
  2192. a_loadaddr_ref_reg(list,refsize,refptrdef,ref,newbase);
  2193. reference_reset_base(result.ref,refptrdef,newbase,0,result.ref.alignment);
  2194. end;
  2195. result.ref.index:=tmpreg;
  2196. tmpreg:=getintregister(list,ptruinttype);
  2197. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  2198. a_op_const_reg(list,OP_AND,ptruinttype,7,tmpreg);
  2199. result.bitindexreg:=tmpreg;
  2200. end;
  2201. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  2202. var
  2203. reg: tregister;
  2204. regsize: tdef;
  2205. begin
  2206. if (fromsize.size>=tosize.size) then
  2207. regsize:=fromsize
  2208. else
  2209. regsize:=tosize;
  2210. reg:=getfpuregister(list,regsize);
  2211. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  2212. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  2213. end;
  2214. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  2215. begin
  2216. case loc.loc of
  2217. LOC_REFERENCE, LOC_CREFERENCE:
  2218. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  2219. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2220. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  2221. else
  2222. internalerror(2010120412);
  2223. end;
  2224. end;
  2225. procedure thlcgobj.a_loadfpu_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  2226. var
  2227. reg: tregister;
  2228. begin
  2229. case loc.loc of
  2230. LOC_REFERENCE, LOC_CREFERENCE:
  2231. begin
  2232. reg:=getfpuregister(list,tosize);
  2233. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  2234. a_loadfpu_reg_ref(list,tosize,tosize,reg,ref);
  2235. end;
  2236. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2237. begin
  2238. a_loadfpu_reg_ref(list,fromsize,tosize,loc.register,ref);
  2239. end;
  2240. else
  2241. internalerror(2014080802);
  2242. end;
  2243. end;
  2244. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  2245. begin
  2246. case loc.loc of
  2247. LOC_REFERENCE, LOC_CREFERENCE:
  2248. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  2249. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2250. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  2251. else
  2252. internalerror(2010120413);
  2253. end;
  2254. end;
  2255. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  2256. var
  2257. ref : treference;
  2258. begin
  2259. paramanager.alloccgpara(list,cgpara);
  2260. case cgpara.location^.loc of
  2261. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2262. begin
  2263. cgpara.check_simple_location;
  2264. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  2265. end;
  2266. LOC_REFERENCE,LOC_CREFERENCE:
  2267. begin
  2268. cgpara.check_simple_location;
  2269. reference_reset_base(ref,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2270. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  2271. end;
  2272. LOC_REGISTER,LOC_CREGISTER:
  2273. begin
  2274. { paramfpu_ref does the check_simpe_location check here if necessary }
  2275. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  2276. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  2277. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  2278. tg.Ungettemp(list,ref);
  2279. end;
  2280. else
  2281. internalerror(2010120422);
  2282. end;
  2283. end;
  2284. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  2285. var
  2286. hreg : tregister;
  2287. href : treference;
  2288. intptrdef: tdef;
  2289. begin
  2290. case cgpara.location^.loc of
  2291. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2292. begin
  2293. cgpara.check_simple_location;
  2294. paramanager.alloccgpara(list,cgpara);
  2295. a_loadfpu_ref_reg(list,fromsize,cgpara.location^.def,ref,cgpara.location^.register);
  2296. end;
  2297. LOC_REFERENCE,LOC_CREFERENCE:
  2298. begin
  2299. cgpara.check_simple_location;
  2300. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2301. { concatcopy should choose the best way to copy the data }
  2302. g_concatcopy(list,fromsize,ref,href);
  2303. end;
  2304. LOC_REGISTER,LOC_CREGISTER:
  2305. begin
  2306. cgpara.check_simple_location;
  2307. if fromsize.size<>cgpara.location^.def.size then
  2308. internalerror(2014080603);
  2309. { convert the reference from a floating point location to an
  2310. integer location, and load that }
  2311. intptrdef:=cpointerdef.getreusable(cgpara.location^.def);
  2312. hreg:=getaddressregister(list,intptrdef);
  2313. a_loadaddr_ref_reg(list,fromsize,intptrdef,ref,hreg);
  2314. reference_reset_base(href,intptrdef,hreg,0,ref.alignment);
  2315. a_load_ref_cgpara(list,cgpara.location^.def,ref,cgpara);
  2316. end
  2317. else
  2318. internalerror(2010120423);
  2319. end;
  2320. end;
  2321. procedure thlcgobj.a_loadmm_ref_ref(list: TAsmList; fromsize, tosize: tdef; const fromref, toref: treference; shuffle: pmmshuffle);
  2322. var
  2323. reg: tregister;
  2324. begin
  2325. reg:=getmmregister(list,tosize);
  2326. a_loadmm_ref_reg(list,fromsize,tosize,fromref,reg,shuffle);
  2327. a_loadmm_reg_ref(list,tosize,tosize,reg,toref,shuffle);
  2328. end;
  2329. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  2330. var
  2331. tmpreg: tregister;
  2332. begin
  2333. case loc.loc of
  2334. LOC_MMREGISTER,LOC_CMMREGISTER:
  2335. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2336. LOC_REFERENCE,LOC_CREFERENCE:
  2337. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  2338. LOC_REGISTER,LOC_CREGISTER:
  2339. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2340. LOC_SUBSETREG,LOC_CSUBSETREG,
  2341. LOC_SUBSETREF,LOC_CSUBSETREF:
  2342. begin
  2343. tmpreg:=getintregister(list,fromsize);
  2344. a_load_loc_reg(list,fromsize,fromsize,loc,tmpreg);
  2345. a_loadmm_intreg_reg(list,fromsize,tosize,tmpreg,reg,shuffle);
  2346. end
  2347. else
  2348. internalerror(2010120414);
  2349. end;
  2350. end;
  2351. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  2352. begin
  2353. case loc.loc of
  2354. LOC_MMREGISTER,LOC_CMMREGISTER:
  2355. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  2356. LOC_REFERENCE,LOC_CREFERENCE:
  2357. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  2358. else
  2359. internalerror(2010120415);
  2360. end;
  2361. end;
  2362. procedure thlcgobj.a_loadmm_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference; shuffle: pmmshuffle);
  2363. var
  2364. intreg, reg: tregister;
  2365. begin
  2366. case loc.loc of
  2367. LOC_MMREGISTER,LOC_CMMREGISTER:
  2368. a_loadmm_reg_ref(list,fromsize,tosize,loc.register,ref,shuffle);
  2369. LOC_REFERENCE,LOC_CREFERENCE:
  2370. begin
  2371. reg:=getmmregister(list,tosize);
  2372. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  2373. a_loadmm_reg_ref(list,tosize,tosize,reg,ref,shuffle);
  2374. end;
  2375. LOC_REGISTER,LOC_CREGISTER:
  2376. begin
  2377. reg:=getmmregister(list,tosize);
  2378. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2379. a_loadmm_reg_ref(list,tosize,tosize,reg,ref,shuffle);
  2380. end;
  2381. LOC_SUBSETREG,LOC_CSUBSETREG,
  2382. LOC_SUBSETREF,LOC_CSUBSETREF:
  2383. begin
  2384. intreg:=getintregister(list,fromsize);
  2385. reg:=getmmregister(list,tosize);
  2386. a_load_loc_reg(list,fromsize,fromsize,loc,intreg);
  2387. a_loadmm_intreg_reg(list,fromsize,tosize,intreg,reg,shuffle);
  2388. a_loadmm_reg_ref(list,tosize,tosize,reg,ref,shuffle);
  2389. end
  2390. else
  2391. internalerror(2014080803);
  2392. end;
  2393. end;
  2394. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  2395. var
  2396. href : treference;
  2397. begin
  2398. cgpara.check_simple_location;
  2399. paramanager.alloccgpara(list,cgpara);
  2400. case cgpara.location^.loc of
  2401. LOC_MMREGISTER,LOC_CMMREGISTER:
  2402. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  2403. LOC_REFERENCE,LOC_CREFERENCE:
  2404. begin
  2405. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2406. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  2407. end;
  2408. LOC_REGISTER,LOC_CREGISTER:
  2409. begin
  2410. if assigned(shuffle) and
  2411. not shufflescalar(shuffle) then
  2412. internalerror(2012071205);
  2413. a_loadmm_reg_intreg(list,fromsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  2414. end
  2415. else
  2416. internalerror(2012071204);
  2417. end;
  2418. end;
  2419. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  2420. var
  2421. hr : tregister;
  2422. hs : tmmshuffle;
  2423. begin
  2424. cgpara.check_simple_location;
  2425. hr:=getmmregister(list,cgpara.def);
  2426. a_loadmm_ref_reg(list,fromsize,cgpara.def,ref,hr,shuffle);
  2427. if realshuffle(shuffle) then
  2428. begin
  2429. hs:=shuffle^;
  2430. removeshuffles(hs);
  2431. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  2432. end
  2433. else
  2434. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  2435. end;
  2436. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  2437. begin
  2438. {$ifdef extdebug}
  2439. if def_cgsize(fromsize)<>loc.size then
  2440. internalerror(2012071203);
  2441. {$endif}
  2442. case loc.loc of
  2443. LOC_MMREGISTER,LOC_CMMREGISTER:
  2444. a_loadmm_reg_cgpara(list,fromsize,loc.register,cgpara,shuffle);
  2445. LOC_REFERENCE,LOC_CREFERENCE:
  2446. a_loadmm_ref_cgpara(list,fromsize,loc.reference,cgpara,shuffle);
  2447. else
  2448. internalerror(2012071202);
  2449. end;
  2450. end;
  2451. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2452. var
  2453. hr : tregister;
  2454. hs : tmmshuffle;
  2455. begin
  2456. hr:=getmmregister(list,size);
  2457. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2458. if realshuffle(shuffle) then
  2459. begin
  2460. hs:=shuffle^;
  2461. removeshuffles(hs);
  2462. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2463. end
  2464. else
  2465. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2466. end;
  2467. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  2468. begin
  2469. case loc.loc of
  2470. LOC_CMMREGISTER,LOC_MMREGISTER:
  2471. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2472. LOC_CREFERENCE,LOC_REFERENCE:
  2473. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2474. else
  2475. internalerror(2012071201);
  2476. end;
  2477. end;
  2478. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2479. var
  2480. hr : tregister;
  2481. hs : tmmshuffle;
  2482. begin
  2483. hr:=getmmregister(list,size);
  2484. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2485. if realshuffle(shuffle) then
  2486. begin
  2487. hs:=shuffle^;
  2488. removeshuffles(hs);
  2489. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2490. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2491. end
  2492. else
  2493. begin
  2494. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2495. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2496. end;
  2497. end;
  2498. (*
  2499. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2500. begin
  2501. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  2502. end;
  2503. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  2504. begin
  2505. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  2506. end;
  2507. *)
  2508. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  2509. var
  2510. tmpreg : tregister;
  2511. begin
  2512. tmpreg:=getintregister(list,size);
  2513. a_load_ref_reg(list,size,size,ref,tmpreg);
  2514. a_op_const_reg(list,op,size,a,tmpreg);
  2515. a_load_reg_ref(list,size,size,tmpreg,ref);
  2516. end;
  2517. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: tcgint; const sreg: tsubsetregister);
  2518. var
  2519. tmpreg: tregister;
  2520. begin
  2521. tmpreg:=getintregister(list,size);
  2522. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  2523. a_op_const_reg(list,op,size,a,tmpreg);
  2524. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  2525. end;
  2526. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  2527. var
  2528. tmpreg: tregister;
  2529. begin
  2530. tmpreg:=getintregister(list,size);
  2531. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  2532. a_op_const_reg(list,op,size,a,tmpreg);
  2533. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  2534. end;
  2535. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const loc: tlocation);
  2536. begin
  2537. case loc.loc of
  2538. LOC_REGISTER, LOC_CREGISTER:
  2539. a_op_const_reg(list,op,size,a,loc.register);
  2540. LOC_REFERENCE, LOC_CREFERENCE:
  2541. a_op_const_ref(list,op,size,a,loc.reference);
  2542. LOC_SUBSETREG, LOC_CSUBSETREG:
  2543. a_op_const_subsetreg(list,op,size,size,a,loc.sreg);
  2544. LOC_SUBSETREF, LOC_CSUBSETREF:
  2545. a_op_const_subsetref(list,op,size,size,a,loc.sref);
  2546. else
  2547. internalerror(2010120428);
  2548. end;
  2549. end;
  2550. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  2551. var
  2552. tmpreg: tregister;
  2553. begin
  2554. tmpreg:=getintregister(list,size);
  2555. a_load_ref_reg(list,size,size,ref,tmpreg);
  2556. case op of
  2557. OP_NOT,OP_NEG:
  2558. begin
  2559. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  2560. end;
  2561. else
  2562. begin
  2563. a_op_reg_reg(list,op,size,reg,tmpreg);
  2564. end;
  2565. end;
  2566. a_load_reg_ref(list,size,size,tmpreg,ref);
  2567. end;
  2568. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  2569. var
  2570. tmpreg: tregister;
  2571. begin
  2572. case op of
  2573. OP_NOT,OP_NEG:
  2574. { handle it as "load ref,reg; op reg" }
  2575. begin
  2576. a_load_ref_reg(list,size,size,ref,reg);
  2577. a_op_reg_reg(list,op,size,reg,reg);
  2578. end;
  2579. else
  2580. begin
  2581. tmpreg:=getintregister(list,size);
  2582. a_load_ref_reg(list,size,size,ref,tmpreg);
  2583. a_op_reg_reg(list,op,size,tmpreg,reg);
  2584. end;
  2585. end;
  2586. end;
  2587. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  2588. var
  2589. tmpreg: tregister;
  2590. begin
  2591. tmpreg:=getintregister(list,opsize);
  2592. a_load_subsetreg_reg(list,destsubsetsize,opsize,sreg,tmpreg);
  2593. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2594. a_load_reg_subsetreg(list,opsize,destsubsetsize,tmpreg,sreg);
  2595. end;
  2596. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  2597. var
  2598. tmpreg: tregister;
  2599. begin
  2600. tmpreg:=getintregister(list,opsize);
  2601. a_load_subsetref_reg(list,destsubsetsize,opsize,sref,tmpreg);
  2602. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2603. a_load_reg_subsetref(list,opsize,destsubsetsize,tmpreg,sref);
  2604. end;
  2605. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  2606. begin
  2607. case loc.loc of
  2608. LOC_REGISTER, LOC_CREGISTER:
  2609. a_op_reg_reg(list,op,size,reg,loc.register);
  2610. LOC_REFERENCE, LOC_CREFERENCE:
  2611. a_op_reg_ref(list,op,size,reg,loc.reference);
  2612. LOC_SUBSETREG, LOC_CSUBSETREG:
  2613. a_op_reg_subsetreg(list,op,size,size,reg,loc.sreg);
  2614. LOC_SUBSETREF, LOC_CSUBSETREF:
  2615. a_op_reg_subsetref(list,op,size,size,reg,loc.sref);
  2616. else
  2617. internalerror(2010120429);
  2618. end;
  2619. end;
  2620. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  2621. var
  2622. tmpreg: tregister;
  2623. begin
  2624. case loc.loc of
  2625. LOC_REGISTER,LOC_CREGISTER:
  2626. a_op_ref_reg(list,op,size,ref,loc.register);
  2627. LOC_REFERENCE,LOC_CREFERENCE:
  2628. begin
  2629. tmpreg:=getintregister(list,size);
  2630. a_load_ref_reg(list,size,size,ref,tmpreg);
  2631. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  2632. end;
  2633. LOC_SUBSETREG, LOC_CSUBSETREG:
  2634. begin
  2635. tmpreg:=getintregister(list,size);
  2636. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2637. a_op_ref_reg(list,op,size,ref,tmpreg);
  2638. a_load_reg_subsetreg(list,size,size,tmpreg,loc.sreg);
  2639. end;
  2640. LOC_SUBSETREF, LOC_CSUBSETREF:
  2641. begin
  2642. tmpreg:=getintregister(list,size);
  2643. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2644. a_op_ref_reg(list,op,size,ref,tmpreg);
  2645. a_load_reg_subsetref(list,size,size,tmpreg,loc.sref);
  2646. end;
  2647. else
  2648. internalerror(2010120429);
  2649. end;
  2650. end;
  2651. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  2652. begin
  2653. a_load_reg_reg(list,size,size,src,dst);
  2654. a_op_const_reg(list,op,size,a,dst);
  2655. end;
  2656. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  2657. var
  2658. tmpreg: tregister;
  2659. begin
  2660. if (dst<>src1) then
  2661. begin
  2662. a_load_reg_reg(list,size,size,src2,dst);
  2663. a_op_reg_reg(list,op,size,src1,dst);
  2664. end
  2665. else
  2666. begin
  2667. { can we do a direct operation on the target register ? }
  2668. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  2669. a_op_reg_reg(list,op,size,src2,dst)
  2670. else
  2671. begin
  2672. tmpreg:=getintregister(list,size);
  2673. a_load_reg_reg(list,size,size,src2,tmpreg);
  2674. a_op_reg_reg(list,op,size,src1,tmpreg);
  2675. a_load_reg_reg(list,size,size,tmpreg,dst);
  2676. end;
  2677. end;
  2678. end;
  2679. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2680. begin
  2681. ovloc.loc:=LOC_VOID;
  2682. if not setflags then
  2683. a_op_const_reg_reg(list,op,size,a,src,dst)
  2684. else
  2685. internalerror(2010122910);
  2686. end;
  2687. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2688. begin
  2689. ovloc.loc:=LOC_VOID;
  2690. if not setflags then
  2691. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  2692. else
  2693. internalerror(2010122911);
  2694. end;
  2695. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  2696. var
  2697. tmpreg: tregister;
  2698. begin
  2699. tmpreg:=getregisterfordef(list,size);
  2700. a_load_const_reg(list,size,a,tmpreg);
  2701. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2702. end;
  2703. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  2704. var
  2705. tmpreg: tregister;
  2706. begin
  2707. tmpreg:=getregisterfordef(list,size);
  2708. a_load_ref_reg(list,size,size,ref,tmpreg);
  2709. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2710. end;
  2711. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const loc: tlocation; l: tasmlabel);
  2712. var
  2713. tmpreg: tregister;
  2714. begin
  2715. case loc.loc of
  2716. LOC_REGISTER,LOC_CREGISTER:
  2717. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2718. LOC_REFERENCE,LOC_CREFERENCE:
  2719. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2720. LOC_SUBSETREG, LOC_CSUBSETREG:
  2721. begin
  2722. tmpreg:=getintregister(list,size);
  2723. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2724. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2725. end;
  2726. LOC_SUBSETREF, LOC_CSUBSETREF:
  2727. begin
  2728. tmpreg:=getintregister(list,size);
  2729. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2730. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2731. end;
  2732. else
  2733. internalerror(2010120430);
  2734. end;
  2735. end;
  2736. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  2737. var
  2738. tmpreg: tregister;
  2739. begin
  2740. tmpreg:=getintregister(list,size);
  2741. a_load_ref_reg(list,size,size,ref,tmpreg);
  2742. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2743. end;
  2744. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  2745. var
  2746. tmpreg: tregister;
  2747. begin
  2748. tmpreg:=getintregister(list,size);
  2749. a_load_ref_reg(list,size,size,ref,tmpreg);
  2750. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2751. end;
  2752. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  2753. var
  2754. tmpreg: tregister;
  2755. begin
  2756. tmpreg:=getintregister(list,cmpsize);
  2757. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  2758. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2759. end;
  2760. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  2761. var
  2762. tmpreg: tregister;
  2763. begin
  2764. tmpreg:=getintregister(list,cmpsize);
  2765. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  2766. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2767. end;
  2768. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  2769. begin
  2770. case loc.loc of
  2771. LOC_REGISTER,
  2772. LOC_CREGISTER:
  2773. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2774. LOC_REFERENCE,
  2775. LOC_CREFERENCE :
  2776. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2777. LOC_CONSTANT:
  2778. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2779. LOC_SUBSETREG,
  2780. LOC_CSUBSETREG:
  2781. a_cmp_subsetreg_reg_label(list,size,size,cmp_op,loc.sreg,reg,l);
  2782. LOC_SUBSETREF,
  2783. LOC_CSUBSETREF:
  2784. a_cmp_subsetref_reg_label(list,size,size,cmp_op,loc.sref,reg,l);
  2785. else
  2786. internalerror(2010120431);
  2787. end;
  2788. end;
  2789. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  2790. begin
  2791. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2792. end;
  2793. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  2794. var
  2795. tmpreg: tregister;
  2796. begin
  2797. case loc.loc of
  2798. LOC_REGISTER,LOC_CREGISTER:
  2799. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2800. LOC_REFERENCE,LOC_CREFERENCE:
  2801. begin
  2802. tmpreg:=getintregister(list,size);
  2803. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2804. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2805. end;
  2806. LOC_CONSTANT:
  2807. begin
  2808. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  2809. end;
  2810. LOC_SUBSETREG, LOC_CSUBSETREG:
  2811. begin
  2812. tmpreg:=getintregister(list,size);
  2813. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2814. a_cmp_subsetreg_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2815. end;
  2816. LOC_SUBSETREF, LOC_CSUBSETREF:
  2817. begin
  2818. tmpreg:=getintregister(list,size);
  2819. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2820. a_cmp_subsetref_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2821. end;
  2822. else
  2823. internalerror(2010120432);
  2824. end;
  2825. end;
  2826. procedure thlcgobj.g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference);
  2827. begin
  2828. a_load_reg_ref(list,fromsize,tosize,reg,href);
  2829. end;
  2830. procedure thlcgobj.g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference);
  2831. begin
  2832. a_load_const_ref(list,size,a,href);
  2833. end;
  2834. procedure thlcgobj.g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister);
  2835. begin
  2836. a_load_ref_reg(list,fromsize,tosize,href,reg);
  2837. end;
  2838. procedure thlcgobj.g_exception_reason_discard(list: TAsmList; size: tdef; href: treference);
  2839. begin
  2840. { do nothing by default }
  2841. end;
  2842. procedure thlcgobj.g_maybe_testself(list: TAsmList; selftype: tdef; reg: tregister);
  2843. var
  2844. OKLabel : tasmlabel;
  2845. cgpara1 : TCGPara;
  2846. pd : tprocdef;
  2847. begin
  2848. if (cs_check_object in current_settings.localswitches) or
  2849. (cs_check_range in current_settings.localswitches) then
  2850. begin
  2851. pd:=search_system_proc('fpc_handleerror');
  2852. current_asmdata.getjumplabel(oklabel);
  2853. a_cmp_const_reg_label(list,selftype,OC_NE,0,reg,oklabel);
  2854. cgpara1.init;
  2855. paramanager.getintparaloc(list,pd,1,cgpara1);
  2856. a_load_const_cgpara(list,s32inttype,aint(210),cgpara1);
  2857. paramanager.freecgpara(list,cgpara1);
  2858. g_call_system_proc(list,pd,[@cgpara1],nil);
  2859. cgpara1.done;
  2860. a_label(list,oklabel);
  2861. end;
  2862. end;
  2863. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  2864. begin
  2865. if use_vectorfpu(size) then
  2866. a_loadmm_ref_ref(list,size,size,source,dest,mms_movescalar)
  2867. else if size.typ<>floatdef then
  2868. a_load_ref_ref(list,size,size,source,dest)
  2869. else
  2870. a_loadfpu_ref_ref(list,size,size,source,dest);
  2871. end;
  2872. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  2873. begin
  2874. g_concatcopy(list,size,source,dest);
  2875. end;
  2876. procedure thlcgobj.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  2877. var
  2878. cgpara1,cgpara2,cgpara3 : TCGPara;
  2879. pd : tprocdef;
  2880. begin
  2881. pd:=search_system_proc('fpc_shortstr_to_shortstr');
  2882. cgpara1.init;
  2883. cgpara2.init;
  2884. cgpara3.init;
  2885. paramanager.getintparaloc(list,pd,1,cgpara1);
  2886. paramanager.getintparaloc(list,pd,2,cgpara2);
  2887. paramanager.getintparaloc(list,pd,3,cgpara3);
  2888. if pd.is_pushleftright then
  2889. begin
  2890. a_loadaddr_ref_cgpara(list,strdef,dest,cgpara1);
  2891. a_load_const_cgpara(list,s32inttype,strdef.len,cgpara2);
  2892. a_loadaddr_ref_cgpara(list,strdef,source,cgpara3);
  2893. end
  2894. else
  2895. begin
  2896. a_loadaddr_ref_cgpara(list,strdef,source,cgpara3);
  2897. a_load_const_cgpara(list,s32inttype,strdef.len,cgpara2);
  2898. a_loadaddr_ref_cgpara(list,strdef,dest,cgpara1);
  2899. end;
  2900. paramanager.freecgpara(list,cgpara3);
  2901. paramanager.freecgpara(list,cgpara2);
  2902. paramanager.freecgpara(list,cgpara1);
  2903. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil);
  2904. cgpara3.done;
  2905. cgpara2.done;
  2906. cgpara1.done;
  2907. end;
  2908. procedure thlcgobj.g_copyvariant(list: TAsmList; const source, dest: treference; vardef: tvariantdef);
  2909. var
  2910. cgpara1,cgpara2 : TCGPara;
  2911. pd : tprocdef;
  2912. begin
  2913. pd:=search_system_proc('fpc_variant_copy_overwrite');
  2914. cgpara1.init;
  2915. cgpara2.init;
  2916. paramanager.getintparaloc(list,pd,1,cgpara1);
  2917. paramanager.getintparaloc(list,pd,2,cgpara2);
  2918. if pd.is_pushleftright then
  2919. begin
  2920. a_loadaddr_ref_cgpara(list,vardef,source,cgpara1);
  2921. a_loadaddr_ref_cgpara(list,vardef,dest,cgpara2);
  2922. end
  2923. else
  2924. begin
  2925. a_loadaddr_ref_cgpara(list,vardef,dest,cgpara2);
  2926. a_loadaddr_ref_cgpara(list,vardef,source,cgpara1);
  2927. end;
  2928. paramanager.freecgpara(list,cgpara2);
  2929. paramanager.freecgpara(list,cgpara1);
  2930. g_call_system_proc(list,pd,[@cgpara1,@cgpara2],nil);
  2931. cgpara2.done;
  2932. cgpara1.done;
  2933. end;
  2934. procedure thlcgobj.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  2935. var
  2936. href : treference;
  2937. incrfunc : string;
  2938. cgpara1,cgpara2 : TCGPara;
  2939. pd : tprocdef;
  2940. begin
  2941. cgpara1.init;
  2942. cgpara2.init;
  2943. if is_interfacecom_or_dispinterface(t) then
  2944. incrfunc:='fpc_intf_incr_ref'
  2945. else if is_ansistring(t) then
  2946. incrfunc:='fpc_ansistr_incr_ref'
  2947. else if is_widestring(t) then
  2948. incrfunc:='fpc_widestr_incr_ref'
  2949. else if is_unicodestring(t) then
  2950. incrfunc:='fpc_unicodestr_incr_ref'
  2951. else if is_dynamic_array(t) then
  2952. incrfunc:='fpc_dynarray_incr_ref'
  2953. else
  2954. incrfunc:='';
  2955. { call the special incr function or the generic addref }
  2956. if incrfunc<>'' then
  2957. begin
  2958. pd:=search_system_proc(incrfunc);
  2959. paramanager.getintparaloc(list,pd,1,cgpara1);
  2960. { widestrings aren't ref. counted on all platforms so we need the address
  2961. to create a real copy }
  2962. if is_widestring(t) then
  2963. a_loadaddr_ref_cgpara(list,t,ref,cgpara1)
  2964. else
  2965. { these functions get the pointer by value }
  2966. a_load_ref_cgpara(list,t,ref,cgpara1);
  2967. paramanager.freecgpara(list,cgpara1);
  2968. g_call_system_proc(list,pd,[@cgpara1],nil);
  2969. end
  2970. else
  2971. begin
  2972. pd:=search_system_proc('fpc_addref');
  2973. paramanager.getintparaloc(list,pd,1,cgpara1);
  2974. paramanager.getintparaloc(list,pd,2,cgpara2);
  2975. if is_open_array(t) then
  2976. InternalError(201103054);
  2977. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti,false),0,sizeof(pint));
  2978. if pd.is_pushleftright then
  2979. begin
  2980. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2981. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2982. end
  2983. else
  2984. begin
  2985. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2986. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2987. end;
  2988. paramanager.freecgpara(list,cgpara1);
  2989. paramanager.freecgpara(list,cgpara2);
  2990. g_call_system_proc(list,pd,[@cgpara1,@cgpara2],nil);
  2991. end;
  2992. cgpara2.done;
  2993. cgpara1.done;
  2994. end;
  2995. procedure thlcgobj.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  2996. var
  2997. href : treference;
  2998. cgpara1,cgpara2 : TCGPara;
  2999. pd : tprocdef;
  3000. begin
  3001. cgpara1.init;
  3002. cgpara2.init;
  3003. if is_ansistring(t) or
  3004. is_widestring(t) or
  3005. is_unicodestring(t) or
  3006. is_interfacecom_or_dispinterface(t) or
  3007. is_dynamic_array(t) then
  3008. a_load_const_ref(list,t,0,ref)
  3009. else if t.typ=variantdef then
  3010. begin
  3011. pd:=search_system_proc('fpc_variant_init');
  3012. paramanager.getintparaloc(list,pd,1,cgpara1);
  3013. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3014. paramanager.freecgpara(list,cgpara1);
  3015. g_call_system_proc(list,pd,[@cgpara1],nil);
  3016. end
  3017. else
  3018. begin
  3019. if is_open_array(t) then
  3020. InternalError(201103052);
  3021. pd:=search_system_proc('fpc_initialize');
  3022. paramanager.getintparaloc(list,pd,1,cgpara1);
  3023. paramanager.getintparaloc(list,pd,2,cgpara2);
  3024. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti,false),0,sizeof(pint));
  3025. if pd.is_pushleftright then
  3026. begin
  3027. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3028. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3029. end
  3030. else
  3031. begin
  3032. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3033. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3034. end;
  3035. paramanager.freecgpara(list,cgpara1);
  3036. paramanager.freecgpara(list,cgpara2);
  3037. g_call_system_proc(list,pd,[@cgpara1,@cgpara2],nil);
  3038. end;
  3039. cgpara1.done;
  3040. cgpara2.done;
  3041. end;
  3042. procedure thlcgobj.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  3043. var
  3044. href : treference;
  3045. cgpara1,cgpara2 : TCGPara;
  3046. pd : tprocdef;
  3047. decrfunc : string;
  3048. begin
  3049. if is_interfacecom_or_dispinterface(t) then
  3050. decrfunc:='fpc_intf_decr_ref'
  3051. else if is_ansistring(t) then
  3052. decrfunc:='fpc_ansistr_decr_ref'
  3053. else if is_widestring(t) then
  3054. decrfunc:='fpc_widestr_decr_ref'
  3055. else if is_unicodestring(t) then
  3056. decrfunc:='fpc_unicodestr_decr_ref'
  3057. else if t.typ=variantdef then
  3058. decrfunc:='fpc_variant_clear'
  3059. else
  3060. begin
  3061. cgpara1.init;
  3062. cgpara2.init;
  3063. if is_open_array(t) then
  3064. InternalError(201103051);
  3065. { fpc_finalize takes a pointer value parameter, fpc_dynarray_clear a
  3066. pointer var parameter }
  3067. if is_dynamic_array(t) then
  3068. pd:=search_system_proc('fpc_dynarray_clear')
  3069. else
  3070. pd:=search_system_proc('fpc_finalize');
  3071. paramanager.getintparaloc(list,pd,1,cgpara1);
  3072. paramanager.getintparaloc(list,pd,2,cgpara2);
  3073. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti,false),0,sizeof(pint));
  3074. if pd.is_pushleftright then
  3075. begin
  3076. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3077. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3078. end
  3079. else
  3080. begin
  3081. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3082. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3083. end;
  3084. paramanager.freecgpara(list,cgpara1);
  3085. paramanager.freecgpara(list,cgpara2);
  3086. g_call_system_proc(list,pd,[@cgpara1,@cgpara2],nil);
  3087. cgpara1.done;
  3088. cgpara2.done;
  3089. exit;
  3090. end;
  3091. pd:=search_system_proc(decrfunc);
  3092. cgpara1.init;
  3093. paramanager.getintparaloc(list,pd,1,cgpara1);
  3094. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3095. paramanager.freecgpara(list,cgpara1);
  3096. g_call_system_proc(list,pd,[@cgpara1],nil);
  3097. cgpara1.done;
  3098. end;
  3099. procedure thlcgobj.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  3100. var
  3101. cgpara1,cgpara2,cgpara3: TCGPara;
  3102. href: TReference;
  3103. hreg, lenreg: TRegister;
  3104. pd: tprocdef;
  3105. begin
  3106. cgpara1.init;
  3107. cgpara2.init;
  3108. cgpara3.init;
  3109. pd:=search_system_proc(name);
  3110. paramanager.getintparaloc(list,pd,1,cgpara1);
  3111. paramanager.getintparaloc(list,pd,2,cgpara2);
  3112. paramanager.getintparaloc(list,pd,3,cgpara3);
  3113. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti,false),0,sizeof(pint));
  3114. { if calling convention is left to right, push parameters 1 and 2 }
  3115. if pd.is_pushleftright then
  3116. begin
  3117. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3118. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3119. end;
  3120. { push parameter 3 }
  3121. if highloc.loc=LOC_CONSTANT then
  3122. a_load_const_cgpara(list,ptrsinttype,highloc.value+1,cgpara3)
  3123. else
  3124. begin
  3125. if highloc.loc in [LOC_REGISTER,LOC_CREGISTER] then
  3126. hreg:=highloc.register
  3127. else
  3128. begin
  3129. hreg:=getintregister(list,ptrsinttype);
  3130. a_load_loc_reg(list,ptrsinttype,ptrsinttype,highloc,hreg);
  3131. end;
  3132. { increment, converts high(x) to length(x) }
  3133. lenreg:=getintregister(list,ptrsinttype);
  3134. a_op_const_reg_reg(list,OP_ADD,ptrsinttype,1,hreg,lenreg);
  3135. a_load_reg_cgpara(list,ptrsinttype,lenreg,cgpara3);
  3136. end;
  3137. { if calling convention is right to left, push parameters 2 and 1 }
  3138. if not pd.is_pushleftright then
  3139. begin
  3140. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  3141. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  3142. end;
  3143. paramanager.freecgpara(list,cgpara1);
  3144. paramanager.freecgpara(list,cgpara2);
  3145. paramanager.freecgpara(list,cgpara3);
  3146. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil);
  3147. cgpara3.done;
  3148. cgpara2.done;
  3149. cgpara1.done;
  3150. end;
  3151. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  3152. var
  3153. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  3154. aintmax: aint;
  3155. {$else}
  3156. aintmax: longint;
  3157. {$endif}
  3158. neglabel : tasmlabel;
  3159. hreg : tregister;
  3160. lto,hto,
  3161. lfrom,hfrom : TConstExprInt;
  3162. fromsize, tosize: cardinal;
  3163. maxdef: tdef;
  3164. from_signed, to_signed: boolean;
  3165. begin
  3166. { range checking on and range checkable value? }
  3167. if not(cs_check_range in current_settings.localswitches) or
  3168. not(fromdef.typ in [orddef,enumdef]) or
  3169. { C-style booleans can't really fail range checks, }
  3170. { all values are always valid }
  3171. is_cbool(todef) then
  3172. exit;
  3173. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  3174. { handle 64bit rangechecks separate for 32bit processors }
  3175. if is_64bit(fromdef) or is_64bit(todef) then
  3176. begin
  3177. cg64.g_rangecheck64(list,l,fromdef,todef);
  3178. exit;
  3179. end;
  3180. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  3181. { only check when assigning to scalar, subranges are different, }
  3182. { when todef=fromdef then the check is always generated }
  3183. getrange(fromdef,lfrom,hfrom);
  3184. getrange(todef,lto,hto);
  3185. from_signed := is_signed(fromdef);
  3186. to_signed := is_signed(todef);
  3187. { check the rangedef of the array, not the array itself }
  3188. { (only change now, since getrange needs the arraydef) }
  3189. if (todef.typ = arraydef) then
  3190. todef := tarraydef(todef).rangedef;
  3191. { no range check if from and to are equal and are both longint/dword }
  3192. { (if we have a 32bit processor) or int64/qword, since such }
  3193. { operations can at most cause overflows (JM) }
  3194. { Note that these checks are mostly processor independent, they only }
  3195. { have to be changed once we introduce 64bit subrange types }
  3196. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  3197. if (fromdef=todef) and
  3198. (fromdef.typ=orddef) and
  3199. (((((torddef(fromdef).ordtype=s64bit) and
  3200. (lfrom = low(int64)) and
  3201. (hfrom = high(int64))) or
  3202. ((torddef(fromdef).ordtype=u64bit) and
  3203. (lfrom = low(qword)) and
  3204. (hfrom = high(qword))) or
  3205. ((torddef(fromdef).ordtype=scurrency) and
  3206. (lfrom = low(int64)) and
  3207. (hfrom = high(int64)))))) then
  3208. exit;
  3209. {$endif cpuhighleveltarget or cpu64bitalu}
  3210. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  3211. targets }
  3212. {$ifdef cpu32bitaddr}
  3213. if (fromdef = todef) and
  3214. (fromdef.typ=orddef) and
  3215. (((((torddef(fromdef).ordtype = s32bit) and
  3216. (lfrom = int64(low(longint))) and
  3217. (hfrom = int64(high(longint)))) or
  3218. ((torddef(fromdef).ordtype = u32bit) and
  3219. (lfrom = low(cardinal)) and
  3220. (hfrom = high(cardinal)))))) then
  3221. exit;
  3222. {$endif cpu32bitaddr}
  3223. { optimize some range checks away in safe cases }
  3224. fromsize := fromdef.size;
  3225. tosize := todef.size;
  3226. if ((from_signed = to_signed) or
  3227. (not from_signed)) and
  3228. (lto<=lfrom) and (hto>=hfrom) and
  3229. (fromsize <= tosize) then
  3230. begin
  3231. { if fromsize < tosize, and both have the same signed-ness or }
  3232. { fromdef is unsigned, then all bit patterns from fromdef are }
  3233. { valid for todef as well }
  3234. if (fromsize < tosize) then
  3235. exit;
  3236. if (fromsize = tosize) and
  3237. (from_signed = to_signed) then
  3238. { only optimize away if all bit patterns which fit in fromsize }
  3239. { are valid for the todef }
  3240. begin
  3241. {$ifopt Q+}
  3242. {$define overflowon}
  3243. {$Q-}
  3244. {$endif}
  3245. {$ifopt R+}
  3246. {$define rangeon}
  3247. {$R-}
  3248. {$endif}
  3249. if to_signed then
  3250. begin
  3251. { calculation of the low/high ranges must not overflow 64 bit
  3252. otherwise we end up comparing with zero for 64 bit data types on
  3253. 64 bit processors }
  3254. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  3255. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  3256. exit
  3257. end
  3258. else
  3259. begin
  3260. { calculation of the low/high ranges must not overflow 64 bit
  3261. otherwise we end up having all zeros for 64 bit data types on
  3262. 64 bit processors }
  3263. if (lto = 0) and
  3264. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  3265. exit
  3266. end;
  3267. {$ifdef overflowon}
  3268. {$Q+}
  3269. {$undef overflowon}
  3270. {$endif}
  3271. {$ifdef rangeon}
  3272. {$R+}
  3273. {$undef rangeon}
  3274. {$endif}
  3275. end
  3276. end;
  3277. { depending on the types involved, we perform the range check for 64 or
  3278. for 32 bit }
  3279. if fromsize=8 then
  3280. maxdef:=fromdef
  3281. else
  3282. maxdef:=todef;
  3283. {$if sizeof(aintmax) = 8}
  3284. if maxdef.size=8 then
  3285. aintmax:=high(int64)
  3286. else
  3287. {$endif}
  3288. begin
  3289. aintmax:=high(longint);
  3290. maxdef:=u32inttype;
  3291. end;
  3292. { generate the rangecheck code for the def where we are going to }
  3293. { store the result }
  3294. { use the trick that }
  3295. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  3296. { To be able to do that, we have to make sure however that either }
  3297. { fromdef and todef are both signed or unsigned, or that we leave }
  3298. { the parts < 0 and > maxlongint out }
  3299. if from_signed xor to_signed then
  3300. begin
  3301. if from_signed then
  3302. { from is signed, to is unsigned }
  3303. begin
  3304. { if high(from) < 0 -> always range error }
  3305. if (hfrom < 0) or
  3306. { if low(to) > maxlongint also range error }
  3307. (lto > aintmax) then
  3308. begin
  3309. g_call_system_proc(list,'fpc_rangeerror',[],nil);
  3310. exit
  3311. end;
  3312. { from is signed and to is unsigned -> when looking at to }
  3313. { as an signed value, it must be < maxaint (otherwise }
  3314. { it will become negative, which is invalid since "to" is unsigned) }
  3315. if hto > aintmax then
  3316. hto := aintmax;
  3317. end
  3318. else
  3319. { from is unsigned, to is signed }
  3320. begin
  3321. if (lfrom > aintmax) or
  3322. (hto < 0) then
  3323. begin
  3324. g_call_system_proc(list,'fpc_rangeerror',[],nil);
  3325. exit
  3326. end;
  3327. { from is unsigned and to is signed -> when looking at to }
  3328. { as an unsigned value, it must be >= 0 (since negative }
  3329. { values are the same as values > maxlongint) }
  3330. if lto < 0 then
  3331. lto := 0;
  3332. end;
  3333. end;
  3334. hreg:=getintregister(list,maxdef);
  3335. a_load_loc_reg(list,fromdef,maxdef,l,hreg);
  3336. a_op_const_reg(list,OP_SUB,maxdef,tcgint(int64(lto)),hreg);
  3337. current_asmdata.getjumplabel(neglabel);
  3338. {
  3339. if from_signed then
  3340. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  3341. else
  3342. }
  3343. if qword(hto-lto)>qword(aintmax) then
  3344. a_cmp_const_reg_label(list,maxdef,OC_BE,aintmax,hreg,neglabel)
  3345. else
  3346. a_cmp_const_reg_label(list,maxdef,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  3347. g_call_system_proc(list,'fpc_rangeerror',[],nil);
  3348. a_label(list,neglabel);
  3349. end;
  3350. procedure thlcgobj.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  3351. var
  3352. sizereg,sourcereg,lenreg : tregister;
  3353. cgpara1,cgpara2,cgpara3 : TCGPara;
  3354. ptrarrdef : tdef;
  3355. pd : tprocdef;
  3356. getmemres : tcgpara;
  3357. destloc : tlocation;
  3358. begin
  3359. { because some abis don't support dynamic stack allocation properly
  3360. open array value parameters are copied onto the heap
  3361. }
  3362. include(current_procinfo.flags, pi_has_open_array_parameter);
  3363. { calculate necessary memory }
  3364. { read/write operations on one register make the life of the register allocator hard }
  3365. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  3366. begin
  3367. lenreg:=getintregister(list,sinttype);
  3368. a_load_loc_reg(list,sinttype,sinttype,lenloc,lenreg);
  3369. end
  3370. else
  3371. lenreg:=lenloc.register;
  3372. sizereg:=getintregister(list,sinttype);
  3373. a_op_const_reg_reg(list,OP_ADD,sinttype,1,lenreg,sizereg);
  3374. a_op_const_reg(list,OP_IMUL,sinttype,arrdef.elesize,sizereg);
  3375. { load source }
  3376. ptrarrdef:=cpointerdef.getreusable(arrdef);
  3377. sourcereg:=getaddressregister(list,ptrarrdef);
  3378. a_loadaddr_ref_reg(list,arrdef,ptrarrdef,ref,sourcereg);
  3379. { do getmem call }
  3380. pd:=search_system_proc('fpc_getmem');
  3381. cgpara1.init;
  3382. paramanager.getintparaloc(list,pd,1,cgpara1);
  3383. a_load_reg_cgpara(list,sinttype,sizereg,cgpara1);
  3384. paramanager.freecgpara(list,cgpara1);
  3385. getmemres:=g_call_system_proc(list,pd,[@cgpara1],ptrarrdef);
  3386. cgpara1.done;
  3387. { return the new address }
  3388. location_reset(destloc,LOC_REGISTER,OS_ADDR);
  3389. destloc.register:=destreg;
  3390. gen_load_cgpara_loc(list,ptrarrdef,getmemres,destloc,false);
  3391. { do move call }
  3392. pd:=search_system_proc('MOVE');
  3393. cgpara1.init;
  3394. cgpara2.init;
  3395. cgpara3.init;
  3396. paramanager.getintparaloc(list,pd,1,cgpara1);
  3397. paramanager.getintparaloc(list,pd,2,cgpara2);
  3398. paramanager.getintparaloc(list,pd,3,cgpara3);
  3399. if pd.is_pushleftright then
  3400. begin
  3401. { load source }
  3402. a_load_reg_cgpara(list,ptrarrdef,sourcereg,cgpara1);
  3403. { load destination }
  3404. a_load_reg_cgpara(list,ptrarrdef,destreg,cgpara2);
  3405. { load size }
  3406. a_load_reg_cgpara(list,ptrsinttype,sizereg,cgpara3);
  3407. end
  3408. else
  3409. begin
  3410. { load size }
  3411. a_load_reg_cgpara(list,ptrsinttype,sizereg,cgpara3);
  3412. { load destination }
  3413. a_load_reg_cgpara(list,ptrarrdef,destreg,cgpara2);
  3414. { load source }
  3415. a_load_reg_cgpara(list,ptrarrdef,sourcereg,cgpara1);
  3416. end;
  3417. paramanager.freecgpara(list,cgpara3);
  3418. paramanager.freecgpara(list,cgpara2);
  3419. paramanager.freecgpara(list,cgpara1);
  3420. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil);
  3421. cgpara3.done;
  3422. cgpara2.done;
  3423. cgpara1.done;
  3424. getmemres.resetiftemp;
  3425. end;
  3426. procedure thlcgobj.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  3427. var
  3428. cgpara1 : TCGPara;
  3429. pd : tprocdef;
  3430. begin
  3431. { do freemem call }
  3432. pd:=search_system_proc('fpc_freemem');
  3433. cgpara1.init;
  3434. paramanager.getintparaloc(list,pd,1,cgpara1);
  3435. { load source }
  3436. a_load_loc_cgpara(list,cpointerdef.getreusable(arrdef),l,cgpara1);
  3437. paramanager.freecgpara(list,cgpara1);
  3438. g_call_system_proc(list,pd,[@cgpara1],nil);
  3439. cgpara1.done;
  3440. end;
  3441. procedure thlcgobj.g_profilecode(list: TAsmList);
  3442. begin
  3443. end;
  3444. procedure thlcgobj.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  3445. begin
  3446. cg.a_jmp_name(list,externalname);
  3447. end;
  3448. procedure thlcgobj.g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  3449. begin
  3450. case regtyp of
  3451. R_INTREGISTER:
  3452. toreg:=getintregister(list,regsize);
  3453. R_ADDRESSREGISTER:
  3454. toreg:=getaddressregister(list,regsize);
  3455. R_FPUREGISTER:
  3456. toreg:=getfpuregister(list,regsize);
  3457. else
  3458. internalerror(2013112910);
  3459. end;
  3460. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  3461. end;
  3462. procedure thlcgobj.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  3463. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  3464. begin
  3465. case regtyp of
  3466. R_INTREGISTER:
  3467. toreg:=getintregister(list,regsize);
  3468. R_ADDRESSREGISTER:
  3469. toreg:=getaddressregister(list,regsize);
  3470. R_FPUREGISTER:
  3471. toreg:=getfpuregister(list,regsize);
  3472. else
  3473. internalerror(2013112915);
  3474. end;
  3475. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  3476. end;
  3477. begin
  3478. toloc:=fromloc;
  3479. case fromloc.loc of
  3480. { volatile location, can't get a permanent reference }
  3481. LOC_REGISTER,
  3482. LOC_FPUREGISTER:
  3483. internalerror(2012012702);
  3484. LOC_CONSTANT:
  3485. { finished }
  3486. ;
  3487. LOC_CREGISTER:
  3488. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  3489. LOC_CFPUREGISTER:
  3490. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  3491. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  3492. reference to such a location for multiple reading }
  3493. LOC_CREFERENCE,
  3494. LOC_REFERENCE:
  3495. begin
  3496. if (fromloc.reference.base<>NR_NO) and
  3497. (fromloc.reference.base<>current_procinfo.framepointer) and
  3498. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  3499. handle_reg_move(voidpointertype,fromloc.reference.base,toloc.reference.base,getregtype(fromloc.reference.base));
  3500. if (fromloc.reference.index<>NR_NO) and
  3501. (fromloc.reference.index<>current_procinfo.framepointer) and
  3502. (fromloc.reference.index<>NR_STACK_POINTER_REG) then
  3503. handle_reg_move(voidpointertype,fromloc.reference.index,toloc.reference.index,getregtype(fromloc.reference.index));
  3504. end;
  3505. else
  3506. internalerror(2012012701);
  3507. end;
  3508. end;
  3509. procedure thlcgobj.g_ptrtypecast_reg(list: TAsmList; fromdef, todef: tdef; var reg: tregister);
  3510. begin
  3511. { nothing to do }
  3512. end;
  3513. procedure thlcgobj.g_ptrtypecast_ref(list: TAsmList; fromdef, todef: tdef; var ref: treference);
  3514. begin
  3515. { nothing to do }
  3516. end;
  3517. procedure thlcgobj.g_set_addr_nonbitpacked_field_ref(list: TAsmList; recdef: tabstractrecorddef; field: tfieldvarsym; var recref: treference);
  3518. begin
  3519. inc(recref.offset,field.fieldoffset);
  3520. recref.alignment:=newalignment(recref.alignment,field.fieldoffset);
  3521. end;
  3522. procedure thlcgobj.g_setup_load_field_by_name(list: TAsmList; recdef: trecorddef; const name: TIDString; const recref: treference; out fref: treference; out fielddef: tdef);
  3523. var
  3524. sym: tsym;
  3525. field: tfieldvarsym;
  3526. begin
  3527. sym:=search_struct_member(recdef,name);
  3528. if not assigned(sym) or
  3529. (sym.typ<>fieldvarsym) then
  3530. internalerror(2015111901);
  3531. field:=tfieldvarsym(sym);
  3532. fref:=recref;
  3533. fielddef:=field.vardef;
  3534. g_set_addr_nonbitpacked_field_ref(list,recdef,field,fref);
  3535. end;
  3536. procedure thlcgobj.g_load_reg_field_by_name(list: TAsmList; regsize: tdef; recdef: trecorddef; reg: tregister; const name: TIDString; const recref: treference);
  3537. var
  3538. fref: treference;
  3539. fielddef: tdef;
  3540. begin
  3541. g_setup_load_field_by_name(list,recdef,name,recref,fref,fielddef);
  3542. a_load_reg_ref(list,regsize,fielddef,reg,fref);
  3543. end;
  3544. procedure thlcgobj.g_load_const_field_by_name(list: TAsmList; recdef: trecorddef; a: tcgint; const name: TIDString; const recref: treference);
  3545. var
  3546. fref: treference;
  3547. fielddef: tdef;
  3548. begin
  3549. g_setup_load_field_by_name(list,recdef,name,recref,fref,fielddef);
  3550. a_load_const_ref(list,fielddef,a,fref);
  3551. end;
  3552. procedure thlcgobj.g_load_field_reg_by_name(list: TAsmList; recdef: trecorddef; regsize: tdef; const name: TIDString; const recref: treference; reg: tregister);
  3553. var
  3554. fref: treference;
  3555. fielddef: tdef;
  3556. begin
  3557. g_setup_load_field_by_name(list,recdef,name,recref,fref,fielddef);
  3558. a_load_ref_reg(list,fielddef,regsize,fref,reg);
  3559. end;
  3560. procedure thlcgobj.g_force_field_reg_by_name(list: TAsmList; recdef: trecorddef; const name: TIDString; const recref: treference; out regdef: tdef; out reg: tregister);
  3561. var
  3562. fref: treference;
  3563. begin
  3564. g_setup_load_field_by_name(list,recdef,name,recref,fref,regdef);
  3565. reg:=getregisterfordef(list,regdef);
  3566. a_load_ref_reg(list,regdef,regdef,fref,reg);
  3567. end;
  3568. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  3569. var
  3570. hregister,
  3571. hregister2: tregister;
  3572. hl : tasmlabel;
  3573. oldloc : tlocation;
  3574. begin
  3575. oldloc:=l;
  3576. hregister:=getregisterfordef(list,dst_size);
  3577. { load value in new register }
  3578. case l.loc of
  3579. {$ifdef cpuflags}
  3580. LOC_FLAGS :
  3581. begin
  3582. g_flags2reg(list,dst_size,l.resflags,hregister);
  3583. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3584. end;
  3585. {$endif cpuflags}
  3586. LOC_JUMP :
  3587. begin
  3588. a_label(list,l.truelabel);
  3589. if is_cbool(src_size) then
  3590. a_load_const_reg(list,dst_size,-1,hregister)
  3591. else
  3592. a_load_const_reg(list,dst_size,1,hregister);
  3593. current_asmdata.getjumplabel(hl);
  3594. a_jmp_always(list,hl);
  3595. a_label(list,l.falselabel);
  3596. a_load_const_reg(list,dst_size,0,hregister);
  3597. a_label(list,hl);
  3598. end;
  3599. else
  3600. begin
  3601. { load_loc_reg can only handle size >= l.size, when the
  3602. new size is smaller then we need to adjust the size
  3603. of the orignal and maybe recalculate l.register for i386 }
  3604. if (dst_size.size<src_size.size) then
  3605. begin
  3606. hregister2:=getregisterfordef(list,src_size);
  3607. { prevent problems with memory locations -- at this high
  3608. level we cannot twiddle with the reference offset, since
  3609. that may not mean anything (e.g., it refers to fixed-sized
  3610. stack slots on Java) }
  3611. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  3612. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  3613. end
  3614. else
  3615. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  3616. end;
  3617. end;
  3618. if (l.loc <> LOC_CREGISTER) or
  3619. not maybeconst then
  3620. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  3621. else
  3622. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  3623. l.register:=hregister;
  3624. { Release temp if it was a reference }
  3625. if oldloc.loc=LOC_REFERENCE then
  3626. location_freetemp(list,oldloc);
  3627. end;
  3628. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  3629. var
  3630. reg : tregister;
  3631. begin
  3632. if (l.loc<>LOC_FPUREGISTER) and
  3633. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  3634. begin
  3635. { if it's in an mm register, store to memory first }
  3636. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  3637. location_force_mem(list,l,size);
  3638. reg:=getfpuregister(list,size);
  3639. a_loadfpu_loc_reg(list,size,size,l,reg);
  3640. location_freetemp(list,l);
  3641. location_reset(l,LOC_FPUREGISTER,l.size);
  3642. l.register:=reg;
  3643. end;
  3644. end;
  3645. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  3646. var
  3647. r : treference;
  3648. forcesize: aint;
  3649. begin
  3650. case l.loc of
  3651. LOC_FPUREGISTER,
  3652. LOC_CFPUREGISTER :
  3653. begin
  3654. tg.gethltemp(list,size,size.size,tt_normal,r);
  3655. a_loadfpu_reg_ref(list,size,size,l.register,r);
  3656. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3657. l.reference:=r;
  3658. end;
  3659. LOC_MMREGISTER,
  3660. LOC_CMMREGISTER:
  3661. begin
  3662. { vectors can't be represented yet using tdef }
  3663. if size.typ<>floatdef then
  3664. internalerror(2012062301);
  3665. tg.gethltemp(list,size,size.size,tt_normal,r);
  3666. a_loadmm_reg_ref(list,size,size,l.register,r,mms_movescalar);
  3667. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3668. l.reference:=r;
  3669. end;
  3670. LOC_CONSTANT,
  3671. LOC_REGISTER,
  3672. LOC_CREGISTER,
  3673. LOC_SUBSETREG,
  3674. LOC_CSUBSETREG,
  3675. LOC_SUBSETREF,
  3676. LOC_CSUBSETREF:
  3677. begin
  3678. if not is_dynamic_array(size) and
  3679. not is_open_array(size) then
  3680. forcesize:=size.size
  3681. else
  3682. forcesize:=sizeof(pint);
  3683. tg.gethltemp(list,size,forcesize,tt_normal,r);
  3684. a_load_loc_ref(list,size,size,l,r);
  3685. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3686. l.reference:=r;
  3687. end;
  3688. LOC_CREFERENCE,
  3689. LOC_REFERENCE : ;
  3690. else
  3691. internalerror(2011010304);
  3692. end;
  3693. end;
  3694. procedure thlcgobj.location_force_mmregscalar(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  3695. var
  3696. reg : tregister;
  3697. href : treference;
  3698. newsize : tdef;
  3699. begin
  3700. if (l.loc<>LOC_MMREGISTER) and
  3701. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  3702. begin
  3703. { if it's in an fpu register, store to memory first }
  3704. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  3705. begin
  3706. tg.gethltemp(list,size,-1,tt_normal,href);
  3707. hlcg.a_loadfpu_reg_ref(list,size,size,l.register,href);
  3708. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3709. l.reference:=href;
  3710. end;
  3711. { on ARM, CFP values may be located in integer registers,
  3712. and its second_int_to_real() also uses this routine to
  3713. force integer (memory) values in an mmregister }
  3714. if (l.size in [OS_32,OS_S32]) then
  3715. begin
  3716. size:=cgsize_orddef(l.size);
  3717. newsize:=s32floattype;
  3718. end
  3719. else if (l.size in [OS_64,OS_S64]) then
  3720. begin
  3721. size:=cgsize_orddef(l.size);
  3722. newsize:=s64floattype;
  3723. end
  3724. else
  3725. newsize:=size;
  3726. case size.size of
  3727. 4:
  3728. newsize:=s32floattype;
  3729. 8:
  3730. newsize:=s64floattype;
  3731. else
  3732. newsize:=size;
  3733. end;
  3734. reg:=hlcg.getmmregister(list,newsize);
  3735. hlcg.a_loadmm_loc_reg(list,size,newsize,l,reg,mms_movescalar);
  3736. l.size:=def_cgsize(newsize);
  3737. location_freetemp(list,l);
  3738. location_reset(l,LOC_MMREGISTER,l.size);
  3739. l.register:=reg;
  3740. end;
  3741. end;
  3742. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  3743. var
  3744. pdef: tdef;
  3745. begin
  3746. case l.loc of
  3747. LOC_REGISTER,
  3748. LOC_CREGISTER :
  3749. begin
  3750. if not loadref then
  3751. internalerror(200410231);
  3752. reference_reset_base(ref,cpointerdef.getreusable(def),l.register,0,alignment);
  3753. end;
  3754. LOC_REFERENCE,
  3755. LOC_CREFERENCE :
  3756. begin
  3757. if loadref then
  3758. begin
  3759. pdef:=cpointerdef.getreusable(def);
  3760. reference_reset_base(ref,pdef,getaddressregister(list,voidpointertype),0,alignment);
  3761. { it's a pointer to def }
  3762. a_load_ref_reg(list,pdef,pdef,l.reference,ref.base);
  3763. end
  3764. else
  3765. ref:=l.reference;
  3766. end;
  3767. else
  3768. internalerror(200309181);
  3769. end;
  3770. end;
  3771. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  3772. var
  3773. truelabel,
  3774. falselabel: tasmlabel;
  3775. begin
  3776. if p.location.loc<>LOC_JUMP then
  3777. begin
  3778. current_asmdata.getjumplabel(truelabel);
  3779. current_asmdata.getjumplabel(falselabel);
  3780. end
  3781. else
  3782. begin
  3783. truelabel:=p.location.truelabel;
  3784. falselabel:=p.location.falselabel;
  3785. end;
  3786. maketojumpboollabels(list,p,truelabel,falselabel);
  3787. end;
  3788. procedure thlcgobj.maketojumpboollabels(list: TAsmList; p: tnode; truelabel, falselabel: tasmlabel);
  3789. var
  3790. storepos : tfileposinfo;
  3791. begin
  3792. if nf_error in p.flags then
  3793. exit;
  3794. storepos:=current_filepos;
  3795. current_filepos:=p.fileinfo;
  3796. if is_boolean(p.resultdef) then
  3797. begin
  3798. if is_constboolnode(p) then
  3799. begin
  3800. if Tordconstnode(p).value.uvalue<>0 then
  3801. a_jmp_always(list,truelabel)
  3802. else
  3803. a_jmp_always(list,falselabel)
  3804. end
  3805. else
  3806. begin
  3807. case p.location.loc of
  3808. LOC_SUBSETREG,LOC_CSUBSETREG,
  3809. LOC_SUBSETREF,LOC_CSUBSETREF,
  3810. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  3811. begin
  3812. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,truelabel);
  3813. a_jmp_always(list,falselabel);
  3814. end;
  3815. LOC_JUMP:
  3816. begin
  3817. if truelabel<>p.location.truelabel then
  3818. begin
  3819. a_label(list,p.location.truelabel);
  3820. a_jmp_always(list,truelabel);
  3821. end;
  3822. if falselabel<>p.location.falselabel then
  3823. begin
  3824. a_label(list,p.location.falselabel);
  3825. a_jmp_always(list,falselabel);
  3826. end;
  3827. end;
  3828. {$ifdef cpuflags}
  3829. LOC_FLAGS :
  3830. begin
  3831. a_jmp_flags(list,p.location.resflags,truelabel);
  3832. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3833. a_jmp_always(list,falselabel);
  3834. end;
  3835. {$endif cpuflags}
  3836. else
  3837. begin
  3838. printnode(output,p);
  3839. internalerror(2011010418);
  3840. end;
  3841. end;
  3842. end;
  3843. location_reset_jump(p.location,truelabel,falselabel);
  3844. end
  3845. else
  3846. internalerror(2011010419);
  3847. current_filepos:=storepos;
  3848. end;
  3849. procedure thlcgobj.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  3850. var
  3851. rr: treplaceregrec;
  3852. varloc : tai_varloc;
  3853. begin
  3854. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  3855. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  3856. exit;
  3857. rr.old := n.location.register;
  3858. rr.ressym := nil;
  3859. rr.sym := nil;
  3860. rr.oldhi := NR_NO;
  3861. case n.location.loc of
  3862. LOC_CREGISTER:
  3863. begin
  3864. {$ifdef cpu64bitalu}
  3865. if (n.location.size in [OS_128,OS_S128]) then
  3866. begin
  3867. rr.oldhi := n.location.register128.reghi;
  3868. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  3869. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  3870. end
  3871. else
  3872. {$else cpu64bitalu}
  3873. if (n.location.size in [OS_64,OS_S64]) then
  3874. begin
  3875. rr.oldhi := n.location.register64.reghi;
  3876. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  3877. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  3878. end
  3879. else
  3880. {$endif cpu64bitalu}
  3881. if getregtype(rr.old)=R_ADDRESSREGISTER then
  3882. rr.new := hlcg.getaddressregister(current_asmdata.CurrAsmList,n.resultdef)
  3883. else
  3884. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  3885. end;
  3886. LOC_CFPUREGISTER:
  3887. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  3888. {$ifdef SUPPORT_MMX}
  3889. LOC_CMMXREGISTER:
  3890. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  3891. {$endif SUPPORT_MMX}
  3892. LOC_CMMREGISTER:
  3893. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  3894. else
  3895. exit;
  3896. end;
  3897. { self is implicitly returned from constructors, even if there are no
  3898. references to it; additionally, funcretsym is not set for constructor
  3899. procdefs }
  3900. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  3901. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  3902. else if not is_void(current_procinfo.procdef.returndef) and
  3903. assigned(current_procinfo.procdef.funcretsym) and
  3904. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  3905. rr.ressym:=current_procinfo.procdef.funcretsym;
  3906. if not foreachnode(n,@do_replace_node_regs,@rr) then
  3907. exit;
  3908. if reload then
  3909. case n.location.loc of
  3910. LOC_CREGISTER:
  3911. begin
  3912. {$ifdef cpu64bitalu}
  3913. if (n.location.size in [OS_128,OS_S128]) then
  3914. cg128.a_load128_reg_reg(list,n.location.register128,joinreg128(rr.new,rr.newhi))
  3915. else
  3916. {$else cpu64bitalu}
  3917. if (n.location.size in [OS_64,OS_S64]) then
  3918. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  3919. else
  3920. {$endif cpu64bitalu}
  3921. a_load_reg_reg(list,n.resultdef,n.resultdef,n.location.register,rr.new);
  3922. end;
  3923. LOC_CFPUREGISTER:
  3924. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  3925. {$ifdef SUPPORT_MMX}
  3926. LOC_CMMXREGISTER:
  3927. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  3928. {$endif SUPPORT_MMX}
  3929. LOC_CMMREGISTER:
  3930. a_loadmm_reg_reg(list,n.resultdef,n.resultdef,n.location.register,rr.new,nil);
  3931. else
  3932. internalerror(2006090920);
  3933. end;
  3934. { now that we've change the loadn/temp, also change the node result location }
  3935. {$ifdef cpu64bitalu}
  3936. if (n.location.size in [OS_128,OS_S128]) then
  3937. begin
  3938. n.location.register128.reglo := rr.new;
  3939. n.location.register128.reghi := rr.newhi;
  3940. if assigned(rr.sym) then
  3941. begin
  3942. varloc:=tai_varloc.create128(rr.sym,rr.new,rr.newhi);
  3943. list.concat(varloc);
  3944. end;
  3945. end
  3946. else
  3947. {$else cpu64bitalu}
  3948. if (n.location.size in [OS_64,OS_S64]) then
  3949. begin
  3950. n.location.register64.reglo := rr.new;
  3951. n.location.register64.reghi := rr.newhi;
  3952. if assigned(rr.sym) then
  3953. begin
  3954. varloc:=tai_varloc.create64(rr.sym,rr.new,rr.newhi);
  3955. list.concat(varloc);
  3956. end;
  3957. end
  3958. else
  3959. {$endif cpu64bitalu}
  3960. begin
  3961. n.location.register := rr.new;
  3962. if assigned(rr.sym) then
  3963. begin
  3964. varloc:=tai_varloc.create(rr.sym,rr.new);
  3965. list.concat(varloc);
  3966. end;
  3967. end;
  3968. end;
  3969. function thlcgobj.do_replace_node_regs(var n: tnode; para: pointer): foreachnoderesult;
  3970. var
  3971. rr: preplaceregrec absolute para;
  3972. begin
  3973. result := fen_false;
  3974. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  3975. exit;
  3976. case n.nodetype of
  3977. loadn:
  3978. begin
  3979. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  3980. (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  3981. not assigned(tloadnode(n).left) and
  3982. ((tloadnode(n).symtableentry <> rr^.ressym) or
  3983. not(fc_exit in flowcontrol)
  3984. ) and
  3985. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  3986. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  3987. begin
  3988. {$ifdef cpu64bitalu}
  3989. { it's possible a 128 bit location was shifted and/xor typecasted }
  3990. { in a 64 bit value, so only 1 register was left in the location }
  3991. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_128,OS_S128]) then
  3992. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi = rr^.oldhi) then
  3993. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register128.reghi := rr^.newhi
  3994. else
  3995. exit;
  3996. {$else cpu64bitalu}
  3997. { it's possible a 64 bit location was shifted and/xor typecasted }
  3998. { in a 32 bit value, so only 1 register was left in the location }
  3999. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  4000. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  4001. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  4002. else
  4003. exit;
  4004. {$endif cpu64bitalu}
  4005. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  4006. rr^.sym := tabstractnormalvarsym(tloadnode(n).symtableentry);
  4007. result := fen_norecurse_true;
  4008. end;
  4009. end;
  4010. temprefn:
  4011. begin
  4012. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  4013. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  4014. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  4015. begin
  4016. {$ifdef cpu64bitalu}
  4017. { it's possible a 128 bit location was shifted and/xor typecasted }
  4018. { in a 64 bit value, so only 1 register was left in the location }
  4019. if (ttemprefnode(n).tempinfo^.location.size in [OS_128,OS_S128]) then
  4020. if (ttemprefnode(n).tempinfo^.location.register128.reghi = rr^.oldhi) then
  4021. ttemprefnode(n).tempinfo^.location.register128.reghi := rr^.newhi
  4022. else
  4023. exit;
  4024. {$else cpu64bitalu}
  4025. { it's possible a 64 bit location was shifted and/xor typecasted }
  4026. { in a 32 bit value, so only 1 register was left in the location }
  4027. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  4028. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  4029. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  4030. else
  4031. exit;
  4032. {$endif cpu64bitalu}
  4033. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  4034. result := fen_norecurse_true;
  4035. end;
  4036. end;
  4037. { optimize the searching a bit }
  4038. derefn,addrn,
  4039. calln,inlinen,casen,
  4040. addn,subn,muln,
  4041. andn,orn,xorn,
  4042. ltn,lten,gtn,gten,equaln,unequaln,
  4043. slashn,divn,shrn,shln,notn,
  4044. inn,
  4045. asn,isn:
  4046. result := fen_norecurse_false;
  4047. end;
  4048. end;
  4049. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  4050. var
  4051. item,
  4052. previtem : TCmdStrListItem;
  4053. begin
  4054. previtem:=nil;
  4055. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  4056. while assigned(item) do
  4057. begin
  4058. {$ifdef arm}
  4059. if GenerateThumbCode or GenerateThumb2Code then
  4060. list.concat(tai_directive.create(asd_thumb_func,''));
  4061. {$endif arm}
  4062. { "double link" all procedure entry symbols via .reference }
  4063. { directives on darwin, because otherwise the linker }
  4064. { sometimes strips the procedure if only on of the symbols }
  4065. { is referenced }
  4066. if assigned(previtem) and
  4067. (target_info.system in systems_darwin) then
  4068. list.concat(tai_directive.create(asd_reference,item.str));
  4069. if (cs_profile in current_settings.moduleswitches) or
  4070. { smart linking using a library requires to promote
  4071. all non-nested procedures to AB_GLOBAL
  4072. otherwise you get undefined symbol error at linking
  4073. for msdos target with -CX option for instance }
  4074. (create_smartlink_library and not is_nested_pd(current_procinfo.procdef)) or
  4075. (po_global in current_procinfo.procdef.procoptions) then
  4076. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  4077. else
  4078. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  4079. if assigned(previtem) and
  4080. (target_info.system in systems_darwin) then
  4081. list.concat(tai_directive.create(asd_reference,previtem.str));
  4082. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  4083. list.concat(Tai_function_name.create(item.str));
  4084. previtem:=item;
  4085. item := TCmdStrListItem(item.next);
  4086. end;
  4087. current_procinfo.procdef.procstarttai:=tai(list.last);
  4088. end;
  4089. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  4090. var
  4091. initname : tsymstr;
  4092. begin
  4093. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  4094. current_procinfo.procdef.procendtai:=tai(list.last);
  4095. if (current_module.islibrary) then
  4096. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  4097. begin
  4098. { ToDo: systems that use indirect entry info, but check back with Windows! }
  4099. if target_info.system in systems_darwin then
  4100. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain }
  4101. initname:=target_info.cprefix+'FPC_LIBMAIN'
  4102. else
  4103. initname:=current_procinfo.procdef.mangledname;
  4104. { setinitname may generate a new section -> don't add to the
  4105. current list, because we assume this remains a text section }
  4106. exportlib.setinitname(current_asmdata.AsmLists[al_exports],initname);
  4107. end;
  4108. end;
  4109. procedure thlcgobj.handle_external_proc(list: TAsmList; pd: tprocdef; const importname: TSymStr);
  4110. begin
  4111. { External declared in implementation, and there was already a
  4112. forward (or interface) declaration then we need to generate
  4113. a stub that calls the external routine }
  4114. if (not pd.forwarddef) and
  4115. (pd.hasforward) then
  4116. begin
  4117. if importname<>'' then
  4118. begin
  4119. { add the procedure to the al_procedures }
  4120. maybe_new_object_file(list);
  4121. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  4122. if (po_global in pd.procoptions) then
  4123. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  4124. else
  4125. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  4126. g_external_wrapper(list,pd,importname);
  4127. end;
  4128. { remove the external stuff, so that the interface crc
  4129. doesn't change. This makes the function calls less
  4130. efficient, but it means that the interface doesn't
  4131. change if the function is ever redirected to another
  4132. function or implemented in the unit. }
  4133. pd.procoptions:=pd.procoptions-[po_external,po_has_importname,po_has_importdll];
  4134. stringdispose(pd.import_name);
  4135. stringdispose(pd.import_dll);
  4136. pd.import_nr:=0;
  4137. end;
  4138. end;
  4139. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  4140. begin
  4141. { initialize register variables }
  4142. case current_procinfo.procdef.proctypeoption of
  4143. potype_unitinit:
  4144. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  4145. potype_proginit:
  4146. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  4147. end;
  4148. { initialises temp. ansi/wide string data }
  4149. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  4150. inittempvariables(list);
  4151. {$ifdef OLDREGVARS}
  4152. load_regvars(list,nil);
  4153. {$endif OLDREGVARS}
  4154. end;
  4155. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  4156. var
  4157. old_current_procinfo: tprocinfo;
  4158. begin
  4159. old_current_procinfo:=current_procinfo;
  4160. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  4161. begin
  4162. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  4163. exit;
  4164. current_procinfo:=current_procinfo.parent;
  4165. end;
  4166. {$ifdef OLDREGVARS}
  4167. cleanup_regvars(list);
  4168. {$endif OLDREGVARS}
  4169. { finalize temporary data }
  4170. finalizetempvariables(list);
  4171. { finalize paras data }
  4172. if assigned(current_procinfo.procdef.parast) and
  4173. not(po_assembler in current_procinfo.procdef.procoptions) then
  4174. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  4175. current_procinfo:=old_current_procinfo;
  4176. end;
  4177. procedure thlcgobj.varsym_set_localloc(list: TAsmList; vs: tabstractnormalvarsym);
  4178. begin
  4179. if cs_asm_source in current_settings.globalswitches then
  4180. begin
  4181. case vs.initialloc.loc of
  4182. LOC_REFERENCE :
  4183. begin
  4184. if not assigned(vs.initialloc.reference.symbol) then
  4185. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  4186. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset)+
  4187. ', size='+tcgsize2str(vs.initialloc.size))));
  4188. end;
  4189. end;
  4190. end;
  4191. vs.localloc:=vs.initialloc;
  4192. end;
  4193. procedure thlcgobj.paravarsym_set_initialloc_to_paraloc(vs: tparavarsym);
  4194. begin
  4195. reference_reset_base(vs.initialloc.reference,voidstackpointertype,tparavarsym(vs).paraloc[calleeside].location^.reference.index,
  4196. tparavarsym(vs).paraloc[calleeside].location^.reference.offset,tparavarsym(vs).paraloc[calleeside].alignment);
  4197. end;
  4198. procedure thlcgobj.gen_entry_code(list: TAsmList);
  4199. begin
  4200. { the actual profile code can clobber some registers,
  4201. therefore if the context must be saved, do it before
  4202. the actual call to the profile code
  4203. }
  4204. if (cs_profile in current_settings.moduleswitches) and
  4205. not(po_assembler in current_procinfo.procdef.procoptions) then
  4206. begin
  4207. { non-win32 can call mcout even in main }
  4208. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  4209. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  4210. begin
  4211. g_profilecode(list);
  4212. end;
  4213. end;
  4214. { call startup helpers from main program }
  4215. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  4216. begin
  4217. { initialize units }
  4218. if not(current_module.islibrary) then
  4219. {$ifdef AVR}
  4220. cg.a_call_name(list,'FPC_INIT_FUNC_TABLE',false)
  4221. {$else AVR}
  4222. g_call_system_proc(list,'fpc_initializeunits',[],nil)
  4223. {$endif AVR}
  4224. else
  4225. g_call_system_proc(list,'fpc_libinitializeunits',[],nil);
  4226. end;
  4227. list.concat(Tai_force_line.Create);
  4228. {$ifdef OLDREGVARS}
  4229. load_regvars(list,nil);
  4230. {$endif OLDREGVARS}
  4231. end;
  4232. procedure thlcgobj.gen_exit_code(list: TAsmList);
  4233. begin
  4234. { TODO: create high level version (create compilerproc in system unit,
  4235. look up procdef, use hlcgobj.a_call_name()) }
  4236. { call __EXIT for main program }
  4237. if (not current_module.islibrary) and
  4238. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  4239. g_call_system_proc(list,'fpc_do_exit',[],nil);
  4240. end;
  4241. procedure thlcgobj.inittempvariables(list: TAsmList);
  4242. var
  4243. hp : ptemprecord;
  4244. href : treference;
  4245. begin
  4246. hp:=tg.templist;
  4247. while assigned(hp) do
  4248. begin
  4249. if hp^.fini and
  4250. assigned(hp^.def) and
  4251. is_managed_type(hp^.def) then
  4252. begin
  4253. temp_to_ref(hp,href);
  4254. g_initialize(list,hp^.def,href);
  4255. end;
  4256. hp:=hp^.next;
  4257. end;
  4258. end;
  4259. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  4260. var
  4261. hp : ptemprecord;
  4262. href : treference;
  4263. begin
  4264. hp:=tg.templist;
  4265. while assigned(hp) do
  4266. begin
  4267. if hp^.fini and
  4268. assigned(hp^.def) and
  4269. is_managed_type(hp^.def) then
  4270. begin
  4271. include(current_procinfo.flags,pi_needs_implicit_finally);
  4272. temp_to_ref(hp,href);
  4273. g_finalize(list,hp^.def,href);
  4274. end;
  4275. hp:=hp^.next;
  4276. end;
  4277. end;
  4278. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  4279. var
  4280. href : treference;
  4281. begin
  4282. if (tsym(p).typ=staticvarsym) and not(tstaticvarsym(p).noregvarinitneeded) then
  4283. begin
  4284. { Static variables can have the initialloc only set to LOC_CxREGISTER
  4285. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  4286. case tstaticvarsym(p).initialloc.loc of
  4287. LOC_CREGISTER :
  4288. begin
  4289. {$ifdef cpu64bitalu}
  4290. if (tstaticvarsym(p).initialloc.size in [OS_128,OS_S128]) then
  4291. cg128.a_load128_const_reg(TAsmList(arg),0,0,tstaticvarsym(p).initialloc.register128)
  4292. else
  4293. {$else cpu64bitalu}
  4294. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  4295. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  4296. else
  4297. {$endif cpu64bitalu}
  4298. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  4299. tstaticvarsym(p).initialloc.register);
  4300. end;
  4301. LOC_CMMREGISTER :
  4302. { clear the whole register }
  4303. a_opmm_reg_reg(TAsmList(arg),OP_XOR,tstaticvarsym(p).vardef,
  4304. tstaticvarsym(p).initialloc.register,
  4305. tstaticvarsym(p).initialloc.register,
  4306. nil);
  4307. LOC_CFPUREGISTER :
  4308. begin
  4309. { initialize fpu regvar by loading from memory }
  4310. reference_reset_symbol(href,
  4311. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  4312. var_align(tstaticvarsym(p).vardef.alignment));
  4313. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  4314. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  4315. end;
  4316. LOC_INVALID :
  4317. ;
  4318. else
  4319. internalerror(200410124);
  4320. end;
  4321. end;
  4322. end;
  4323. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  4324. var
  4325. list : TAsmList;
  4326. href : treference;
  4327. hsym : tparavarsym;
  4328. eldef : tdef;
  4329. highloc : tlocation;
  4330. begin
  4331. if not(tsym(p).typ=paravarsym) then
  4332. exit;
  4333. list:=TAsmList(arg);
  4334. if is_managed_type(tparavarsym(p).vardef) then
  4335. begin
  4336. if (tparavarsym(p).varspez=vs_value) then
  4337. begin
  4338. include(current_procinfo.flags,pi_needs_implicit_finally);
  4339. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,
  4340. is_open_array(tparavarsym(p).vardef) or
  4341. ((target_info.system in systems_caller_copy_addr_value_para) and
  4342. paramanager.push_addr_param(vs_value,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)),
  4343. sizeof(pint));
  4344. if is_open_array(tparavarsym(p).vardef) then
  4345. begin
  4346. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  4347. begin
  4348. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  4349. if not assigned(hsym) then
  4350. internalerror(201003032);
  4351. highloc:=hsym.initialloc
  4352. end
  4353. else
  4354. highloc.loc:=LOC_INVALID;
  4355. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  4356. g_ptrtypecast_ref(list,cpointerdef.getreusable(tparavarsym(p).vardef),cpointerdef.getreusable(eldef),href);
  4357. g_array_rtti_helper(list,eldef,href,highloc,'fpc_finalize_array');
  4358. end
  4359. else
  4360. g_finalize(list,tparavarsym(p).vardef,href);
  4361. end;
  4362. end;
  4363. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  4364. if not(target_info.system in systems_caller_copy_addr_value_para) and
  4365. (tparavarsym(p).varspez=vs_value) and
  4366. (is_open_array(tparavarsym(p).vardef) or
  4367. is_array_of_const(tparavarsym(p).vardef)) then
  4368. begin
  4369. { cdecl functions don't have a high pointer so it is not possible to generate
  4370. a local copy }
  4371. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  4372. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  4373. end;
  4374. end;
  4375. { generates the code for incrementing the reference count of parameters and
  4376. initialize out parameters }
  4377. { generates the code for incrementing the reference count of parameters and
  4378. initialize out parameters }
  4379. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  4380. var
  4381. href : treference;
  4382. hsym : tparavarsym;
  4383. eldef : tdef;
  4384. list : TAsmList;
  4385. highloc : tlocation;
  4386. needs_inittable : boolean;
  4387. begin
  4388. list:=TAsmList(arg);
  4389. if (tsym(p).typ=paravarsym) then
  4390. begin
  4391. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  4392. case tparavarsym(p).varspez of
  4393. vs_value :
  4394. if needs_inittable then
  4395. begin
  4396. { variants are already handled by the call to fpc_variant_copy_overwrite if
  4397. they are passed by reference }
  4398. if not((tparavarsym(p).vardef.typ=variantdef) and
  4399. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  4400. begin
  4401. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,
  4402. is_open_array(tparavarsym(p).vardef) or
  4403. ((target_info.system in systems_caller_copy_addr_value_para) and
  4404. paramanager.push_addr_param(vs_value,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)),
  4405. sizeof(pint));
  4406. if is_open_array(tparavarsym(p).vardef) then
  4407. begin
  4408. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  4409. begin
  4410. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  4411. if not assigned(hsym) then
  4412. internalerror(201003032);
  4413. highloc:=hsym.initialloc
  4414. end
  4415. else
  4416. highloc.loc:=LOC_INVALID;
  4417. { open arrays do not contain correct element count in their rtti,
  4418. the actual count must be passed separately. }
  4419. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  4420. g_ptrtypecast_ref(list,cpointerdef.getreusable(tparavarsym(p).vardef),cpointerdef.getreusable(eldef),href);
  4421. g_array_rtti_helper(list,eldef,href,highloc,'fpc_addref_array');
  4422. end
  4423. else
  4424. g_incrrefcount(list,tparavarsym(p).vardef,href);
  4425. end;
  4426. end;
  4427. vs_out :
  4428. begin
  4429. if needs_inittable then
  4430. begin
  4431. { we have no idea about the alignment at the callee side,
  4432. and the user also cannot specify "unaligned" here, so
  4433. assume worst case }
  4434. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  4435. if needs_inittable then
  4436. begin
  4437. if is_open_array(tparavarsym(p).vardef) then
  4438. begin
  4439. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  4440. begin
  4441. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  4442. if not assigned(hsym) then
  4443. internalerror(201003032);
  4444. highloc:=hsym.initialloc
  4445. end
  4446. else
  4447. highloc.loc:=LOC_INVALID;
  4448. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  4449. g_ptrtypecast_ref(list,cpointerdef.getreusable(tparavarsym(p).vardef),cpointerdef.getreusable(eldef),href);
  4450. g_array_rtti_helper(list,eldef,href,highloc,'fpc_initialize_array');
  4451. end
  4452. else
  4453. g_initialize(list,tparavarsym(p).vardef,href);
  4454. end;
  4455. end;
  4456. end;
  4457. end;
  4458. end;
  4459. end;
  4460. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  4461. var
  4462. i: longint;
  4463. currpara: tparavarsym;
  4464. begin
  4465. if (po_assembler in current_procinfo.procdef.procoptions) or
  4466. { exceptfilters have a single hidden 'parentfp' parameter, which
  4467. is handled by tcg.g_proc_entry. }
  4468. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  4469. exit;
  4470. { Copy parameters to local references/registers }
  4471. for i:=0 to current_procinfo.procdef.paras.count-1 do
  4472. begin
  4473. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  4474. { don't use currpara.vardef, as this will be wrong in case of
  4475. call-by-reference parameters (it won't contain the pointer) }
  4476. gen_load_cgpara_loc(list,currpara.paraloc[calleeside].def,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  4477. end;
  4478. { generate copies of call by value parameters, must be done before
  4479. the initialization and body is parsed because the refcounts are
  4480. incremented using the local copies }
  4481. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  4482. if not(po_assembler in current_procinfo.procdef.procoptions) then
  4483. begin
  4484. { initialize refcounted paras, and trash others. Needed here
  4485. instead of in gen_initialize_code, because when a reference is
  4486. intialised or trashed while the pointer to that reference is kept
  4487. in a regvar, we add a register move and that one again has to
  4488. come after the parameter loading code as far as the register
  4489. allocator is concerned }
  4490. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  4491. end;
  4492. end;
  4493. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  4494. var
  4495. href : treference;
  4496. hreg : tregister;
  4497. list : TAsmList;
  4498. hsym : tparavarsym;
  4499. l : longint;
  4500. highloc,
  4501. localcopyloc : tlocation;
  4502. begin
  4503. list:=TAsmList(arg);
  4504. if (tsym(p).typ=paravarsym) and
  4505. ((vo_has_local_copy in tparavarsym(p).varoptions) or
  4506. (not(target_info.system in systems_caller_copy_addr_value_para) and
  4507. (is_open_array(tparavarsym(p).vardef) or
  4508. is_array_of_const(tparavarsym(p).vardef)) and
  4509. (tparavarsym(p).varspez=vs_value))) then
  4510. begin
  4511. { we have no idea about the alignment at the caller side }
  4512. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  4513. if is_open_array(tparavarsym(p).vardef) or
  4514. is_array_of_const(tparavarsym(p).vardef) then
  4515. begin
  4516. { cdecl functions don't have a high pointer so it is not possible to generate
  4517. a local copy }
  4518. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  4519. begin
  4520. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  4521. begin
  4522. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  4523. if not assigned(hsym) then
  4524. internalerror(2011020506);
  4525. highloc:=hsym.initialloc
  4526. end
  4527. else
  4528. highloc.loc:=LOC_INVALID;
  4529. hreg:=getaddressregister(list,voidpointertype);
  4530. if not is_packed_array(tparavarsym(p).vardef) then
  4531. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  4532. else
  4533. internalerror(2011020507);
  4534. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  4535. a_load_reg_loc(list,cpointerdef.getreusable(tparavarsym(p).vardef),cpointerdef.getreusable(tparavarsym(p).vardef),hreg,tparavarsym(p).initialloc);
  4536. end;
  4537. end
  4538. else
  4539. begin
  4540. { Allocate space for the local copy }
  4541. l:=tparavarsym(p).getsize;
  4542. localcopyloc.loc:=LOC_REFERENCE;
  4543. localcopyloc.size:=int_cgsize(l);
  4544. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  4545. { Copy data }
  4546. if is_shortstring(tparavarsym(p).vardef) then
  4547. begin
  4548. { this code is only executed before the code for the body and the entry/exit code is generated
  4549. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  4550. }
  4551. include(current_procinfo.flags,pi_do_call);
  4552. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  4553. end
  4554. else if tparavarsym(p).vardef.typ=variantdef then
  4555. begin
  4556. { this code is only executed before the code for the body and the entry/exit code is generated
  4557. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  4558. }
  4559. include(current_procinfo.flags,pi_do_call);
  4560. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  4561. end
  4562. else
  4563. begin
  4564. { pass proper alignment info }
  4565. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  4566. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  4567. end;
  4568. { update localloc of varsym }
  4569. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  4570. tparavarsym(p).localloc:=localcopyloc;
  4571. tparavarsym(p).initialloc:=localcopyloc;
  4572. end;
  4573. end;
  4574. end;
  4575. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  4576. var
  4577. tmploc: tlocation;
  4578. begin
  4579. case l.loc of
  4580. LOC_MMREGISTER,
  4581. LOC_CMMREGISTER:
  4582. case cgpara.location^.loc of
  4583. LOC_REFERENCE,
  4584. LOC_CREFERENCE,
  4585. LOC_MMREGISTER,
  4586. LOC_CMMREGISTER,
  4587. LOC_REGISTER,
  4588. LOC_CREGISTER :
  4589. a_loadmm_reg_cgpara(list,size,l.register,cgpara,mms_movescalar);
  4590. LOC_FPUREGISTER,
  4591. LOC_CFPUREGISTER:
  4592. begin
  4593. tmploc:=l;
  4594. location_force_fpureg(list,tmploc,size,false);
  4595. a_loadfpu_reg_cgpara(list,size,tmploc.register,cgpara);
  4596. end;
  4597. else
  4598. internalerror(200204249);
  4599. end;
  4600. LOC_FPUREGISTER,
  4601. LOC_CFPUREGISTER:
  4602. case cgpara.location^.loc of
  4603. LOC_MMREGISTER,
  4604. LOC_CMMREGISTER:
  4605. begin
  4606. tmploc:=l;
  4607. location_force_mmregscalar(list,tmploc,size,false);
  4608. a_loadmm_reg_cgpara(list,size,tmploc.register,cgpara,mms_movescalar);
  4609. end;
  4610. { Some targets pass floats in normal registers }
  4611. LOC_REGISTER,
  4612. LOC_CREGISTER,
  4613. LOC_REFERENCE,
  4614. LOC_CREFERENCE,
  4615. LOC_FPUREGISTER,
  4616. LOC_CFPUREGISTER:
  4617. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  4618. else
  4619. internalerror(2011010210);
  4620. end;
  4621. LOC_REFERENCE,
  4622. LOC_CREFERENCE:
  4623. case cgpara.location^.loc of
  4624. LOC_MMREGISTER,
  4625. LOC_CMMREGISTER:
  4626. a_loadmm_ref_cgpara(list,size,l.reference,cgpara,mms_movescalar);
  4627. { Some targets pass floats in normal registers }
  4628. LOC_REGISTER,
  4629. LOC_CREGISTER,
  4630. LOC_REFERENCE,
  4631. LOC_CREFERENCE,
  4632. LOC_FPUREGISTER,
  4633. LOC_CFPUREGISTER:
  4634. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  4635. else
  4636. internalerror(2011010211);
  4637. end;
  4638. LOC_REGISTER,
  4639. LOC_CREGISTER :
  4640. a_load_loc_cgpara(list,size,l,cgpara);
  4641. else
  4642. internalerror(2011010212);
  4643. end;
  4644. end;
  4645. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  4646. begin
  4647. { do nothing by default }
  4648. end;
  4649. procedure thlcgobj.gen_load_loc_function_result(list: TAsmList; vardef: tdef; const l: tlocation);
  4650. begin
  4651. gen_load_loc_cgpara(list,vardef,l,current_procinfo.procdef.funcretloc[calleeside]);
  4652. end;
  4653. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  4654. var
  4655. tmploc: tlocation;
  4656. begin
  4657. { Handle Floating point types differently
  4658. This doesn't depend on emulator settings, emulator settings should
  4659. be handled by cpupara }
  4660. if (vardef.typ=floatdef) or
  4661. { some ABIs return certain records in an fpu register }
  4662. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  4663. (assigned(cgpara.location) and
  4664. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  4665. begin
  4666. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  4667. exit;
  4668. end;
  4669. { in case of multiple locations, force the source to memory as only
  4670. a_load_ref_cgpara supports multiple locations }
  4671. if assigned(cgpara.location^.next) and
  4672. not(l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  4673. begin
  4674. tmploc:=l;
  4675. location_force_mem(list,tmploc,vardef);
  4676. a_load_loc_cgpara(list,vardef,tmploc,cgpara);
  4677. location_freetemp(list,tmploc);
  4678. exit;
  4679. end;
  4680. case l.loc of
  4681. LOC_CONSTANT,
  4682. LOC_REGISTER,
  4683. LOC_CREGISTER,
  4684. LOC_REFERENCE,
  4685. LOC_CREFERENCE :
  4686. begin
  4687. a_load_loc_cgpara(list,vardef,l,cgpara);
  4688. end;
  4689. LOC_MMREGISTER,
  4690. LOC_CMMREGISTER:
  4691. begin
  4692. if use_vectorfpu(vardef) then
  4693. a_loadmm_loc_cgpara(list,vardef,l,cgpara,mms_movescalar)
  4694. else
  4695. { no vector support yet }
  4696. internalerror(2012071212);
  4697. {
  4698. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  4699. }
  4700. end;
  4701. else
  4702. internalerror(2011010213);
  4703. end;
  4704. end;
  4705. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  4706. var
  4707. href : treference;
  4708. begin
  4709. para.check_simple_location;
  4710. { skip e.g. empty records }
  4711. if (para.location^.loc = LOC_VOID) then
  4712. exit;
  4713. case destloc.loc of
  4714. LOC_REFERENCE :
  4715. begin
  4716. { If the parameter location is reused we don't need to copy
  4717. anything }
  4718. if not reusepara then
  4719. begin
  4720. case para.location^.loc of
  4721. LOC_REFERENCE,LOC_CREFERENCE:
  4722. begin
  4723. reference_reset_base(href,voidstackpointertype,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  4724. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  4725. end;
  4726. else
  4727. internalerror(2013102301);
  4728. end;
  4729. end;
  4730. end;
  4731. { TODO other possible locations }
  4732. else
  4733. internalerror(2011010308);
  4734. end;
  4735. end;
  4736. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  4737. var
  4738. ressym : tabstractnormalvarsym;
  4739. retdef : tdef;
  4740. begin
  4741. { Is the loading needed? }
  4742. if is_void(current_procinfo.procdef.returndef) or
  4743. (
  4744. (po_assembler in current_procinfo.procdef.procoptions) and
  4745. (not(assigned(current_procinfo.procdef.funcretsym)) or
  4746. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0) or
  4747. (po_nostackframe in current_procinfo.procdef.procoptions))
  4748. ) then
  4749. exit;
  4750. { constructors return self }
  4751. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  4752. begin
  4753. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'));
  4754. retdef:=ressym.vardef;
  4755. { and TP-style constructors return a pointer to self }
  4756. if is_object(ressym.vardef) then
  4757. retdef:=cpointerdef.getreusable(retdef);
  4758. end
  4759. else
  4760. begin
  4761. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  4762. retdef:=ressym.vardef;
  4763. end;
  4764. if (ressym.refs>0) or
  4765. is_managed_type(retdef) then
  4766. begin
  4767. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  4768. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  4769. gen_load_loc_function_result(list,retdef,ressym.localloc);
  4770. end
  4771. else
  4772. gen_load_uninitialized_function_result(list,current_procinfo.procdef,retdef,current_procinfo.procdef.funcretloc[calleeside])
  4773. end;
  4774. procedure thlcgobj.gen_stack_check_size_para(list: TAsmList);
  4775. var
  4776. paraloc1 : tcgpara;
  4777. pd : tprocdef;
  4778. begin
  4779. pd:=search_system_proc('fpc_stackcheck');
  4780. paraloc1.init;
  4781. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  4782. hlcg.a_load_const_cgpara(list,paraloc1.def,current_procinfo.calc_stackframe_size,paraloc1);
  4783. paramanager.freecgpara(list,paraloc1);
  4784. paraloc1.done;
  4785. end;
  4786. procedure thlcgobj.gen_stack_check_call(list: TAsmList);
  4787. var
  4788. paraloc1 : tcgpara;
  4789. pd : tprocdef;
  4790. begin
  4791. pd:=search_system_proc('fpc_stackcheck');
  4792. paraloc1.init;
  4793. { The parameter to fpc_stackcheck is loaded seperately via
  4794. gen_stack_check_size_para() }
  4795. paramanager.getintparaloc(list,pd,1,paraloc1);
  4796. paramanager.freecgpara(list,paraloc1);
  4797. { Call the helper }
  4798. hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  4799. paraloc1.done;
  4800. end;
  4801. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  4802. var
  4803. alt: TAsmListType;
  4804. begin
  4805. if not(po_assembler in pd.procoptions) then
  4806. alt:=al_procedures
  4807. else
  4808. alt:=al_pure_assembler;
  4809. { add the procedure to the al_procedures }
  4810. maybe_new_object_file(current_asmdata.asmlists[alt]);
  4811. new_section(current_asmdata.asmlists[alt],sec_code,lower(pd.mangledname),getprocalign);
  4812. current_asmdata.asmlists[alt].concatlist(code);
  4813. { save local data (casetable) also in the same file }
  4814. if assigned(data) and
  4815. (not data.empty) then
  4816. current_asmdata.asmlists[alt].concatlist(data);
  4817. end;
  4818. function thlcgobj.g_call_system_proc(list: TAsmList; const procname: string; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  4819. var
  4820. pd: tprocdef;
  4821. begin
  4822. pd:=search_system_proc(procname);
  4823. pd.init_paraloc_info(callerside);
  4824. if systemunit<>current_module.globalsymtable then
  4825. current_module.addimportedsym(pd.procsym);
  4826. result:=g_call_system_proc_intern(list,pd,paras,forceresdef);
  4827. end;
  4828. function thlcgobj.g_call_system_proc(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  4829. begin
  4830. { separate non-virtual routine to make it clear that the routine to
  4831. override, if any, is g_call_system_proc_intern (and that none of
  4832. the g_call_system_proc variants should be made virtual) }
  4833. pd.init_paraloc_info(callerside);
  4834. if systemunit<>current_module.globalsymtable then
  4835. current_module.addimportedsym(pd.procsym);
  4836. result:=g_call_system_proc_intern(list,pd,paras,forceresdef);
  4837. end;
  4838. function thlcgobj.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  4839. begin
  4840. allocallcpuregisters(list);
  4841. result:=a_call_name(list,pd,pd.mangledname,paras,forceresdef,false);
  4842. deallocallcpuregisters(list);
  4843. end;
  4844. end.