hlcgobj.pas 231 KB

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