hlcgobj.pas 236 KB

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