hlcgobj.pas 238 KB

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