hlcgobj.pas 239 KB

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