hlcgobj.pas 241 KB

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