hlcgobj.pas 228 KB

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