hlcgobj.pas 245 KB

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