hlcgobj.pas 226 KB

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