2
0

cgcpu.pas 218 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the ARM
  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. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,cg64f32,rgcpu;
  27. type
  28. { tbasecgarm is shared between all arm architectures }
  29. tbasecgarm = class(tcg)
  30. { true, if the next arithmetic operation should modify the flags }
  31. cgsetflags : boolean;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  37. { move instructions }
  38. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  39. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  40. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  41. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  42. { fpu move instructions }
  43. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  44. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  45. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  46. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  47. { comparison operations }
  48. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  49. l : tasmlabel);override;
  50. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  51. procedure a_jmp_name(list : TAsmList;const s : string); override;
  52. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  53. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  54. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  55. procedure g_profilecode(list : TAsmList); override;
  56. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  57. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  58. procedure g_maybe_got_init(list : TAsmList); override;
  59. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  60. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  61. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  62. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  63. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  64. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  65. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  66. procedure g_save_registers(list : TAsmList);override;
  67. procedure g_restore_registers(list : TAsmList);override;
  68. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  69. procedure fixref(list : TAsmList;var ref : treference);
  70. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; virtual;
  71. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  72. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  73. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  74. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  75. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  76. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  77. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); override;
  78. { Transform unsupported methods into Internal errors }
  79. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister); override;
  80. { try to generate optimized 32 Bit multiplication, returns true if successful generated }
  81. function try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  82. { clear out potential overflow bits from 8 or 16 bit operations }
  83. { the upper 24/16 bits of a register after an operation }
  84. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  85. { mla for thumb requires that none of the registers is equal to r13/r15, this method ensures this }
  86. procedure safe_mla(list: TAsmList;op1,op2,op3,op4 : TRegister);
  87. end;
  88. { tcgarm is shared between normal arm and thumb-2 }
  89. tcgarm = class(tbasecgarm)
  90. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  91. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  92. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  93. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  94. size: tcgsize; a: tcgint; src, dst: tregister); override;
  95. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  96. size: tcgsize; src1, src2, dst: tregister); override;
  97. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  98. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  99. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  100. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  101. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  102. {Multiply two 32-bit registers into lo and hi 32-bit registers}
  103. procedure a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister); override;
  104. end;
  105. { normal arm cg }
  106. tarmcgarm = class(tcgarm)
  107. procedure init_register_allocators;override;
  108. procedure done_register_allocators;override;
  109. end;
  110. { 64 bit cg for all arm flavours }
  111. tbasecg64farm = class(tcg64f32)
  112. end;
  113. { tcg64farm is shared between normal arm and thumb-2 }
  114. tcg64farm = class(tbasecg64farm)
  115. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  116. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  117. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  118. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  119. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  120. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  121. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);override;
  122. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);override;
  123. end;
  124. tarmcg64farm = class(tcg64farm)
  125. end;
  126. tthumbcgarm = class(tbasecgarm)
  127. procedure init_register_allocators;override;
  128. procedure done_register_allocators;override;
  129. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);override;
  130. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  131. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,dst: TRegister);override;
  132. procedure a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);override;
  133. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); override;
  134. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  135. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const Ref: treference; reg: tregister);override;
  136. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg: tregister);override;
  137. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  138. function handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference; override;
  139. procedure g_external_wrapper(list : TAsmList; procdef : tprocdef; const externalname : string); override;
  140. end;
  141. tthumbcg64farm = class(tbasecg64farm)
  142. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  143. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  144. end;
  145. tthumb2cgarm = class(tcgarm)
  146. procedure init_register_allocators;override;
  147. procedure done_register_allocators;override;
  148. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  149. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  150. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  151. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  152. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  153. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  154. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  155. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  156. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  157. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; override;
  158. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  159. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  160. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  161. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  162. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  163. end;
  164. tthumb2cg64farm = class(tcg64farm)
  165. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  166. end;
  167. const
  168. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  169. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  170. winstackpagesize = 4096;
  171. function get_fpu_postfix(def : tdef) : toppostfix;
  172. procedure create_codegen;
  173. implementation
  174. uses
  175. globals,verbose,systems,cutils,
  176. aopt,aoptcpu,
  177. fmodule,
  178. symconst,symsym,symtable,
  179. tgobj,
  180. procinfo,cpupi,
  181. paramgr;
  182. function get_fpu_postfix(def : tdef) : toppostfix;
  183. begin
  184. if def.typ=floatdef then
  185. begin
  186. case tfloatdef(def).floattype of
  187. s32real:
  188. result:=PF_S;
  189. s64real:
  190. result:=PF_D;
  191. s80real:
  192. result:=PF_E;
  193. else
  194. internalerror(200401272);
  195. end;
  196. end
  197. else
  198. internalerror(200401271);
  199. end;
  200. procedure tarmcgarm.init_register_allocators;
  201. begin
  202. inherited init_register_allocators;
  203. { currently, we always save R14, so we can use it }
  204. if (target_info.system<>system_arm_darwin) then
  205. begin
  206. if assigned(current_procinfo) and (current_procinfo.framepointer<>NR_R11) then
  207. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  208. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  209. RS_R9,RS_R10,RS_R11,RS_R14],first_int_imreg,[])
  210. else
  211. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  212. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  213. RS_R9,RS_R10,RS_R14],first_int_imreg,[])
  214. end
  215. else
  216. { r7 is not available on Darwin, it's used as frame pointer (always,
  217. for backtrace support -- also in gcc/clang -> R11 can be used).
  218. r9 is volatile }
  219. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  220. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R9,RS_R12,RS_R4,RS_R5,RS_R6,RS_R8,
  221. RS_R10,RS_R11,RS_R14],first_int_imreg,[]);
  222. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  223. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  224. { The register allocator currently cannot deal with multiple
  225. non-overlapping subregs per register, so we can only use
  226. half the single precision registers for now (as sub registers of the
  227. double precision ones). }
  228. if current_settings.fputype=fpu_vfpv3 then
  229. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  230. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  231. RS_D16,RS_D17,RS_D18,RS_D19,RS_D20,RS_D21,RS_D22,RS_D23,RS_D24,RS_D25,RS_D26,RS_D27,RS_D28,RS_D29,RS_D30,RS_D31,
  232. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  233. ],first_mm_imreg,[])
  234. else
  235. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  236. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15],first_mm_imreg,[]);
  237. end;
  238. procedure tarmcgarm.done_register_allocators;
  239. begin
  240. rg[R_INTREGISTER].free;
  241. rg[R_FPUREGISTER].free;
  242. rg[R_MMREGISTER].free;
  243. inherited done_register_allocators;
  244. end;
  245. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  246. var
  247. imm_shift : byte;
  248. l : tasmlabel;
  249. hr : treference;
  250. imm1, imm2: DWord;
  251. begin
  252. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  253. internalerror(2002090902);
  254. if is_shifter_const(a,imm_shift) then
  255. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  256. else if is_shifter_const(not(a),imm_shift) then
  257. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  258. { loading of constants with mov and orr }
  259. else if (split_into_shifter_const(a,imm1, imm2)) then
  260. begin
  261. list.concat(taicpu.op_reg_const(A_MOV,reg, imm1));
  262. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg, imm2));
  263. end
  264. { loading of constants with mvn and bic }
  265. else if (split_into_shifter_const(not(a), imm1, imm2)) then
  266. begin
  267. list.concat(taicpu.op_reg_const(A_MVN,reg, imm1));
  268. list.concat(taicpu.op_reg_reg_const(A_BIC,reg,reg, imm2));
  269. end
  270. else
  271. begin
  272. reference_reset(hr,4);
  273. current_asmdata.getjumplabel(l);
  274. cg.a_label(current_procinfo.aktlocaldata,l);
  275. hr.symboldata:=current_procinfo.aktlocaldata.last;
  276. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  277. hr.symbol:=l;
  278. hr.base:=NR_PC;
  279. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  280. end;
  281. end;
  282. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  283. var
  284. oppostfix:toppostfix;
  285. usedtmpref: treference;
  286. tmpreg,tmpreg2 : tregister;
  287. so : tshifterop;
  288. dir : integer;
  289. begin
  290. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  291. FromSize := ToSize;
  292. case FromSize of
  293. { signed integer registers }
  294. OS_8:
  295. oppostfix:=PF_B;
  296. OS_S8:
  297. oppostfix:=PF_SB;
  298. OS_16:
  299. oppostfix:=PF_H;
  300. OS_S16:
  301. oppostfix:=PF_SH;
  302. OS_32,
  303. OS_S32:
  304. oppostfix:=PF_None;
  305. else
  306. InternalError(200308297);
  307. end;
  308. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  309. begin
  310. if target_info.endian=endian_big then
  311. dir:=-1
  312. else
  313. dir:=1;
  314. case FromSize of
  315. OS_16,OS_S16:
  316. begin
  317. { only complicated references need an extra loadaddr }
  318. if assigned(ref.symbol) or
  319. (ref.index<>NR_NO) or
  320. (ref.offset<-4095) or
  321. (ref.offset>4094) or
  322. { sometimes the compiler reused registers }
  323. (reg=ref.index) or
  324. (reg=ref.base) then
  325. begin
  326. tmpreg2:=getintregister(list,OS_INT);
  327. a_loadaddr_ref_reg(list,ref,tmpreg2);
  328. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  329. end
  330. else
  331. usedtmpref:=ref;
  332. if target_info.endian=endian_big then
  333. inc(usedtmpref.offset,1);
  334. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  335. tmpreg:=getintregister(list,OS_INT);
  336. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  337. inc(usedtmpref.offset,dir);
  338. if FromSize=OS_16 then
  339. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  340. else
  341. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  342. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  343. end;
  344. OS_32,OS_S32:
  345. begin
  346. tmpreg:=getintregister(list,OS_INT);
  347. { only complicated references need an extra loadaddr }
  348. if assigned(ref.symbol) or
  349. (ref.index<>NR_NO) or
  350. (ref.offset<-4095) or
  351. (ref.offset>4092) or
  352. { sometimes the compiler reused registers }
  353. (reg=ref.index) or
  354. (reg=ref.base) then
  355. begin
  356. tmpreg2:=getintregister(list,OS_INT);
  357. a_loadaddr_ref_reg(list,ref,tmpreg2);
  358. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  359. end
  360. else
  361. usedtmpref:=ref;
  362. shifterop_reset(so);so.shiftmode:=SM_LSL;
  363. if ref.alignment=2 then
  364. begin
  365. if target_info.endian=endian_big then
  366. inc(usedtmpref.offset,2);
  367. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  368. inc(usedtmpref.offset,dir*2);
  369. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  370. so.shiftimm:=16;
  371. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  372. end
  373. else
  374. begin
  375. tmpreg2:=getintregister(list,OS_INT);
  376. if target_info.endian=endian_big then
  377. inc(usedtmpref.offset,3);
  378. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  379. inc(usedtmpref.offset,dir);
  380. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  381. inc(usedtmpref.offset,dir);
  382. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  383. so.shiftimm:=8;
  384. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  385. inc(usedtmpref.offset,dir);
  386. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  387. so.shiftimm:=16;
  388. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg2,so));
  389. so.shiftimm:=24;
  390. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  391. end;
  392. end
  393. else
  394. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  395. end;
  396. end
  397. else
  398. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  399. if (fromsize=OS_S8) and (tosize = OS_16) then
  400. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  401. end;
  402. procedure tcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  403. var
  404. hsym : tsym;
  405. href : treference;
  406. paraloc : Pcgparalocation;
  407. shift : byte;
  408. begin
  409. { calculate the parameter info for the procdef }
  410. procdef.init_paraloc_info(callerside);
  411. hsym:=tsym(procdef.parast.Find('self'));
  412. if not(assigned(hsym) and
  413. (hsym.typ=paravarsym)) then
  414. internalerror(200305251);
  415. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  416. while paraloc<>nil do
  417. with paraloc^ do
  418. begin
  419. case loc of
  420. LOC_REGISTER:
  421. begin
  422. if is_shifter_const(ioffset,shift) then
  423. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  424. else
  425. begin
  426. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  427. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  428. end;
  429. end;
  430. LOC_REFERENCE:
  431. begin
  432. { offset in the wrapper needs to be adjusted for the stored
  433. return address }
  434. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  435. if is_shifter_const(ioffset,shift) then
  436. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  437. else
  438. begin
  439. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  440. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  441. end;
  442. end
  443. else
  444. internalerror(200309189);
  445. end;
  446. paraloc:=next;
  447. end;
  448. end;
  449. procedure tbasecgarm.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  450. var
  451. ref: treference;
  452. begin
  453. paraloc.check_simple_location;
  454. paramanager.allocparaloc(list,paraloc.location);
  455. case paraloc.location^.loc of
  456. LOC_REGISTER,LOC_CREGISTER:
  457. a_load_const_reg(list,size,a,paraloc.location^.register);
  458. LOC_REFERENCE:
  459. begin
  460. reference_reset(ref,paraloc.alignment);
  461. ref.base:=paraloc.location^.reference.index;
  462. ref.offset:=paraloc.location^.reference.offset;
  463. a_load_const_ref(list,size,a,ref);
  464. end;
  465. else
  466. internalerror(2002081101);
  467. end;
  468. end;
  469. procedure tbasecgarm.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  470. var
  471. tmpref, ref: treference;
  472. location: pcgparalocation;
  473. sizeleft: aint;
  474. begin
  475. location := paraloc.location;
  476. tmpref := r;
  477. sizeleft := paraloc.intsize;
  478. while assigned(location) do
  479. begin
  480. paramanager.allocparaloc(list,location);
  481. case location^.loc of
  482. LOC_REGISTER,LOC_CREGISTER:
  483. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  484. LOC_REFERENCE:
  485. begin
  486. reference_reset_base(ref,location^.reference.index,location^.reference.offset,paraloc.alignment);
  487. { doubles in softemu mode have a strange order of registers and references }
  488. if location^.size=OS_32 then
  489. g_concatcopy(list,tmpref,ref,4)
  490. else
  491. begin
  492. g_concatcopy(list,tmpref,ref,sizeleft);
  493. if assigned(location^.next) then
  494. internalerror(2005010710);
  495. end;
  496. end;
  497. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  498. case location^.size of
  499. OS_F32, OS_F64:
  500. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  501. else
  502. internalerror(2002072801);
  503. end;
  504. LOC_VOID:
  505. begin
  506. // nothing to do
  507. end;
  508. else
  509. internalerror(2002081103);
  510. end;
  511. inc(tmpref.offset,tcgsize2size[location^.size]);
  512. dec(sizeleft,tcgsize2size[location^.size]);
  513. location := location^.next;
  514. end;
  515. end;
  516. procedure tbasecgarm.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  517. var
  518. ref: treference;
  519. tmpreg: tregister;
  520. begin
  521. paraloc.check_simple_location;
  522. paramanager.allocparaloc(list,paraloc.location);
  523. case paraloc.location^.loc of
  524. LOC_REGISTER,LOC_CREGISTER:
  525. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  526. LOC_REFERENCE:
  527. begin
  528. reference_reset(ref,paraloc.alignment);
  529. ref.base := paraloc.location^.reference.index;
  530. ref.offset := paraloc.location^.reference.offset;
  531. tmpreg := getintregister(list,OS_ADDR);
  532. a_loadaddr_ref_reg(list,r,tmpreg);
  533. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  534. end;
  535. else
  536. internalerror(2002080701);
  537. end;
  538. end;
  539. procedure tbasecgarm.a_call_name(list : TAsmList;const s : string; weak: boolean);
  540. var
  541. branchopcode: tasmop;
  542. r : treference;
  543. sym : TAsmSymbol;
  544. begin
  545. { check not really correct: should only be used for non-Thumb cpus }
  546. if (CPUARM_HAS_BLX_LABEL in cpu_capabilities[current_settings.cputype]) and
  547. { WinCE GNU AS (not sure if this applies in general) does not support BLX imm }
  548. (target_info.system<>system_arm_wince) then
  549. branchopcode:=A_BLX
  550. else
  551. branchopcode:=A_BL;
  552. if not(weak) then
  553. sym:=current_asmdata.RefAsmSymbol(s)
  554. else
  555. sym:=current_asmdata.WeakRefAsmSymbol(s);
  556. reference_reset_symbol(r,sym,0,sizeof(pint));
  557. if (tf_pic_uses_got in target_info.flags) and
  558. (cs_create_pic in current_settings.moduleswitches) then
  559. begin
  560. include(current_procinfo.flags,pi_needs_got);
  561. r.refaddr:=addr_pic
  562. end
  563. else
  564. r.refaddr:=addr_full;
  565. list.concat(taicpu.op_ref(branchopcode,r));
  566. {
  567. the compiler does not properly set this flag anymore in pass 1, and
  568. for now we only need it after pass 2 (I hope) (JM)
  569. if not(pi_do_call in current_procinfo.flags) then
  570. internalerror(2003060703);
  571. }
  572. include(current_procinfo.flags,pi_do_call);
  573. end;
  574. procedure tbasecgarm.a_call_reg(list : TAsmList;reg: tregister);
  575. begin
  576. { check not really correct: should only be used for non-Thumb cpus }
  577. if not(CPUARM_HAS_BLX in cpu_capabilities[current_settings.cputype]) then
  578. begin
  579. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  580. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  581. end
  582. else
  583. list.concat(taicpu.op_reg(A_BLX, reg));
  584. {
  585. the compiler does not properly set this flag anymore in pass 1, and
  586. for now we only need it after pass 2 (I hope) (JM)
  587. if not(pi_do_call in current_procinfo.flags) then
  588. internalerror(2003060703);
  589. }
  590. include(current_procinfo.flags,pi_do_call);
  591. end;
  592. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  593. begin
  594. a_op_const_reg_reg(list,op,size,a,reg,reg);
  595. end;
  596. procedure tcgarm.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  597. var
  598. tmpreg,tmpresreg : tregister;
  599. tmpref : treference;
  600. begin
  601. tmpreg:=getintregister(list,size);
  602. tmpresreg:=getintregister(list,size);
  603. tmpref:=a_internal_load_ref_reg(list,size,size,ref,tmpreg);
  604. a_op_const_reg_reg(list,op,size,a,tmpreg,tmpresreg);
  605. a_load_reg_ref(list,size,size,tmpresreg,tmpref);
  606. end;
  607. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  608. var
  609. so : tshifterop;
  610. begin
  611. if op = OP_NEG then
  612. begin
  613. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  614. maybeadjustresult(list,OP_NEG,size,dst);
  615. end
  616. else if op = OP_NOT then
  617. begin
  618. if size in [OS_8, OS_16, OS_S8, OS_S16] then
  619. begin
  620. shifterop_reset(so);
  621. so.shiftmode:=SM_LSL;
  622. if size in [OS_8, OS_S8] then
  623. so.shiftimm:=24
  624. else
  625. so.shiftimm:=16;
  626. list.concat(taicpu.op_reg_reg_shifterop(A_MVN,dst,src,so));
  627. {Using a shift here allows this to be folded into another instruction}
  628. if size in [OS_S8, OS_S16] then
  629. so.shiftmode:=SM_ASR
  630. else
  631. so.shiftmode:=SM_LSR;
  632. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  633. end
  634. else
  635. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  636. end
  637. else
  638. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  639. end;
  640. const
  641. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  642. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  643. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR,A_NONE,A_NONE);
  644. op_reg_opcg2asmop: array[TOpCG] of tasmop =
  645. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  646. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  647. op_reg_postfix: array[TOpCG] of TOpPostfix =
  648. (PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  649. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None);
  650. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  651. size: tcgsize; a: tcgint; src, dst: tregister);
  652. var
  653. ovloc : tlocation;
  654. begin
  655. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  656. end;
  657. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  658. size: tcgsize; src1, src2, dst: tregister);
  659. var
  660. ovloc : tlocation;
  661. begin
  662. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  663. end;
  664. function opshift2shiftmode(op: TOpCg): tshiftmode;
  665. begin
  666. case op of
  667. OP_SHL: Result:=SM_LSL;
  668. OP_SHR: Result:=SM_LSR;
  669. OP_ROR: Result:=SM_ROR;
  670. OP_ROL: Result:=SM_ROR;
  671. OP_SAR: Result:=SM_ASR;
  672. else internalerror(2012070501);
  673. end
  674. end;
  675. function tbasecgarm.try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  676. var
  677. multiplier : dword;
  678. power : longint;
  679. shifterop : tshifterop;
  680. bitsset : byte;
  681. negative : boolean;
  682. first : boolean;
  683. b,
  684. cycles : byte;
  685. maxeffort : byte;
  686. begin
  687. result:=true;
  688. cycles:=0;
  689. negative:=a<0;
  690. shifterop.rs:=NR_NO;
  691. shifterop.shiftmode:=SM_LSL;
  692. if negative then
  693. inc(cycles);
  694. multiplier:=dword(abs(a));
  695. bitsset:=popcnt(multiplier and $fffffffe);
  696. { heuristics to estimate how much instructions are reasonable to replace the mul,
  697. this is currently based on XScale timings }
  698. { in the simplest case, we need a mov to load the constant and a mul to carry out the
  699. actual multiplication, this requires min. 1+4 cycles
  700. because the first shift imm. might cause a stall and because we need more instructions
  701. when replacing the mul we generate max. 3 instructions to replace this mul }
  702. maxeffort:=3;
  703. { if the constant is not a shifter op, we need either some mov/mvn/bic/or sequence or
  704. a ldr, so generating one more operation to replace this is beneficial }
  705. if not(is_shifter_const(dword(a),b)) and not(is_shifter_const(not(dword(a)),b)) then
  706. inc(maxeffort);
  707. { if the upper 5 bits are all set or clear, mul is one cycle faster }
  708. if ((dword(a) and $f8000000)=0) or ((dword(a) and $f8000000)=$f8000000) then
  709. dec(maxeffort);
  710. { if the upper 17 bits are all set or clear, mul is another cycle faster }
  711. if ((dword(a) and $ffff8000)=0) or ((dword(a) and $ffff8000)=$ffff8000) then
  712. dec(maxeffort);
  713. { most simple cases }
  714. if a=1 then
  715. a_load_reg_reg(list,OS_32,OS_32,src,dst)
  716. else if a=0 then
  717. a_load_const_reg(list,OS_32,0,dst)
  718. else if a=-1 then
  719. a_op_reg_reg(list,OP_NEG,OS_32,src,dst)
  720. { add up ?
  721. basically, one add is needed for each bit being set in the constant factor
  722. however, the least significant bit is for free, it can be hidden in the initial
  723. instruction
  724. }
  725. else if (bitsset+cycles<=maxeffort) and
  726. (bitsset<=popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)) then
  727. begin
  728. first:=true;
  729. while multiplier<>0 do
  730. begin
  731. shifterop.shiftimm:=BsrDWord(multiplier);
  732. if odd(multiplier) then
  733. begin
  734. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,shifterop));
  735. dec(multiplier);
  736. end
  737. else
  738. if first then
  739. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  740. else
  741. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,src,shifterop));
  742. first:=false;
  743. dec(multiplier,1 shl shifterop.shiftimm);
  744. end;
  745. if negative then
  746. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  747. end
  748. { subtract from the next greater power of two? }
  749. else if popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)+cycles+1<=maxeffort then
  750. begin
  751. first:=true;
  752. while multiplier<>0 do
  753. begin
  754. if first then
  755. begin
  756. multiplier:=(1 shl power)-multiplier;
  757. shifterop.shiftimm:=power;
  758. end
  759. else
  760. shifterop.shiftimm:=BsrDWord(multiplier);
  761. if odd(multiplier) then
  762. begin
  763. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,shifterop));
  764. dec(multiplier);
  765. end
  766. else
  767. if first then
  768. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  769. else
  770. begin
  771. list.concat(taicpu.op_reg_reg_reg_shifterop(A_SUB,dst,dst,src,shifterop));
  772. dec(multiplier,1 shl shifterop.shiftimm);
  773. end;
  774. first:=false;
  775. end;
  776. if negative then
  777. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  778. end
  779. else
  780. result:=false;
  781. end;
  782. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  783. var
  784. shift, lsb, width : byte;
  785. tmpreg : tregister;
  786. so : tshifterop;
  787. l1 : longint;
  788. imm1, imm2: DWord;
  789. begin
  790. optimize_op_const(size, op, a);
  791. case op of
  792. OP_NONE:
  793. begin
  794. if src <> dst then
  795. a_load_reg_reg(list, size, size, src, dst);
  796. exit;
  797. end;
  798. OP_MOVE:
  799. begin
  800. a_load_const_reg(list, size, a, dst);
  801. exit;
  802. end;
  803. end;
  804. ovloc.loc:=LOC_VOID;
  805. if {$ifopt R+}(a<>-2147483648) and{$endif} not setflags and is_shifter_const(-a,shift) then
  806. case op of
  807. OP_ADD:
  808. begin
  809. op:=OP_SUB;
  810. a:=aint(dword(-a));
  811. end;
  812. OP_SUB:
  813. begin
  814. op:=OP_ADD;
  815. a:=aint(dword(-a));
  816. end
  817. end;
  818. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  819. case op of
  820. OP_NEG,OP_NOT:
  821. internalerror(200308281);
  822. OP_SHL,
  823. OP_SHR,
  824. OP_ROL,
  825. OP_ROR,
  826. OP_SAR:
  827. begin
  828. if a>32 then
  829. internalerror(200308294);
  830. shifterop_reset(so);
  831. so.shiftmode:=opshift2shiftmode(op);
  832. if op = OP_ROL then
  833. so.shiftimm:=32-a
  834. else
  835. so.shiftimm:=a;
  836. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  837. end;
  838. else
  839. {if (op in [OP_SUB, OP_ADD]) and
  840. ((a < 0) or
  841. (a > 4095)) then
  842. begin
  843. tmpreg:=getintregister(list,size);
  844. list.concat(taicpu.op_reg_const(A_MOVT, tmpreg, (a shr 16) and $FFFF));
  845. list.concat(taicpu.op_reg_const(A_MOV, tmpreg, a and $FFFF));
  846. list.concat(setoppostfix(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  847. ));
  848. end
  849. else}
  850. begin
  851. if cgsetflags or setflags then
  852. a_reg_alloc(list,NR_DEFAULTFLAGS);
  853. list.concat(setoppostfix(
  854. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  855. end;
  856. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  857. begin
  858. ovloc.loc:=LOC_FLAGS;
  859. case op of
  860. OP_ADD:
  861. ovloc.resflags:=F_CS;
  862. OP_SUB:
  863. ovloc.resflags:=F_CC;
  864. end;
  865. end;
  866. end
  867. else
  868. begin
  869. { there could be added some more sophisticated optimizations }
  870. if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  871. a_op_reg_reg(list,OP_NEG,size,src,dst)
  872. { we do this here instead in the peephole optimizer because
  873. it saves us a register }
  874. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  875. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  876. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  877. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  878. begin
  879. if l1>32 then{roozbeh does this ever happen?}
  880. internalerror(200308296);
  881. shifterop_reset(so);
  882. so.shiftmode:=SM_LSL;
  883. so.shiftimm:=l1;
  884. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  885. end
  886. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  887. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  888. begin
  889. if l1>32 then{does this ever happen?}
  890. internalerror(201205181);
  891. shifterop_reset(so);
  892. so.shiftmode:=SM_LSL;
  893. so.shiftimm:=l1;
  894. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  895. end
  896. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  897. begin
  898. { nothing to do on success }
  899. end
  900. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  901. broader range of shifterconstants.}
  902. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  903. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  904. { Doing two shifts instead of two bics might allow the peephole optimizer to fold the second shift
  905. into the following instruction}
  906. else if (op = OP_AND) and
  907. is_continuous_mask(a, lsb, width) and
  908. ((lsb = 0) or ((lsb + width) = 32)) then
  909. begin
  910. shifterop_reset(so);
  911. if (width = 16) and
  912. (lsb = 0) and
  913. (current_settings.cputype >= cpu_armv6) then
  914. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  915. else if (width = 8) and
  916. (lsb = 0) and
  917. (current_settings.cputype >= cpu_armv6) then
  918. list.concat(taicpu.op_reg_reg(A_UXTB,dst,src))
  919. else if lsb = 0 then
  920. begin
  921. so.shiftmode:=SM_LSL;
  922. so.shiftimm:=32-width;
  923. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  924. so.shiftmode:=SM_LSR;
  925. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  926. end
  927. else
  928. begin
  929. so.shiftmode:=SM_LSR;
  930. so.shiftimm:=lsb;
  931. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  932. so.shiftmode:=SM_LSL;
  933. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  934. end;
  935. end
  936. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  937. begin
  938. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,imm1));
  939. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  940. end
  941. else if (op in [OP_ADD, OP_SUB, OP_OR, OP_XOR]) and
  942. not(cgsetflags or setflags) and
  943. split_into_shifter_const(a, imm1, imm2) then
  944. begin
  945. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,imm1));
  946. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  947. end
  948. else
  949. begin
  950. tmpreg:=getintregister(list,size);
  951. a_load_const_reg(list,size,a,tmpreg);
  952. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  953. end;
  954. end;
  955. maybeadjustresult(list,op,size,dst);
  956. end;
  957. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  958. var
  959. so : tshifterop;
  960. tmpreg,overflowreg : tregister;
  961. asmop : tasmop;
  962. begin
  963. ovloc.loc:=LOC_VOID;
  964. case op of
  965. OP_NEG,OP_NOT,
  966. OP_DIV,OP_IDIV:
  967. internalerror(200308283);
  968. OP_SHL,
  969. OP_SHR,
  970. OP_SAR,
  971. OP_ROR:
  972. begin
  973. if (op = OP_ROR) and not(size in [OS_32,OS_S32]) then
  974. internalerror(2008072801);
  975. shifterop_reset(so);
  976. so.rs:=src1;
  977. so.shiftmode:=opshift2shiftmode(op);
  978. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  979. end;
  980. OP_ROL:
  981. begin
  982. if not(size in [OS_32,OS_S32]) then
  983. internalerror(2008072801);
  984. { simulate ROL by ror'ing 32-value }
  985. tmpreg:=getintregister(list,OS_32);
  986. list.concat(taicpu.op_reg_reg_const(A_RSB,tmpreg,src1, 32));
  987. shifterop_reset(so);
  988. so.rs:=tmpreg;
  989. so.shiftmode:=SM_ROR;
  990. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  991. end;
  992. OP_IMUL,
  993. OP_MUL:
  994. begin
  995. if cgsetflags or setflags then
  996. begin
  997. overflowreg:=getintregister(list,size);
  998. if op=OP_IMUL then
  999. asmop:=A_SMULL
  1000. else
  1001. asmop:=A_UMULL;
  1002. { the arm doesn't allow that rd and rm are the same }
  1003. if dst=src2 then
  1004. begin
  1005. if dst<>src1 then
  1006. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  1007. else
  1008. begin
  1009. tmpreg:=getintregister(list,size);
  1010. a_load_reg_reg(list,size,size,src2,dst);
  1011. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  1012. end;
  1013. end
  1014. else
  1015. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  1016. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1017. if op=OP_IMUL then
  1018. begin
  1019. shifterop_reset(so);
  1020. so.shiftmode:=SM_ASR;
  1021. so.shiftimm:=31;
  1022. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  1023. end
  1024. else
  1025. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  1026. ovloc.loc:=LOC_FLAGS;
  1027. ovloc.resflags:=F_NE;
  1028. end
  1029. else
  1030. begin
  1031. { the arm doesn't allow that rd and rm are the same }
  1032. if dst=src2 then
  1033. begin
  1034. if dst<>src1 then
  1035. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  1036. else
  1037. begin
  1038. tmpreg:=getintregister(list,size);
  1039. a_load_reg_reg(list,size,size,src2,dst);
  1040. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  1041. end;
  1042. end
  1043. else
  1044. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  1045. end;
  1046. end;
  1047. else
  1048. begin
  1049. if cgsetflags or setflags then
  1050. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1051. list.concat(setoppostfix(
  1052. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  1053. end;
  1054. end;
  1055. maybeadjustresult(list,op,size,dst);
  1056. end;
  1057. procedure tcgarm.a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister);
  1058. var
  1059. asmop: tasmop;
  1060. begin
  1061. list.concat(tai_comment.create(strpnew('tcgarm.a_mul_reg_reg_pair called')));
  1062. case size of
  1063. OS_32: asmop:=A_UMULL;
  1064. OS_S32: asmop:=A_SMULL;
  1065. else
  1066. InternalError(2014060802);
  1067. end;
  1068. { The caller might omit dstlo or dsthi, when he is not interested in it, we still
  1069. need valid registers everywhere. In case of dsthi = NR_NO we could fall back to
  1070. 32x32=32 bit multiplication}
  1071. if (dstlo = NR_NO) then
  1072. dstlo:=getintregister(list,size);
  1073. if (dsthi = NR_NO) then
  1074. dsthi:=getintregister(list,size);
  1075. list.concat(taicpu.op_reg_reg_reg_reg(asmop, dstlo, dsthi, src1,src2));
  1076. end;
  1077. function tbasecgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  1078. var
  1079. tmpreg1,tmpreg2 : tregister;
  1080. tmpref : treference;
  1081. l : tasmlabel;
  1082. begin
  1083. tmpreg1:=NR_NO;
  1084. { Be sure to have a base register }
  1085. if (ref.base=NR_NO) then
  1086. begin
  1087. if ref.shiftmode<>SM_None then
  1088. internalerror(2014020701);
  1089. ref.base:=ref.index;
  1090. ref.index:=NR_NO;
  1091. end;
  1092. { absolute symbols can't be handled directly, we've to store the symbol reference
  1093. in the text segment and access it pc relative
  1094. For now, we assume that references where base or index equals to PC are already
  1095. relative, all other references are assumed to be absolute and thus they need
  1096. to be handled extra.
  1097. A proper solution would be to change refoptions to a set and store the information
  1098. if the symbol is absolute or relative there.
  1099. }
  1100. if (assigned(ref.symbol) and
  1101. not(is_pc(ref.base)) and
  1102. not(is_pc(ref.index))
  1103. ) or
  1104. { [#xxx] isn't a valid address operand }
  1105. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  1106. (ref.offset<-4095) or
  1107. (ref.offset>4095) or
  1108. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  1109. ((ref.offset<-255) or
  1110. (ref.offset>255)
  1111. )
  1112. ) or
  1113. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1114. ((ref.offset<-1020) or
  1115. (ref.offset>1020) or
  1116. ((abs(ref.offset) mod 4)<>0)
  1117. )
  1118. ) or
  1119. ((GenerateThumbCode) and
  1120. (((oppostfix in [PF_SB,PF_SH]) and (ref.offset<>0)) or
  1121. ((oppostfix=PF_None) and ((ref.offset<0) or ((ref.base<>NR_STACK_POINTER_REG) and (ref.offset>124)) or
  1122. ((ref.base=NR_STACK_POINTER_REG) and (ref.offset>1020)) or ((ref.offset mod 4)<>0))) or
  1123. ((oppostfix=PF_H) and ((ref.offset<0) or (ref.offset>62) or ((ref.offset mod 2)<>0) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0)))) or
  1124. ((oppostfix=PF_B) and ((ref.offset<0) or (ref.offset>31) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0))))
  1125. )
  1126. ) then
  1127. begin
  1128. fixref(list,ref);
  1129. end;
  1130. if GenerateThumbCode then
  1131. begin
  1132. { certain thumb load require base and index }
  1133. if (oppostfix in [PF_SB,PF_SH]) and
  1134. (ref.base<>NR_NO) and (ref.index=NR_NO) then
  1135. begin
  1136. tmpreg1:=getintregister(list,OS_ADDR);
  1137. a_load_const_reg(list,OS_ADDR,0,tmpreg1);
  1138. ref.index:=tmpreg1;
  1139. end;
  1140. { "hi" registers cannot be used as base or index }
  1141. if (getsupreg(ref.base) in [RS_R8..RS_R12,RS_R14]) or
  1142. ((ref.base=NR_R13) and (ref.index<>NR_NO)) then
  1143. begin
  1144. tmpreg1:=getintregister(list,OS_ADDR);
  1145. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg1);
  1146. ref.base:=tmpreg1;
  1147. end;
  1148. if getsupreg(ref.index) in [RS_R8..RS_R14] then
  1149. begin
  1150. tmpreg1:=getintregister(list,OS_ADDR);
  1151. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.index,tmpreg1);
  1152. ref.index:=tmpreg1;
  1153. end;
  1154. end;
  1155. { fold if there is base, index and offset, however, don't fold
  1156. for vfp memory instructions because we later fold the index }
  1157. if not((op in [A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1158. (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  1159. begin
  1160. if tmpreg1<>NR_NO then
  1161. begin
  1162. tmpreg2:=getintregister(list,OS_ADDR);
  1163. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg1,tmpreg2);
  1164. tmpreg1:=tmpreg2;
  1165. end
  1166. else
  1167. begin
  1168. tmpreg1:=getintregister(list,OS_ADDR);
  1169. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg1);
  1170. ref.base:=tmpreg1;
  1171. end;
  1172. ref.offset:=0;
  1173. end;
  1174. { floating point operations have only limited references
  1175. we expect here, that a base is already set }
  1176. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  1177. begin
  1178. if ref.shiftmode<>SM_none then
  1179. internalerror(200309121);
  1180. if tmpreg1<>NR_NO then
  1181. begin
  1182. if ref.base=tmpreg1 then
  1183. begin
  1184. if ref.signindex<0 then
  1185. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,tmpreg1,ref.index))
  1186. else
  1187. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,tmpreg1,ref.index));
  1188. ref.index:=NR_NO;
  1189. end
  1190. else
  1191. begin
  1192. if ref.index<>tmpreg1 then
  1193. internalerror(200403161);
  1194. if ref.signindex<0 then
  1195. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,ref.base,tmpreg1))
  1196. else
  1197. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,tmpreg1));
  1198. ref.base:=tmpreg1;
  1199. ref.index:=NR_NO;
  1200. end;
  1201. end
  1202. else
  1203. begin
  1204. tmpreg1:=getintregister(list,OS_ADDR);
  1205. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,ref.index));
  1206. ref.base:=tmpreg1;
  1207. ref.index:=NR_NO;
  1208. end;
  1209. end;
  1210. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  1211. Result := ref;
  1212. end;
  1213. procedure tbasecgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  1214. var
  1215. oppostfix:toppostfix;
  1216. usedtmpref: treference;
  1217. tmpreg : tregister;
  1218. dir : integer;
  1219. begin
  1220. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  1221. FromSize := ToSize;
  1222. case ToSize of
  1223. { signed integer registers }
  1224. OS_8,
  1225. OS_S8:
  1226. oppostfix:=PF_B;
  1227. OS_16,
  1228. OS_S16:
  1229. oppostfix:=PF_H;
  1230. OS_32,
  1231. OS_S32,
  1232. { for vfp value stored in integer register }
  1233. OS_F32:
  1234. oppostfix:=PF_None;
  1235. else
  1236. InternalError(200308299);
  1237. end;
  1238. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[tosize]) then
  1239. begin
  1240. if target_info.endian=endian_big then
  1241. dir:=-1
  1242. else
  1243. dir:=1;
  1244. case FromSize of
  1245. OS_16,OS_S16:
  1246. begin
  1247. tmpreg:=getintregister(list,OS_INT);
  1248. usedtmpref:=ref;
  1249. if target_info.endian=endian_big then
  1250. inc(usedtmpref.offset,1);
  1251. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1252. inc(usedtmpref.offset,dir);
  1253. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1254. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1255. end;
  1256. OS_32,OS_S32:
  1257. begin
  1258. tmpreg:=getintregister(list,OS_INT);
  1259. usedtmpref:=ref;
  1260. if ref.alignment=2 then
  1261. begin
  1262. if target_info.endian=endian_big then
  1263. inc(usedtmpref.offset,2);
  1264. usedtmpref:=a_internal_load_reg_ref(list,OS_16,OS_16,reg,usedtmpref);
  1265. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,reg,tmpreg);
  1266. inc(usedtmpref.offset,dir*2);
  1267. a_internal_load_reg_ref(list,OS_16,OS_16,tmpreg,usedtmpref);
  1268. end
  1269. else
  1270. begin
  1271. if target_info.endian=endian_big then
  1272. inc(usedtmpref.offset,3);
  1273. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1274. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1275. inc(usedtmpref.offset,dir);
  1276. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1277. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1278. inc(usedtmpref.offset,dir);
  1279. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1280. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1281. inc(usedtmpref.offset,dir);
  1282. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1283. end;
  1284. end
  1285. else
  1286. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1287. end;
  1288. end
  1289. else
  1290. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1291. end;
  1292. function tbasecgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  1293. var
  1294. oppostfix:toppostfix;
  1295. begin
  1296. case ToSize of
  1297. { signed integer registers }
  1298. OS_8,
  1299. OS_S8:
  1300. oppostfix:=PF_B;
  1301. OS_16,
  1302. OS_S16:
  1303. oppostfix:=PF_H;
  1304. OS_32,
  1305. OS_S32:
  1306. oppostfix:=PF_None;
  1307. else
  1308. InternalError(2003082910);
  1309. end;
  1310. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  1311. end;
  1312. function tbasecgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  1313. var
  1314. oppostfix:toppostfix;
  1315. begin
  1316. case FromSize of
  1317. { signed integer registers }
  1318. OS_8:
  1319. oppostfix:=PF_B;
  1320. OS_S8:
  1321. oppostfix:=PF_SB;
  1322. OS_16:
  1323. oppostfix:=PF_H;
  1324. OS_S16:
  1325. oppostfix:=PF_SH;
  1326. OS_32,
  1327. OS_S32:
  1328. oppostfix:=PF_None;
  1329. else
  1330. InternalError(200308291);
  1331. end;
  1332. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  1333. end;
  1334. procedure tbasecgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1335. var
  1336. so : tshifterop;
  1337. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  1338. begin
  1339. if GenerateThumbCode then
  1340. begin
  1341. case shiftmode of
  1342. SM_ASR:
  1343. a_op_const_reg_reg(list,OP_SAR,OS_32,shiftimm,reg,reg2);
  1344. SM_LSR:
  1345. a_op_const_reg_reg(list,OP_SHR,OS_32,shiftimm,reg,reg2);
  1346. SM_LSL:
  1347. a_op_const_reg_reg(list,OP_SHL,OS_32,shiftimm,reg,reg2);
  1348. else
  1349. internalerror(2013090301);
  1350. end;
  1351. end
  1352. else
  1353. begin
  1354. so.shiftmode:=shiftmode;
  1355. so.shiftimm:=shiftimm;
  1356. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  1357. end;
  1358. end;
  1359. var
  1360. instr: taicpu;
  1361. conv_done: boolean;
  1362. begin
  1363. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1364. internalerror(2002090901);
  1365. conv_done:=false;
  1366. if tosize<>fromsize then
  1367. begin
  1368. shifterop_reset(so);
  1369. conv_done:=true;
  1370. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1371. fromsize:=tosize;
  1372. if current_settings.cputype<cpu_armv6 then
  1373. case fromsize of
  1374. OS_8:
  1375. if GenerateThumbCode then
  1376. a_op_const_reg_reg(list,OP_AND,OS_32,$ff,reg1,reg2)
  1377. else
  1378. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1379. OS_S8:
  1380. begin
  1381. do_shift(SM_LSL,24,reg1);
  1382. if tosize=OS_16 then
  1383. begin
  1384. do_shift(SM_ASR,8,reg2);
  1385. do_shift(SM_LSR,16,reg2);
  1386. end
  1387. else
  1388. do_shift(SM_ASR,24,reg2);
  1389. end;
  1390. OS_16:
  1391. begin
  1392. do_shift(SM_LSL,16,reg1);
  1393. do_shift(SM_LSR,16,reg2);
  1394. end;
  1395. OS_S16:
  1396. begin
  1397. do_shift(SM_LSL,16,reg1);
  1398. do_shift(SM_ASR,16,reg2)
  1399. end;
  1400. else
  1401. conv_done:=false;
  1402. end
  1403. else
  1404. case fromsize of
  1405. OS_8:
  1406. if GenerateThumbCode then
  1407. list.concat(taicpu.op_reg_reg(A_UXTB,reg2,reg1))
  1408. else
  1409. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1410. OS_S8:
  1411. begin
  1412. if tosize=OS_16 then
  1413. begin
  1414. so.shiftmode:=SM_ROR;
  1415. so.shiftimm:=16;
  1416. list.concat(taicpu.op_reg_reg_shifterop(A_SXTB16,reg2,reg1,so));
  1417. do_shift(SM_LSR,16,reg2);
  1418. end
  1419. else
  1420. list.concat(taicpu.op_reg_reg(A_SXTB,reg2,reg1));
  1421. end;
  1422. OS_16:
  1423. list.concat(taicpu.op_reg_reg(A_UXTH,reg2,reg1));
  1424. OS_S16:
  1425. list.concat(taicpu.op_reg_reg(A_SXTH,reg2,reg1));
  1426. else
  1427. conv_done:=false;
  1428. end
  1429. end;
  1430. if not conv_done and (reg1<>reg2) then
  1431. begin
  1432. { same size, only a register mov required }
  1433. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  1434. list.Concat(instr);
  1435. { Notify the register allocator that we have written a move instruction so
  1436. it can try to eliminate it. }
  1437. add_move_instruction(instr);
  1438. end;
  1439. end;
  1440. procedure tbasecgarm.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  1441. var
  1442. href,href2 : treference;
  1443. hloc : pcgparalocation;
  1444. begin
  1445. href:=ref;
  1446. hloc:=paraloc.location;
  1447. while assigned(hloc) do
  1448. begin
  1449. case hloc^.loc of
  1450. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1451. begin
  1452. paramanager.allocparaloc(list,paraloc.location);
  1453. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  1454. end;
  1455. LOC_REGISTER :
  1456. case hloc^.size of
  1457. OS_32,
  1458. OS_F32:
  1459. begin
  1460. paramanager.allocparaloc(list,paraloc.location);
  1461. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  1462. end;
  1463. OS_64,
  1464. OS_F64:
  1465. cg64.a_load64_ref_cgpara(list,href,paraloc);
  1466. else
  1467. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  1468. end;
  1469. LOC_REFERENCE :
  1470. begin
  1471. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,paraloc.alignment);
  1472. { concatcopy should choose the best way to copy the data }
  1473. g_concatcopy(list,href,href2,tcgsize2size[hloc^.size]);
  1474. end;
  1475. else
  1476. internalerror(200408241);
  1477. end;
  1478. inc(href.offset,tcgsize2size[hloc^.size]);
  1479. hloc:=hloc^.next;
  1480. end;
  1481. end;
  1482. procedure tbasecgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1483. begin
  1484. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  1485. end;
  1486. procedure tbasecgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1487. var
  1488. oppostfix:toppostfix;
  1489. begin
  1490. case fromsize of
  1491. OS_32,
  1492. OS_F32:
  1493. oppostfix:=PF_S;
  1494. OS_64,
  1495. OS_F64:
  1496. oppostfix:=PF_D;
  1497. OS_F80:
  1498. oppostfix:=PF_E;
  1499. else
  1500. InternalError(200309021);
  1501. end;
  1502. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1503. if fromsize<>tosize then
  1504. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  1505. end;
  1506. procedure tbasecgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1507. var
  1508. oppostfix:toppostfix;
  1509. begin
  1510. case tosize of
  1511. OS_F32:
  1512. oppostfix:=PF_S;
  1513. OS_F64:
  1514. oppostfix:=PF_D;
  1515. OS_F80:
  1516. oppostfix:=PF_E;
  1517. else
  1518. InternalError(200309022);
  1519. end;
  1520. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1521. end;
  1522. { comparison operations }
  1523. procedure tbasecgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1524. l : tasmlabel);
  1525. var
  1526. tmpreg : tregister;
  1527. b : byte;
  1528. begin
  1529. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1530. if (not(GenerateThumbCode) and is_shifter_const(a,b)) or
  1531. ((GenerateThumbCode) and is_thumb_imm(a)) then
  1532. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1533. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1534. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1535. else if (a<>$7fffffff) and (a<>-1) and not(GenerateThumbCode) and is_shifter_const(-a,b) then
  1536. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1537. else
  1538. begin
  1539. tmpreg:=getintregister(list,size);
  1540. a_load_const_reg(list,size,a,tmpreg);
  1541. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1542. end;
  1543. a_jmp_cond(list,cmp_op,l);
  1544. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1545. end;
  1546. procedure tbasecgarm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1547. begin
  1548. if reverse then
  1549. begin
  1550. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,src));
  1551. list.Concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,31));
  1552. list.Concat(taicpu.op_reg_reg_const(A_AND,dst,dst,255));
  1553. end
  1554. { it is decided during the compilation of the system unit if this code is used or not
  1555. so no additional check for rbit is needed }
  1556. else
  1557. begin
  1558. list.Concat(taicpu.op_reg_reg(A_RBIT,dst,src));
  1559. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,dst));
  1560. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1561. list.Concat(taicpu.op_reg_const(A_CMP,dst,32));
  1562. if GenerateThumb2Code then
  1563. list.Concat(taicpu.op_cond(A_IT, C_EQ));
  1564. list.Concat(setcondition(taicpu.op_reg_const(A_MOV,dst,$ff),C_EQ));
  1565. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1566. end;
  1567. end;
  1568. procedure tbasecgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1569. begin
  1570. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1571. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1572. a_jmp_cond(list,cmp_op,l);
  1573. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1574. end;
  1575. procedure tbasecgarm.a_jmp_name(list : TAsmList;const s : string);
  1576. var
  1577. ai : taicpu;
  1578. begin
  1579. { generate far jump, leave it to the optimizer to get rid of it }
  1580. if GenerateThumbCode then
  1581. ai:=taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s))
  1582. else
  1583. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1584. ai.is_jmp:=true;
  1585. list.concat(ai);
  1586. end;
  1587. procedure tbasecgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1588. var
  1589. ai : taicpu;
  1590. begin
  1591. { generate far jump, leave it to the optimizer to get rid of it }
  1592. if GenerateThumbCode then
  1593. ai:=taicpu.op_sym(A_BL,l)
  1594. else
  1595. ai:=taicpu.op_sym(A_B,l);
  1596. ai.is_jmp:=true;
  1597. list.concat(ai);
  1598. end;
  1599. procedure tbasecgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1600. var
  1601. ai : taicpu;
  1602. inv_flags : TResFlags;
  1603. hlabel : TAsmLabel;
  1604. begin
  1605. if GenerateThumbCode then
  1606. begin
  1607. inv_flags:=f;
  1608. inverse_flags(inv_flags);
  1609. { the optimizer has to fix this if jump range is sufficient short }
  1610. current_asmdata.getjumplabel(hlabel);
  1611. ai:=setcondition(taicpu.op_sym(A_B,hlabel),flags_to_cond(inv_flags));
  1612. ai.is_jmp:=true;
  1613. list.concat(ai);
  1614. a_jmp_always(list,l);
  1615. a_label(list,hlabel);
  1616. end
  1617. else
  1618. begin
  1619. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1620. ai.is_jmp:=true;
  1621. list.concat(ai);
  1622. end;
  1623. end;
  1624. procedure tbasecgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1625. begin
  1626. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1627. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1628. end;
  1629. procedure tbasecgarm.g_profilecode(list : TAsmList);
  1630. begin
  1631. if target_info.system = system_arm_linux then
  1632. begin
  1633. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R14]));
  1634. a_call_name(list,'__gnu_mcount_nc',false);
  1635. end
  1636. else
  1637. internalerror(2014091201);
  1638. end;
  1639. procedure tbasecgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1640. var
  1641. ref : treference;
  1642. shift : byte;
  1643. firstfloatreg,lastfloatreg,
  1644. r : byte;
  1645. mmregs,
  1646. regs, saveregs : tcpuregisterset;
  1647. registerarea,
  1648. r7offset,
  1649. stackmisalignment : pint;
  1650. postfix: toppostfix;
  1651. imm1, imm2: DWord;
  1652. stack_parameters : Boolean;
  1653. begin
  1654. LocalSize:=align(LocalSize,4);
  1655. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  1656. { call instruction does not put anything on the stack }
  1657. registerarea:=0;
  1658. tarmprocinfo(current_procinfo).stackpaddingreg:=High(TSuperRegister);
  1659. lastfloatreg:=RS_NO;
  1660. if not(nostackframe) then
  1661. begin
  1662. firstfloatreg:=RS_NO;
  1663. mmregs:=[];
  1664. case current_settings.fputype of
  1665. fpu_fpa,
  1666. fpu_fpa10,
  1667. fpu_fpa11:
  1668. begin
  1669. { save floating point registers? }
  1670. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1671. for r:=RS_F0 to RS_F7 do
  1672. if r in regs then
  1673. begin
  1674. if firstfloatreg=RS_NO then
  1675. firstfloatreg:=r;
  1676. lastfloatreg:=r;
  1677. inc(registerarea,12);
  1678. end;
  1679. end;
  1680. fpu_vfpv2,
  1681. fpu_vfpv3,
  1682. fpu_vfpv3_d16:
  1683. begin;
  1684. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1685. end;
  1686. end;
  1687. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1688. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1689. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1690. { save int registers }
  1691. reference_reset(ref,4);
  1692. ref.index:=NR_STACK_POINTER_REG;
  1693. ref.addressmode:=AM_PREINDEXED;
  1694. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1695. if not(target_info.system in systems_darwin) then
  1696. begin
  1697. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1698. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1699. begin
  1700. a_reg_alloc(list,NR_R12);
  1701. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1702. end;
  1703. { the (old) ARM APCS requires saving both the stack pointer (to
  1704. crawl the stack) and the PC (to identify the function this
  1705. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  1706. and R15 -- still needs updating for EABI and Darwin, they don't
  1707. need that }
  1708. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1709. regs:=regs+[RS_FRAME_POINTER_REG,RS_R12,RS_R14,RS_R15]
  1710. else
  1711. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1712. include(regs,RS_R14);
  1713. if regs<>[] then
  1714. begin
  1715. for r:=RS_R0 to RS_R15 do
  1716. if r in regs then
  1717. inc(registerarea,4);
  1718. { if the stack is not 8 byte aligned, try to add an extra register,
  1719. so we can avoid the extra sub/add ...,#4 later (KB) }
  1720. if ((registerarea mod current_settings.alignment.localalignmax) <> 0) then
  1721. for r:=RS_R3 downto RS_R0 do
  1722. if not(r in regs) then
  1723. begin
  1724. regs:=regs+[r];
  1725. inc(registerarea,4);
  1726. tarmprocinfo(current_procinfo).stackpaddingreg:=r;
  1727. break;
  1728. end;
  1729. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1730. end;
  1731. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1732. begin
  1733. { the framepointer now points to the saved R15, so the saved
  1734. framepointer is at R11-12 (for get_caller_frame) }
  1735. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1736. a_reg_dealloc(list,NR_R12);
  1737. end;
  1738. end
  1739. else
  1740. begin
  1741. { always save r14 if we use r7 as the framepointer, because
  1742. the parameter offsets are hardcoded in advance and always
  1743. assume that r14 sits on the stack right behind the saved r7
  1744. }
  1745. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1746. include(regs,RS_FRAME_POINTER_REG);
  1747. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1748. include(regs,RS_R14);
  1749. if regs<>[] then
  1750. begin
  1751. { on Darwin, you first have to save [r4-r7,lr], and then
  1752. [r8,r10,r11] and make r7 point to the previously saved
  1753. r7 so that you can perform a stack crawl based on it
  1754. ([r7] is previous stack frame, [r7+4] is return address
  1755. }
  1756. include(regs,RS_FRAME_POINTER_REG);
  1757. saveregs:=regs-[RS_R8,RS_R10,RS_R11];
  1758. r7offset:=0;
  1759. for r:=RS_R0 to RS_R15 do
  1760. if r in saveregs then
  1761. begin
  1762. inc(registerarea,4);
  1763. if r<RS_FRAME_POINTER_REG then
  1764. inc(r7offset,4);
  1765. end;
  1766. { save the registers }
  1767. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1768. { make r7 point to the saved r7 (regardless of whether this
  1769. frame uses the framepointer, for backtrace purposes) }
  1770. if r7offset<>0 then
  1771. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_FRAME_POINTER_REG,NR_R13,r7offset))
  1772. else
  1773. list.concat(taicpu.op_reg_reg(A_MOV,NR_R7,NR_R13));
  1774. { now save the rest (if any) }
  1775. saveregs:=regs-saveregs;
  1776. if saveregs<>[] then
  1777. begin
  1778. for r:=RS_R8 to RS_R11 do
  1779. if r in saveregs then
  1780. inc(registerarea,4);
  1781. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1782. end;
  1783. end;
  1784. end;
  1785. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  1786. if (LocalSize<>0) or
  1787. ((stackmisalignment<>0) and
  1788. ((pi_do_call in current_procinfo.flags) or
  1789. (po_assembler in current_procinfo.procdef.procoptions))) then
  1790. begin
  1791. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1792. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  1793. begin
  1794. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  1795. internalerror(2014030901)
  1796. else
  1797. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  1798. end;
  1799. if is_shifter_const(localsize,shift) then
  1800. begin
  1801. a_reg_dealloc(list,NR_R12);
  1802. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1803. end
  1804. else if split_into_shifter_const(localsize, imm1, imm2) then
  1805. begin
  1806. a_reg_dealloc(list,NR_R12);
  1807. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1808. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1809. end
  1810. else
  1811. begin
  1812. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1813. a_reg_alloc(list,NR_R12);
  1814. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1815. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1816. a_reg_dealloc(list,NR_R12);
  1817. end;
  1818. end;
  1819. if (mmregs<>[]) or
  1820. (firstfloatreg<>RS_NO) then
  1821. begin
  1822. reference_reset(ref,4);
  1823. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1824. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1825. begin
  1826. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1827. begin
  1828. a_reg_alloc(list,NR_R12);
  1829. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1830. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1831. a_reg_dealloc(list,NR_R12);
  1832. end
  1833. else
  1834. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1835. ref.base:=NR_R12;
  1836. end
  1837. else
  1838. begin
  1839. ref.base:=current_procinfo.framepointer;
  1840. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1841. end;
  1842. case current_settings.fputype of
  1843. fpu_fpa,
  1844. fpu_fpa10,
  1845. fpu_fpa11:
  1846. begin
  1847. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1848. lastfloatreg-firstfloatreg+1,ref));
  1849. end;
  1850. fpu_vfpv2,
  1851. fpu_vfpv3,
  1852. fpu_vfpv3_d16:
  1853. begin
  1854. ref.index:=ref.base;
  1855. ref.base:=NR_NO;
  1856. { FSTMX is deprecated on ARMv6 and later }
  1857. if (current_settings.cputype<cpu_armv6) then
  1858. postfix:=PF_IAX
  1859. else
  1860. postfix:=PF_IAD;
  1861. list.concat(setoppostfix(taicpu.op_ref_regset(A_FSTM,ref,R_MMREGISTER,R_SUBFD,mmregs),postfix));
  1862. end;
  1863. end;
  1864. end;
  1865. end;
  1866. end;
  1867. procedure tbasecgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1868. var
  1869. ref : treference;
  1870. LocalSize : longint;
  1871. firstfloatreg,lastfloatreg,
  1872. r,
  1873. shift : byte;
  1874. mmregs,
  1875. saveregs,
  1876. regs : tcpuregisterset;
  1877. registerarea,
  1878. stackmisalignment: pint;
  1879. paddingreg: TSuperRegister;
  1880. mmpostfix: toppostfix;
  1881. imm1, imm2: DWord;
  1882. begin
  1883. if not(nostackframe) then
  1884. begin
  1885. registerarea:=0;
  1886. firstfloatreg:=RS_NO;
  1887. lastfloatreg:=RS_NO;
  1888. mmregs:=[];
  1889. saveregs:=[];
  1890. case current_settings.fputype of
  1891. fpu_fpa,
  1892. fpu_fpa10,
  1893. fpu_fpa11:
  1894. begin
  1895. { restore floating point registers? }
  1896. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1897. for r:=RS_F0 to RS_F7 do
  1898. if r in regs then
  1899. begin
  1900. if firstfloatreg=RS_NO then
  1901. firstfloatreg:=r;
  1902. lastfloatreg:=r;
  1903. { floating point register space is already included in
  1904. localsize below by calc_stackframe_size
  1905. inc(registerarea,12);
  1906. }
  1907. end;
  1908. end;
  1909. fpu_vfpv2,
  1910. fpu_vfpv3,
  1911. fpu_vfpv3_d16:
  1912. begin;
  1913. { restore vfp registers? }
  1914. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1915. end;
  1916. end;
  1917. if (firstfloatreg<>RS_NO) or
  1918. (mmregs<>[]) then
  1919. begin
  1920. reference_reset(ref,4);
  1921. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1922. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1923. begin
  1924. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1925. begin
  1926. a_reg_alloc(list,NR_R12);
  1927. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1928. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1929. a_reg_dealloc(list,NR_R12);
  1930. end
  1931. else
  1932. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1933. ref.base:=NR_R12;
  1934. end
  1935. else
  1936. begin
  1937. ref.base:=current_procinfo.framepointer;
  1938. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1939. end;
  1940. case current_settings.fputype of
  1941. fpu_fpa,
  1942. fpu_fpa10,
  1943. fpu_fpa11:
  1944. begin
  1945. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1946. lastfloatreg-firstfloatreg+1,ref));
  1947. end;
  1948. fpu_vfpv2,
  1949. fpu_vfpv3,
  1950. fpu_vfpv3_d16:
  1951. begin
  1952. ref.index:=ref.base;
  1953. ref.base:=NR_NO;
  1954. { FLDMX is deprecated on ARMv6 and later }
  1955. if (current_settings.cputype<cpu_armv6) then
  1956. mmpostfix:=PF_IAX
  1957. else
  1958. mmpostfix:=PF_IAD;
  1959. list.concat(setoppostfix(taicpu.op_ref_regset(A_FLDM,ref,R_MMREGISTER,R_SUBFD,mmregs),mmpostfix));
  1960. end;
  1961. end;
  1962. end;
  1963. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1964. if (pi_do_call in current_procinfo.flags) or
  1965. (regs<>[]) or
  1966. ((target_info.system in systems_darwin) and
  1967. (current_procinfo.framepointer<>NR_STACK_POINTER_REG)) then
  1968. begin
  1969. exclude(regs,RS_R14);
  1970. include(regs,RS_R15);
  1971. if (target_info.system in systems_darwin) then
  1972. include(regs,RS_FRAME_POINTER_REG);
  1973. end;
  1974. if not(target_info.system in systems_darwin) then
  1975. begin
  1976. { restore saved stack pointer to SP (R13) and saved lr to PC (R15).
  1977. The saved PC came after that but is discarded, since we restore
  1978. the stack pointer }
  1979. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  1980. regs:=regs+[RS_FRAME_POINTER_REG,RS_R13,RS_R15];
  1981. end
  1982. else
  1983. begin
  1984. { restore R8-R11 already if necessary (they've been stored
  1985. before the others) }
  1986. saveregs:=regs*[RS_R8,RS_R10,RS_R11];
  1987. if saveregs<>[] then
  1988. begin
  1989. reference_reset(ref,4);
  1990. ref.index:=NR_STACK_POINTER_REG;
  1991. ref.addressmode:=AM_PREINDEXED;
  1992. for r:=RS_R8 to RS_R11 do
  1993. if r in saveregs then
  1994. inc(registerarea,4);
  1995. regs:=regs-saveregs;
  1996. end;
  1997. end;
  1998. for r:=RS_R0 to RS_R15 do
  1999. if r in regs then
  2000. inc(registerarea,4);
  2001. { reapply the stack padding reg, in case there was one, see the complimentary
  2002. comment in g_proc_entry() (KB) }
  2003. paddingreg:=tarmprocinfo(current_procinfo).stackpaddingreg;
  2004. if paddingreg < RS_R4 then
  2005. if paddingreg in regs then
  2006. internalerror(201306190)
  2007. else
  2008. begin
  2009. regs:=regs+[paddingreg];
  2010. inc(registerarea,4);
  2011. end;
  2012. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  2013. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  2014. (target_info.system in systems_darwin) then
  2015. begin
  2016. LocalSize:=current_procinfo.calc_stackframe_size;
  2017. if (LocalSize<>0) or
  2018. ((stackmisalignment<>0) and
  2019. ((pi_do_call in current_procinfo.flags) or
  2020. (po_assembler in current_procinfo.procdef.procoptions))) then
  2021. begin
  2022. if pi_estimatestacksize in current_procinfo.flags then
  2023. LocalSize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  2024. else
  2025. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  2026. if is_shifter_const(LocalSize,shift) then
  2027. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  2028. else if split_into_shifter_const(localsize, imm1, imm2) then
  2029. begin
  2030. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  2031. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  2032. end
  2033. else
  2034. begin
  2035. a_reg_alloc(list,NR_R12);
  2036. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  2037. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  2038. a_reg_dealloc(list,NR_R12);
  2039. end;
  2040. end;
  2041. if (target_info.system in systems_darwin) and
  2042. (saveregs<>[]) then
  2043. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  2044. if regs=[] then
  2045. begin
  2046. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2047. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2048. else
  2049. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2050. end
  2051. else
  2052. begin
  2053. reference_reset(ref,4);
  2054. ref.index:=NR_STACK_POINTER_REG;
  2055. ref.addressmode:=AM_PREINDEXED;
  2056. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  2057. end;
  2058. end
  2059. else
  2060. begin
  2061. { restore int registers and return }
  2062. reference_reset(ref,4);
  2063. ref.index:=NR_FRAME_POINTER_REG;
  2064. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_EA));
  2065. end;
  2066. end
  2067. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2068. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2069. else
  2070. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2071. end;
  2072. procedure tbasecgarm.g_maybe_got_init(list : TAsmList);
  2073. var
  2074. ref : treference;
  2075. l : TAsmLabel;
  2076. begin
  2077. if (cs_create_pic in current_settings.moduleswitches) and
  2078. (pi_needs_got in current_procinfo.flags) and
  2079. (tf_pic_uses_got in target_info.flags) then
  2080. begin
  2081. reference_reset(ref,4);
  2082. current_asmdata.getdatalabel(l);
  2083. cg.a_label(current_procinfo.aktlocaldata,l);
  2084. ref.symbol:=l;
  2085. ref.base:=NR_PC;
  2086. ref.symboldata:=current_procinfo.aktlocaldata.last;
  2087. list.concat(Taicpu.op_reg_ref(A_LDR,current_procinfo.got,ref));
  2088. current_asmdata.getaddrlabel(l);
  2089. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_32bit,l,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),-8));
  2090. cg.a_label(list,l);
  2091. list.concat(Taicpu.op_reg_reg_reg(A_ADD,current_procinfo.got,NR_PC,current_procinfo.got));
  2092. end;
  2093. end;
  2094. procedure tbasecgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  2095. var
  2096. b : byte;
  2097. tmpref : treference;
  2098. instr : taicpu;
  2099. begin
  2100. if ref.addressmode<>AM_OFFSET then
  2101. internalerror(200309071);
  2102. tmpref:=ref;
  2103. { Be sure to have a base register }
  2104. if (tmpref.base=NR_NO) then
  2105. begin
  2106. if tmpref.shiftmode<>SM_None then
  2107. internalerror(2014020702);
  2108. if tmpref.signindex<0 then
  2109. internalerror(200312023);
  2110. tmpref.base:=tmpref.index;
  2111. tmpref.index:=NR_NO;
  2112. end;
  2113. if assigned(tmpref.symbol) or
  2114. not((is_shifter_const(tmpref.offset,b)) or
  2115. (is_shifter_const(-tmpref.offset,b))
  2116. ) then
  2117. fixref(list,tmpref);
  2118. { expect a base here if there is an index }
  2119. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  2120. internalerror(200312022);
  2121. if tmpref.index<>NR_NO then
  2122. begin
  2123. if tmpref.shiftmode<>SM_None then
  2124. internalerror(200312021);
  2125. if tmpref.signindex<0 then
  2126. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  2127. else
  2128. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  2129. if tmpref.offset<>0 then
  2130. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  2131. end
  2132. else
  2133. begin
  2134. if tmpref.base=NR_NO then
  2135. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  2136. else
  2137. if tmpref.offset<>0 then
  2138. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  2139. else
  2140. begin
  2141. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  2142. list.concat(instr);
  2143. add_move_instruction(instr);
  2144. end;
  2145. end;
  2146. end;
  2147. procedure tbasecgarm.fixref(list : TAsmList;var ref : treference);
  2148. var
  2149. tmpreg, tmpreg2 : tregister;
  2150. tmpref : treference;
  2151. l, piclabel : tasmlabel;
  2152. indirection_done : boolean;
  2153. begin
  2154. { absolute symbols can't be handled directly, we've to store the symbol reference
  2155. in the text segment and access it pc relative
  2156. For now, we assume that references where base or index equals to PC are already
  2157. relative, all other references are assumed to be absolute and thus they need
  2158. to be handled extra.
  2159. A proper solution would be to change refoptions to a set and store the information
  2160. if the symbol is absolute or relative there.
  2161. }
  2162. { create consts entry }
  2163. reference_reset(tmpref,4);
  2164. current_asmdata.getjumplabel(l);
  2165. cg.a_label(current_procinfo.aktlocaldata,l);
  2166. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2167. piclabel:=nil;
  2168. tmpreg:=NR_NO;
  2169. indirection_done:=false;
  2170. if assigned(ref.symbol) then
  2171. begin
  2172. if (target_info.system=system_arm_darwin) and
  2173. (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) then
  2174. begin
  2175. tmpreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  2176. if ref.offset<>0 then
  2177. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  2178. indirection_done:=true;
  2179. end
  2180. else if (cs_create_pic in current_settings.moduleswitches) then
  2181. if (tf_pic_uses_got in target_info.flags) then
  2182. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym_offset(aitconst_got,ref.symbol,ref.offset))
  2183. else
  2184. begin
  2185. { ideally, we would want to generate
  2186. ldr r1, LPICConstPool
  2187. LPICLocal:
  2188. ldr/str r2,[pc,r1]
  2189. ...
  2190. LPICConstPool:
  2191. .long _globsym-(LPICLocal+8)
  2192. However, we cannot be sure that the ldr/str will follow
  2193. right after the call to fixref, so we have to load the
  2194. complete address already in a register.
  2195. }
  2196. current_asmdata.getaddrlabel(piclabel);
  2197. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,ref.symbol,ref.offset-8));
  2198. end
  2199. else
  2200. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  2201. end
  2202. else
  2203. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  2204. { load consts entry }
  2205. if not indirection_done then
  2206. begin
  2207. tmpreg:=getintregister(list,OS_INT);
  2208. tmpref.symbol:=l;
  2209. tmpref.base:=NR_PC;
  2210. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2211. if (cs_create_pic in current_settings.moduleswitches) and
  2212. (tf_pic_uses_got in target_info.flags) and
  2213. assigned(ref.symbol) then
  2214. begin
  2215. reference_reset(tmpref,4);
  2216. tmpref.base:=current_procinfo.got;
  2217. tmpref.index:=tmpreg;
  2218. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2219. end;
  2220. end;
  2221. if assigned(piclabel) then
  2222. begin
  2223. cg.a_label(list,piclabel);
  2224. tmpreg2:=getaddressregister(list);
  2225. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpreg,NR_PC,tmpreg2);
  2226. tmpreg:=tmpreg2
  2227. end;
  2228. { This routine can be called with PC as base/index in case the offset
  2229. was too large to encode in a load/store. In that case, the entire
  2230. absolute expression has been re-encoded in a new constpool entry, and
  2231. we have to remove the use of PC from the original reference (the code
  2232. above made everything relative to the value loaded from the new
  2233. constpool entry) }
  2234. if is_pc(ref.base) then
  2235. ref.base:=NR_NO;
  2236. if is_pc(ref.index) then
  2237. ref.index:=NR_NO;
  2238. if (ref.base<>NR_NO) then
  2239. begin
  2240. if ref.index<>NR_NO then
  2241. begin
  2242. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  2243. ref.base:=tmpreg;
  2244. end
  2245. else
  2246. if ref.base<>NR_PC then
  2247. begin
  2248. ref.index:=tmpreg;
  2249. ref.shiftimm:=0;
  2250. ref.signindex:=1;
  2251. ref.shiftmode:=SM_None;
  2252. end
  2253. else
  2254. ref.base:=tmpreg;
  2255. end
  2256. else
  2257. ref.base:=tmpreg;
  2258. ref.offset:=0;
  2259. ref.symbol:=nil;
  2260. end;
  2261. procedure tbasecgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  2262. var
  2263. paraloc1,paraloc2,paraloc3 : TCGPara;
  2264. pd : tprocdef;
  2265. begin
  2266. pd:=search_system_proc('MOVE');
  2267. paraloc1.init;
  2268. paraloc2.init;
  2269. paraloc3.init;
  2270. paramanager.getintparaloc(pd,1,paraloc1);
  2271. paramanager.getintparaloc(pd,2,paraloc2);
  2272. paramanager.getintparaloc(pd,3,paraloc3);
  2273. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  2274. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  2275. a_loadaddr_ref_cgpara(list,source,paraloc1);
  2276. paramanager.freecgpara(list,paraloc3);
  2277. paramanager.freecgpara(list,paraloc2);
  2278. paramanager.freecgpara(list,paraloc1);
  2279. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2280. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2281. a_call_name(list,'FPC_MOVE',false);
  2282. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2283. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2284. paraloc3.done;
  2285. paraloc2.done;
  2286. paraloc1.done;
  2287. end;
  2288. procedure tbasecgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  2289. const
  2290. maxtmpreg_arm = 10; {roozbeh: can be reduced to 8 or lower if might conflick with reserved ones,also +2 is used becouse of regs required for referencing}
  2291. maxtmpreg_thumb = 5;
  2292. var
  2293. srcref,dstref,usedtmpref,usedtmpref2:treference;
  2294. srcreg,destreg,countreg,r,tmpreg:tregister;
  2295. helpsize:aint;
  2296. copysize:byte;
  2297. cgsize:Tcgsize;
  2298. tmpregisters:array[1..maxtmpreg_arm] of tregister;
  2299. maxtmpreg,
  2300. tmpregi,tmpregi2:byte;
  2301. { will never be called with count<=4 }
  2302. procedure genloop(count : aword;size : byte);
  2303. const
  2304. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2305. var
  2306. l : tasmlabel;
  2307. begin
  2308. current_asmdata.getjumplabel(l);
  2309. if count<size then size:=1;
  2310. a_load_const_reg(list,OS_INT,count div size,countreg);
  2311. cg.a_label(list,l);
  2312. srcref.addressmode:=AM_POSTINDEXED;
  2313. dstref.addressmode:=AM_POSTINDEXED;
  2314. srcref.offset:=size;
  2315. dstref.offset:=size;
  2316. r:=getintregister(list,size2opsize[size]);
  2317. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2318. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2319. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  2320. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2321. a_jmp_flags(list,F_NE,l);
  2322. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2323. srcref.offset:=1;
  2324. dstref.offset:=1;
  2325. case count mod size of
  2326. 1:
  2327. begin
  2328. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2329. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2330. end;
  2331. 2:
  2332. if aligned then
  2333. begin
  2334. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2335. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2336. end
  2337. else
  2338. begin
  2339. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2340. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2341. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2342. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2343. end;
  2344. 3:
  2345. if aligned then
  2346. begin
  2347. srcref.offset:=2;
  2348. dstref.offset:=2;
  2349. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2350. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2351. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2352. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2353. end
  2354. else
  2355. begin
  2356. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2357. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2358. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2359. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2360. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2361. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2362. end;
  2363. end;
  2364. { keep the registers alive }
  2365. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2366. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2367. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2368. end;
  2369. { will never be called with count<=4 }
  2370. procedure genloop_thumb(count : aword;size : byte);
  2371. procedure refincofs(const ref : treference;const value : longint = 1);
  2372. begin
  2373. a_op_const_reg(list,OP_ADD,OS_ADDR,value,ref.base);
  2374. end;
  2375. const
  2376. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2377. var
  2378. l : tasmlabel;
  2379. begin
  2380. current_asmdata.getjumplabel(l);
  2381. if count<size then size:=1;
  2382. a_load_const_reg(list,OS_INT,count div size,countreg);
  2383. cg.a_label(list,l);
  2384. r:=getintregister(list,size2opsize[size]);
  2385. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2386. refincofs(srcref);
  2387. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2388. refincofs(dstref);
  2389. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2390. list.concat(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1));
  2391. a_jmp_flags(list,F_NE,l);
  2392. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2393. case count mod size of
  2394. 1:
  2395. begin
  2396. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2397. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2398. end;
  2399. 2:
  2400. if aligned then
  2401. begin
  2402. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2403. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2404. end
  2405. else
  2406. begin
  2407. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2408. refincofs(srcref);
  2409. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2410. refincofs(dstref);
  2411. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2412. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2413. end;
  2414. 3:
  2415. if aligned then
  2416. begin
  2417. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2418. refincofs(srcref,2);
  2419. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2420. refincofs(dstref,2);
  2421. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2422. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2423. end
  2424. else
  2425. begin
  2426. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2427. refincofs(srcref);
  2428. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2429. refincofs(dstref);
  2430. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2431. refincofs(srcref);
  2432. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2433. refincofs(dstref);
  2434. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2435. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2436. end;
  2437. end;
  2438. { keep the registers alive }
  2439. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2440. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2441. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2442. end;
  2443. begin
  2444. if len=0 then
  2445. exit;
  2446. if GenerateThumbCode then
  2447. maxtmpreg:=maxtmpreg_thumb
  2448. else
  2449. maxtmpreg:=maxtmpreg_arm;
  2450. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  2451. dstref:=dest;
  2452. srcref:=source;
  2453. if cs_opt_size in current_settings.optimizerswitches then
  2454. helpsize:=8;
  2455. if aligned and (len=4) then
  2456. begin
  2457. tmpreg:=getintregister(list,OS_32);
  2458. a_load_ref_reg(list,OS_32,OS_32,source,tmpreg);
  2459. a_load_reg_ref(list,OS_32,OS_32,tmpreg,dest);
  2460. end
  2461. else if aligned and (len=2) then
  2462. begin
  2463. tmpreg:=getintregister(list,OS_16);
  2464. a_load_ref_reg(list,OS_16,OS_16,source,tmpreg);
  2465. a_load_reg_ref(list,OS_16,OS_16,tmpreg,dest);
  2466. end
  2467. else if (len<=helpsize) and aligned then
  2468. begin
  2469. tmpregi:=0;
  2470. srcreg:=getintregister(list,OS_ADDR);
  2471. { explicit pc relative addressing, could be
  2472. e.g. a floating point constant }
  2473. if source.base=NR_PC then
  2474. begin
  2475. { ... then we don't need a loadaddr }
  2476. srcref:=source;
  2477. end
  2478. else
  2479. begin
  2480. a_loadaddr_ref_reg(list,source,srcreg);
  2481. reference_reset_base(srcref,srcreg,0,source.alignment);
  2482. end;
  2483. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  2484. begin
  2485. inc(tmpregi);
  2486. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  2487. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  2488. inc(srcref.offset,4);
  2489. dec(len,4);
  2490. end;
  2491. destreg:=getintregister(list,OS_ADDR);
  2492. a_loadaddr_ref_reg(list,dest,destreg);
  2493. reference_reset_base(dstref,destreg,0,dest.alignment);
  2494. tmpregi2:=1;
  2495. while (tmpregi2<=tmpregi) do
  2496. begin
  2497. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  2498. inc(dstref.offset,4);
  2499. inc(tmpregi2);
  2500. end;
  2501. copysize:=4;
  2502. cgsize:=OS_32;
  2503. while len<>0 do
  2504. begin
  2505. if len<2 then
  2506. begin
  2507. copysize:=1;
  2508. cgsize:=OS_8;
  2509. end
  2510. else if len<4 then
  2511. begin
  2512. copysize:=2;
  2513. cgsize:=OS_16;
  2514. end;
  2515. dec(len,copysize);
  2516. r:=getintregister(list,cgsize);
  2517. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2518. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2519. inc(srcref.offset,copysize);
  2520. inc(dstref.offset,copysize);
  2521. end;{end of while}
  2522. end
  2523. else
  2524. begin
  2525. cgsize:=OS_32;
  2526. if (len<=4) then{len<=4 and not aligned}
  2527. begin
  2528. r:=getintregister(list,cgsize);
  2529. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2530. if Len=1 then
  2531. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  2532. else
  2533. begin
  2534. tmpreg:=getintregister(list,cgsize);
  2535. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2536. inc(usedtmpref.offset,1);
  2537. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2538. inc(usedtmpref2.offset,1);
  2539. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2540. if len>2 then
  2541. begin
  2542. inc(usedtmpref.offset,1);
  2543. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2544. inc(usedtmpref2.offset,1);
  2545. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2546. if len>3 then
  2547. begin
  2548. inc(usedtmpref.offset,1);
  2549. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2550. inc(usedtmpref2.offset,1);
  2551. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2552. end;
  2553. end;
  2554. end;
  2555. end{end of if len<=4}
  2556. else
  2557. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  2558. destreg:=getintregister(list,OS_ADDR);
  2559. a_loadaddr_ref_reg(list,dest,destreg);
  2560. reference_reset_base(dstref,destreg,0,dest.alignment);
  2561. srcreg:=getintregister(list,OS_ADDR);
  2562. a_loadaddr_ref_reg(list,source,srcreg);
  2563. reference_reset_base(srcref,srcreg,0,source.alignment);
  2564. countreg:=getintregister(list,OS_32);
  2565. // if cs_opt_size in current_settings.optimizerswitches then
  2566. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  2567. {if aligned then
  2568. genloop(len,4)
  2569. else}
  2570. if GenerateThumbCode then
  2571. genloop_thumb(len,1)
  2572. else
  2573. genloop(len,1);
  2574. end;
  2575. end;
  2576. end;
  2577. procedure tbasecgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2578. begin
  2579. g_concatcopy_internal(list,source,dest,len,false);
  2580. end;
  2581. procedure tbasecgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2582. begin
  2583. if (source.alignment in [1,3]) or
  2584. (dest.alignment in [1,3]) then
  2585. g_concatcopy_internal(list,source,dest,len,false)
  2586. else
  2587. g_concatcopy_internal(list,source,dest,len,true);
  2588. end;
  2589. procedure tbasecgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  2590. var
  2591. ovloc : tlocation;
  2592. begin
  2593. ovloc.loc:=LOC_VOID;
  2594. g_overflowCheck_loc(list,l,def,ovloc);
  2595. end;
  2596. procedure tbasecgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2597. var
  2598. hl : tasmlabel;
  2599. ai:TAiCpu;
  2600. hflags : tresflags;
  2601. begin
  2602. if not(cs_check_overflow in current_settings.localswitches) then
  2603. exit;
  2604. current_asmdata.getjumplabel(hl);
  2605. case ovloc.loc of
  2606. LOC_VOID:
  2607. begin
  2608. ai:=taicpu.op_sym(A_B,hl);
  2609. ai.is_jmp:=true;
  2610. if not((def.typ=pointerdef) or
  2611. ((def.typ=orddef) and
  2612. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2613. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2614. ai.SetCondition(C_VC)
  2615. else
  2616. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  2617. ai.SetCondition(C_CS)
  2618. else
  2619. ai.SetCondition(C_CC);
  2620. list.concat(ai);
  2621. end;
  2622. LOC_FLAGS:
  2623. begin
  2624. hflags:=ovloc.resflags;
  2625. inverse_flags(hflags);
  2626. cg.a_jmp_flags(list,hflags,hl);
  2627. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2628. end;
  2629. else
  2630. internalerror(200409281);
  2631. end;
  2632. a_call_name(list,'FPC_OVERFLOW',false);
  2633. a_label(list,hl);
  2634. end;
  2635. procedure tbasecgarm.g_save_registers(list : TAsmList);
  2636. begin
  2637. { this work is done in g_proc_entry }
  2638. end;
  2639. procedure tbasecgarm.g_restore_registers(list : TAsmList);
  2640. begin
  2641. { this work is done in g_proc_exit }
  2642. end;
  2643. procedure tbasecgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2644. var
  2645. ai : taicpu;
  2646. hlabel : TAsmLabel;
  2647. begin
  2648. if GenerateThumbCode then
  2649. begin
  2650. { the optimizer has to fix this if jump range is sufficient short }
  2651. current_asmdata.getjumplabel(hlabel);
  2652. ai:=Taicpu.Op_sym(A_B,hlabel);
  2653. ai.SetCondition(inverse_cond(OpCmp2AsmCond[cond]));
  2654. ai.is_jmp:=true;
  2655. list.concat(ai);
  2656. a_jmp_always(list,l);
  2657. a_label(list,hlabel);
  2658. end
  2659. else
  2660. begin
  2661. ai:=Taicpu.Op_sym(A_B,l);
  2662. ai.SetCondition(OpCmp2AsmCond[cond]);
  2663. ai.is_jmp:=true;
  2664. list.concat(ai);
  2665. end;
  2666. end;
  2667. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2668. const
  2669. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2670. (A_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2671. (A_FCVTDS,A_FCPYD,A_NONE,A_NONE,A_NONE),
  2672. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2673. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2674. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2675. begin
  2676. result:=convertop[fromsize,tosize];
  2677. if result=A_NONE then
  2678. internalerror(200312205);
  2679. end;
  2680. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2681. var
  2682. instr: taicpu;
  2683. begin
  2684. if shuffle=nil then
  2685. begin
  2686. if fromsize=tosize then
  2687. { needs correct size in case of spilling }
  2688. case fromsize of
  2689. OS_F32:
  2690. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2691. OS_F64:
  2692. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2693. else
  2694. internalerror(2009112405);
  2695. end
  2696. else
  2697. internalerror(2009112406);
  2698. end
  2699. else if shufflescalar(shuffle) then
  2700. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2701. else
  2702. internalerror(2009112407);
  2703. list.concat(instr);
  2704. case instr.opcode of
  2705. A_FCPYS,
  2706. A_FCPYD:
  2707. add_move_instruction(instr);
  2708. end;
  2709. end;
  2710. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2711. var
  2712. intreg,
  2713. tmpmmreg : tregister;
  2714. reg64 : tregister64;
  2715. op : tasmop;
  2716. begin
  2717. if assigned(shuffle) and
  2718. not(shufflescalar(shuffle)) then
  2719. internalerror(2009112413);
  2720. case fromsize of
  2721. OS_32,OS_S32:
  2722. begin
  2723. fromsize:=OS_F32;
  2724. { since we are loading an integer, no conversion may be required }
  2725. if (fromsize<>tosize) then
  2726. internalerror(2009112801);
  2727. end;
  2728. OS_64,OS_S64:
  2729. begin
  2730. fromsize:=OS_F64;
  2731. { since we are loading an integer, no conversion may be required }
  2732. if (fromsize<>tosize) then
  2733. internalerror(2009112901);
  2734. end;
  2735. end;
  2736. if (fromsize<>tosize) then
  2737. tmpmmreg:=getmmregister(list,fromsize)
  2738. else
  2739. tmpmmreg:=reg;
  2740. if (ref.alignment in [1,2]) then
  2741. begin
  2742. case fromsize of
  2743. OS_F32:
  2744. begin
  2745. intreg:=getintregister(list,OS_32);
  2746. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2747. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2748. end;
  2749. OS_F64:
  2750. begin
  2751. reg64.reglo:=getintregister(list,OS_32);
  2752. reg64.reghi:=getintregister(list,OS_32);
  2753. cg64.a_load64_ref_reg(list,ref,reg64);
  2754. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2755. end;
  2756. else
  2757. internalerror(2009112412);
  2758. end;
  2759. end
  2760. else
  2761. begin
  2762. case fromsize of
  2763. OS_F32:
  2764. op:=A_FLDS;
  2765. OS_F64:
  2766. op:=A_FLDD;
  2767. else
  2768. internalerror(2009112415);
  2769. end;
  2770. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2771. end;
  2772. if (tmpmmreg<>reg) then
  2773. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2774. end;
  2775. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2776. var
  2777. intreg,
  2778. tmpmmreg : tregister;
  2779. reg64 : tregister64;
  2780. op : tasmop;
  2781. begin
  2782. if assigned(shuffle) and
  2783. not(shufflescalar(shuffle)) then
  2784. internalerror(2009112416);
  2785. case tosize of
  2786. OS_32,OS_S32:
  2787. begin
  2788. tosize:=OS_F32;
  2789. { since we are loading an integer, no conversion may be required }
  2790. if (fromsize<>tosize) then
  2791. internalerror(2009112801);
  2792. end;
  2793. OS_64,OS_S64:
  2794. begin
  2795. tosize:=OS_F64;
  2796. { since we are loading an integer, no conversion may be required }
  2797. if (fromsize<>tosize) then
  2798. internalerror(2009112901);
  2799. end;
  2800. end;
  2801. if (fromsize<>tosize) then
  2802. begin
  2803. tmpmmreg:=getmmregister(list,tosize);
  2804. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2805. end
  2806. else
  2807. tmpmmreg:=reg;
  2808. if (ref.alignment in [1,2]) then
  2809. begin
  2810. case tosize of
  2811. OS_F32:
  2812. begin
  2813. intreg:=getintregister(list,OS_32);
  2814. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2815. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2816. end;
  2817. OS_F64:
  2818. begin
  2819. reg64.reglo:=getintregister(list,OS_32);
  2820. reg64.reghi:=getintregister(list,OS_32);
  2821. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2822. cg64.a_load64_reg_ref(list,reg64,ref);
  2823. end;
  2824. else
  2825. internalerror(2009112417);
  2826. end;
  2827. end
  2828. else
  2829. begin
  2830. case fromsize of
  2831. OS_F32:
  2832. op:=A_FSTS;
  2833. OS_F64:
  2834. op:=A_FSTD;
  2835. else
  2836. internalerror(2009112418);
  2837. end;
  2838. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2839. end;
  2840. end;
  2841. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2842. begin
  2843. { this code can only be used to transfer raw data, not to perform
  2844. conversions }
  2845. if (tosize<>OS_F32) then
  2846. internalerror(2009112419);
  2847. if not(fromsize in [OS_32,OS_S32]) then
  2848. internalerror(2009112420);
  2849. if assigned(shuffle) and
  2850. not shufflescalar(shuffle) then
  2851. internalerror(2009112516);
  2852. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2853. end;
  2854. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2855. begin
  2856. { this code can only be used to transfer raw data, not to perform
  2857. conversions }
  2858. if (fromsize<>OS_F32) then
  2859. internalerror(2009112430);
  2860. if not(tosize in [OS_32,OS_S32]) then
  2861. internalerror(2009112420);
  2862. if assigned(shuffle) and
  2863. not shufflescalar(shuffle) then
  2864. internalerror(2009112514);
  2865. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2866. end;
  2867. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2868. var
  2869. tmpreg: tregister;
  2870. begin
  2871. { the vfp doesn't support xor nor any other logical operation, but
  2872. this routine is used to initialise global mm regvars. We can
  2873. easily initialise an mm reg with 0 though. }
  2874. case op of
  2875. OP_XOR:
  2876. begin
  2877. if (src<>dst) or
  2878. (reg_cgsize(src)<>size) or
  2879. assigned(shuffle) then
  2880. internalerror(2009112907);
  2881. tmpreg:=getintregister(list,OS_32);
  2882. a_load_const_reg(list,OS_32,0,tmpreg);
  2883. case size of
  2884. OS_F32:
  2885. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2886. OS_F64:
  2887. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2888. else
  2889. internalerror(2009112908);
  2890. end;
  2891. end
  2892. else
  2893. internalerror(2009112906);
  2894. end;
  2895. end;
  2896. procedure tbasecgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2897. procedure loadvmttor12;
  2898. var
  2899. tmpref,
  2900. href : treference;
  2901. extrareg : boolean;
  2902. l : TAsmLabel;
  2903. begin
  2904. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2905. if GenerateThumbCode then
  2906. begin
  2907. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2908. begin
  2909. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2910. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2911. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2912. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2913. end
  2914. else
  2915. begin
  2916. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2917. { create consts entry }
  2918. reference_reset(tmpref,4);
  2919. current_asmdata.getjumplabel(l);
  2920. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2921. cg.a_label(current_procinfo.aktlocaldata,l);
  2922. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2923. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2924. tmpref.symbol:=l;
  2925. tmpref.base:=NR_PC;
  2926. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2927. href.offset:=0;
  2928. href.index:=NR_R1;
  2929. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2930. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2931. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2932. end;
  2933. end
  2934. else
  2935. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2936. end;
  2937. procedure op_onr12methodaddr;
  2938. var
  2939. tmpref,
  2940. href : treference;
  2941. l : TAsmLabel;
  2942. begin
  2943. if (procdef.extnumber=$ffff) then
  2944. Internalerror(200006139);
  2945. if GenerateThumbCode then
  2946. begin
  2947. reference_reset_base(href,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2948. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2949. begin
  2950. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2951. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2952. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2953. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2954. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2955. end
  2956. else
  2957. begin
  2958. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2959. { create consts entry }
  2960. reference_reset(tmpref,4);
  2961. current_asmdata.getjumplabel(l);
  2962. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2963. cg.a_label(current_procinfo.aktlocaldata,l);
  2964. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2965. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2966. tmpref.symbol:=l;
  2967. tmpref.base:=NR_PC;
  2968. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2969. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2970. href.offset:=0;
  2971. href.base:=NR_R0;
  2972. href.index:=NR_R1;
  2973. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2974. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2975. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2976. end;
  2977. end
  2978. else
  2979. begin
  2980. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2981. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2982. end;
  2983. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2984. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12))
  2985. else
  2986. list.concat(taicpu.op_reg(A_BX,NR_R12));
  2987. end;
  2988. var
  2989. make_global : boolean;
  2990. tmpref : treference;
  2991. l : TAsmLabel;
  2992. begin
  2993. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2994. Internalerror(200006137);
  2995. if not assigned(procdef.struct) or
  2996. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2997. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2998. Internalerror(200006138);
  2999. if procdef.owner.symtabletype<>ObjectSymtable then
  3000. Internalerror(200109191);
  3001. if GenerateThumbCode or GenerateThumb2Code then
  3002. list.concat(tai_thumb_func.create);
  3003. make_global:=false;
  3004. if (not current_module.is_unit) or
  3005. create_smartlink or
  3006. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  3007. make_global:=true;
  3008. if make_global then
  3009. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  3010. else
  3011. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  3012. { the wrapper might need aktlocaldata for the additional data to
  3013. load the constant }
  3014. current_procinfo:=cprocinfo.create(nil);
  3015. { set param1 interface to self }
  3016. g_adjust_self_value(list,procdef,ioffset);
  3017. { case 4 }
  3018. if (po_virtualmethod in procdef.procoptions) and
  3019. not is_objectpascal_helper(procdef.struct) then
  3020. begin
  3021. loadvmttor12;
  3022. op_onr12methodaddr;
  3023. end
  3024. { case 0 }
  3025. else if GenerateThumbCode then
  3026. begin
  3027. { bl cannot be used here because it destroys lr }
  3028. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3029. { create consts entry }
  3030. reference_reset(tmpref,4);
  3031. current_asmdata.getjumplabel(l);
  3032. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3033. cg.a_label(current_procinfo.aktlocaldata,l);
  3034. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3035. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3036. tmpref.symbol:=l;
  3037. tmpref.base:=NR_PC;
  3038. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  3039. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3040. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3041. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3042. end
  3043. else
  3044. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3045. list.concatlist(current_procinfo.aktlocaldata);
  3046. current_procinfo.Free;
  3047. current_procinfo:=nil;
  3048. list.concat(Tai_symbol_end.Createname(labelname));
  3049. end;
  3050. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3051. const
  3052. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3053. begin
  3054. if (op in overflowops) and
  3055. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3056. a_load_reg_reg(list,OS_32,size,dst,dst);
  3057. end;
  3058. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3059. procedure checkreg(var reg : TRegister);
  3060. var
  3061. tmpreg : TRegister;
  3062. begin
  3063. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3064. (getsupreg(reg)=RS_R15) then
  3065. begin
  3066. tmpreg:=getintregister(list,OS_INT);
  3067. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3068. reg:=tmpreg;
  3069. end;
  3070. end;
  3071. begin
  3072. checkreg(op1);
  3073. checkreg(op2);
  3074. checkreg(op3);
  3075. checkreg(op4);
  3076. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3077. end;
  3078. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3079. begin
  3080. case op of
  3081. OP_NEG:
  3082. begin
  3083. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3084. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3085. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3086. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3087. end;
  3088. OP_NOT:
  3089. begin
  3090. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3091. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3092. end;
  3093. else
  3094. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3095. end;
  3096. end;
  3097. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3098. begin
  3099. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3100. end;
  3101. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3102. var
  3103. ovloc : tlocation;
  3104. begin
  3105. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3106. end;
  3107. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3108. var
  3109. ovloc : tlocation;
  3110. begin
  3111. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3112. end;
  3113. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3114. begin
  3115. { this code can only be used to transfer raw data, not to perform
  3116. conversions }
  3117. if (mmsize<>OS_F64) then
  3118. internalerror(2009112405);
  3119. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  3120. end;
  3121. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3122. begin
  3123. { this code can only be used to transfer raw data, not to perform
  3124. conversions }
  3125. if (mmsize<>OS_F64) then
  3126. internalerror(2009112406);
  3127. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  3128. end;
  3129. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3130. var
  3131. tmpreg : tregister;
  3132. b : byte;
  3133. begin
  3134. ovloc.loc:=LOC_VOID;
  3135. case op of
  3136. OP_NEG,
  3137. OP_NOT :
  3138. internalerror(2012022501);
  3139. end;
  3140. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3141. begin
  3142. case op of
  3143. OP_ADD:
  3144. begin
  3145. if is_shifter_const(lo(value),b) then
  3146. begin
  3147. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3148. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3149. end
  3150. else
  3151. begin
  3152. tmpreg:=cg.getintregister(list,OS_32);
  3153. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3154. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3155. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3156. end;
  3157. if is_shifter_const(hi(value),b) then
  3158. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3159. else
  3160. begin
  3161. tmpreg:=cg.getintregister(list,OS_32);
  3162. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3163. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3164. end;
  3165. end;
  3166. OP_SUB:
  3167. begin
  3168. if is_shifter_const(lo(value),b) then
  3169. begin
  3170. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3171. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3172. end
  3173. else
  3174. begin
  3175. tmpreg:=cg.getintregister(list,OS_32);
  3176. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3177. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3178. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3179. end;
  3180. if is_shifter_const(hi(value),b) then
  3181. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  3182. else
  3183. begin
  3184. tmpreg:=cg.getintregister(list,OS_32);
  3185. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3186. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3187. end;
  3188. end;
  3189. else
  3190. internalerror(200502131);
  3191. end;
  3192. if size=OS_64 then
  3193. begin
  3194. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3195. ovloc.loc:=LOC_FLAGS;
  3196. case op of
  3197. OP_ADD:
  3198. ovloc.resflags:=F_CS;
  3199. OP_SUB:
  3200. ovloc.resflags:=F_CC;
  3201. end;
  3202. end;
  3203. end
  3204. else
  3205. begin
  3206. case op of
  3207. OP_AND,OP_OR,OP_XOR:
  3208. begin
  3209. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3210. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3211. end;
  3212. OP_ADD:
  3213. begin
  3214. if is_shifter_const(aint(lo(value)),b) then
  3215. begin
  3216. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3217. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3218. end
  3219. else
  3220. begin
  3221. tmpreg:=cg.getintregister(list,OS_32);
  3222. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3223. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3224. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3225. end;
  3226. if is_shifter_const(aint(hi(value)),b) then
  3227. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3228. else
  3229. begin
  3230. tmpreg:=cg.getintregister(list,OS_32);
  3231. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3232. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3233. end;
  3234. end;
  3235. OP_SUB:
  3236. begin
  3237. if is_shifter_const(aint(lo(value)),b) then
  3238. begin
  3239. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3240. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3241. end
  3242. else
  3243. begin
  3244. tmpreg:=cg.getintregister(list,OS_32);
  3245. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3246. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3247. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3248. end;
  3249. if is_shifter_const(aint(hi(value)),b) then
  3250. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3251. else
  3252. begin
  3253. tmpreg:=cg.getintregister(list,OS_32);
  3254. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3255. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3256. end;
  3257. end;
  3258. else
  3259. internalerror(2003083101);
  3260. end;
  3261. end;
  3262. end;
  3263. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3264. begin
  3265. ovloc.loc:=LOC_VOID;
  3266. case op of
  3267. OP_NEG,
  3268. OP_NOT :
  3269. internalerror(2012022502);
  3270. end;
  3271. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3272. begin
  3273. case op of
  3274. OP_ADD:
  3275. begin
  3276. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3277. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3278. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3279. end;
  3280. OP_SUB:
  3281. begin
  3282. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3283. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3284. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3285. end;
  3286. else
  3287. internalerror(2003083101);
  3288. end;
  3289. if size=OS_64 then
  3290. begin
  3291. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3292. ovloc.loc:=LOC_FLAGS;
  3293. case op of
  3294. OP_ADD:
  3295. ovloc.resflags:=F_CS;
  3296. OP_SUB:
  3297. ovloc.resflags:=F_CC;
  3298. end;
  3299. end;
  3300. end
  3301. else
  3302. begin
  3303. case op of
  3304. OP_AND,OP_OR,OP_XOR:
  3305. begin
  3306. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3307. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3308. end;
  3309. OP_ADD:
  3310. begin
  3311. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3312. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3313. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3314. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3315. end;
  3316. OP_SUB:
  3317. begin
  3318. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3319. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3320. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3321. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3322. end;
  3323. else
  3324. internalerror(2003083101);
  3325. end;
  3326. end;
  3327. end;
  3328. procedure tthumbcgarm.init_register_allocators;
  3329. begin
  3330. inherited init_register_allocators;
  3331. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3332. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3333. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3334. else
  3335. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3336. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3337. end;
  3338. procedure tthumbcgarm.done_register_allocators;
  3339. begin
  3340. rg[R_INTREGISTER].free;
  3341. rg[R_FPUREGISTER].free;
  3342. rg[R_MMREGISTER].free;
  3343. inherited done_register_allocators;
  3344. end;
  3345. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3346. var
  3347. ref : treference;
  3348. shift : byte;
  3349. r : byte;
  3350. regs, saveregs : tcpuregisterset;
  3351. r7offset,
  3352. stackmisalignment : pint;
  3353. postfix: toppostfix;
  3354. registerarea,
  3355. imm1, imm2: DWord;
  3356. stack_parameters: Boolean;
  3357. begin
  3358. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3359. LocalSize:=align(LocalSize,4);
  3360. { call instruction does not put anything on the stack }
  3361. stackmisalignment:=0;
  3362. if not(nostackframe) then
  3363. begin
  3364. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3365. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3366. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3367. { save int registers }
  3368. reference_reset(ref,4);
  3369. ref.index:=NR_STACK_POINTER_REG;
  3370. ref.addressmode:=AM_PREINDEXED;
  3371. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3372. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3373. begin
  3374. //!!!! a_reg_alloc(list,NR_R12);
  3375. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3376. end;
  3377. { the (old) ARM APCS requires saving both the stack pointer (to
  3378. crawl the stack) and the PC (to identify the function this
  3379. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3380. and R15 -- still needs updating for EABI and Darwin, they don't
  3381. need that }
  3382. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3383. regs:=regs+[RS_R7,RS_R14]
  3384. else
  3385. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3386. include(regs,RS_R14);
  3387. { safely estimate stack size }
  3388. if localsize+current_settings.alignment.localalignmax+4>508 then
  3389. begin
  3390. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3391. include(regs,RS_R4);
  3392. end;
  3393. registerarea:=0;
  3394. if regs<>[] then
  3395. begin
  3396. for r:=RS_R0 to RS_R15 do
  3397. if r in regs then
  3398. inc(registerarea,4);
  3399. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3400. end;
  3401. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3402. if stack_parameters or (LocalSize<>0) or
  3403. ((stackmisalignment<>0) and
  3404. ((pi_do_call in current_procinfo.flags) or
  3405. (po_assembler in current_procinfo.procdef.procoptions))) then
  3406. begin
  3407. { do we access stack parameters?
  3408. if yes, the previously estimated stacksize must be used }
  3409. if stack_parameters then
  3410. begin
  3411. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  3412. begin
  3413. writeln(localsize);
  3414. writeln(tarmprocinfo(current_procinfo).stackframesize);
  3415. internalerror(2013040601);
  3416. end
  3417. else
  3418. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  3419. end
  3420. else
  3421. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3422. if localsize<508 then
  3423. begin
  3424. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3425. end
  3426. else if localsize<=1016 then
  3427. begin
  3428. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3429. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3430. end
  3431. else
  3432. begin
  3433. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3434. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3435. include(regs,RS_R4);
  3436. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3437. //!!!! a_reg_alloc(list,NR_R12);
  3438. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3439. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3440. //!!!! a_reg_dealloc(list,NR_R12);
  3441. end;
  3442. end;
  3443. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3444. begin
  3445. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3446. end;
  3447. end;
  3448. end;
  3449. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3450. var
  3451. ref : treference;
  3452. LocalSize : longint;
  3453. r,
  3454. shift : byte;
  3455. saveregs,
  3456. regs : tcpuregisterset;
  3457. registerarea : DWord;
  3458. stackmisalignment: pint;
  3459. imm1, imm2: DWord;
  3460. stack_parameters : Boolean;
  3461. begin
  3462. if not(nostackframe) then
  3463. begin
  3464. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3465. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3466. include(regs,RS_R15);
  3467. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3468. include(regs,getsupreg(current_procinfo.framepointer));
  3469. registerarea:=0;
  3470. for r:=RS_R0 to RS_R15 do
  3471. if r in regs then
  3472. inc(registerarea,4);
  3473. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3474. LocalSize:=current_procinfo.calc_stackframe_size;
  3475. if stack_parameters then
  3476. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  3477. else
  3478. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3479. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3480. (target_info.system in systems_darwin) then
  3481. begin
  3482. if (LocalSize<>0) or
  3483. ((stackmisalignment<>0) and
  3484. ((pi_do_call in current_procinfo.flags) or
  3485. (po_assembler in current_procinfo.procdef.procoptions))) then
  3486. begin
  3487. if LocalSize=0 then
  3488. else if LocalSize<=508 then
  3489. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3490. else if LocalSize<=1016 then
  3491. begin
  3492. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3493. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3494. end
  3495. else
  3496. begin
  3497. a_reg_alloc(list,NR_R3);
  3498. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3499. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3500. a_reg_dealloc(list,NR_R3);
  3501. end;
  3502. end;
  3503. if regs=[] then
  3504. begin
  3505. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3506. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3507. else
  3508. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3509. end
  3510. else
  3511. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3512. end;
  3513. end
  3514. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3515. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3516. else
  3517. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3518. end;
  3519. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3520. var
  3521. oppostfix:toppostfix;
  3522. usedtmpref: treference;
  3523. tmpreg,tmpreg2 : tregister;
  3524. dir : integer;
  3525. begin
  3526. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3527. FromSize := ToSize;
  3528. case FromSize of
  3529. { signed integer registers }
  3530. OS_8:
  3531. oppostfix:=PF_B;
  3532. OS_S8:
  3533. oppostfix:=PF_SB;
  3534. OS_16:
  3535. oppostfix:=PF_H;
  3536. OS_S16:
  3537. oppostfix:=PF_SH;
  3538. OS_32,
  3539. OS_S32:
  3540. oppostfix:=PF_None;
  3541. else
  3542. InternalError(200308298);
  3543. end;
  3544. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3545. begin
  3546. if target_info.endian=endian_big then
  3547. dir:=-1
  3548. else
  3549. dir:=1;
  3550. case FromSize of
  3551. OS_16,OS_S16:
  3552. begin
  3553. { only complicated references need an extra loadaddr }
  3554. if assigned(ref.symbol) or
  3555. (ref.index<>NR_NO) or
  3556. (ref.offset<-124) or
  3557. (ref.offset>124) or
  3558. { sometimes the compiler reused registers }
  3559. (reg=ref.index) or
  3560. (reg=ref.base) then
  3561. begin
  3562. tmpreg2:=getintregister(list,OS_INT);
  3563. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3564. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3565. end
  3566. else
  3567. usedtmpref:=ref;
  3568. if target_info.endian=endian_big then
  3569. inc(usedtmpref.offset,1);
  3570. tmpreg:=getintregister(list,OS_INT);
  3571. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3572. inc(usedtmpref.offset,dir);
  3573. if FromSize=OS_16 then
  3574. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3575. else
  3576. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3577. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3578. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3579. end;
  3580. OS_32,OS_S32:
  3581. begin
  3582. tmpreg:=getintregister(list,OS_INT);
  3583. { only complicated references need an extra loadaddr }
  3584. if assigned(ref.symbol) or
  3585. (ref.index<>NR_NO) or
  3586. (ref.offset<-124) or
  3587. (ref.offset>124) or
  3588. { sometimes the compiler reused registers }
  3589. (reg=ref.index) or
  3590. (reg=ref.base) then
  3591. begin
  3592. tmpreg2:=getintregister(list,OS_INT);
  3593. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3594. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3595. end
  3596. else
  3597. usedtmpref:=ref;
  3598. if ref.alignment=2 then
  3599. begin
  3600. if target_info.endian=endian_big then
  3601. inc(usedtmpref.offset,2);
  3602. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3603. inc(usedtmpref.offset,dir*2);
  3604. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3605. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3606. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3607. end
  3608. else
  3609. begin
  3610. if target_info.endian=endian_big then
  3611. inc(usedtmpref.offset,3);
  3612. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3613. inc(usedtmpref.offset,dir);
  3614. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3615. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3616. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3617. inc(usedtmpref.offset,dir);
  3618. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3619. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3620. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3621. inc(usedtmpref.offset,dir);
  3622. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3623. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3624. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3625. end;
  3626. end
  3627. else
  3628. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3629. end;
  3630. end
  3631. else
  3632. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3633. if (fromsize=OS_S8) and (tosize = OS_16) then
  3634. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3635. end;
  3636. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3637. var
  3638. imm_shift : byte;
  3639. l : tasmlabel;
  3640. hr : treference;
  3641. begin
  3642. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3643. internalerror(2002090902);
  3644. if is_thumb_imm(a) then
  3645. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3646. else
  3647. begin
  3648. reference_reset(hr,4);
  3649. current_asmdata.getjumplabel(l);
  3650. cg.a_label(current_procinfo.aktlocaldata,l);
  3651. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3652. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3653. hr.symbol:=l;
  3654. hr.base:=NR_PC;
  3655. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3656. end;
  3657. end;
  3658. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3659. var
  3660. hsym : tsym;
  3661. href,
  3662. tmpref : treference;
  3663. paraloc : Pcgparalocation;
  3664. l : TAsmLabel;
  3665. begin
  3666. { calculate the parameter info for the procdef }
  3667. procdef.init_paraloc_info(callerside);
  3668. hsym:=tsym(procdef.parast.Find('self'));
  3669. if not(assigned(hsym) and
  3670. (hsym.typ=paravarsym)) then
  3671. internalerror(200305251);
  3672. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3673. while paraloc<>nil do
  3674. with paraloc^ do
  3675. begin
  3676. case loc of
  3677. LOC_REGISTER:
  3678. begin
  3679. if is_thumb_imm(ioffset) then
  3680. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3681. else
  3682. begin
  3683. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3684. reference_reset(tmpref,4);
  3685. current_asmdata.getjumplabel(l);
  3686. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3687. cg.a_label(current_procinfo.aktlocaldata,l);
  3688. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3689. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3690. tmpref.symbol:=l;
  3691. tmpref.base:=NR_PC;
  3692. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3693. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3694. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3695. end;
  3696. end;
  3697. LOC_REFERENCE:
  3698. begin
  3699. { offset in the wrapper needs to be adjusted for the stored
  3700. return address }
  3701. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  3702. if is_thumb_imm(ioffset) then
  3703. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  3704. else
  3705. begin
  3706. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3707. reference_reset(tmpref,4);
  3708. current_asmdata.getjumplabel(l);
  3709. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3710. cg.a_label(current_procinfo.aktlocaldata,l);
  3711. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3712. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3713. tmpref.symbol:=l;
  3714. tmpref.base:=NR_PC;
  3715. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3716. a_op_reg_ref(list,OP_SUB,size,NR_R4,href);
  3717. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3718. end;
  3719. end
  3720. else
  3721. internalerror(200309189);
  3722. end;
  3723. paraloc:=next;
  3724. end;
  3725. end;
  3726. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3727. var
  3728. href : treference;
  3729. tmpreg : TRegister;
  3730. begin
  3731. href:=ref;
  3732. if { LDR/STR limitations }
  3733. (
  3734. (((op=A_LDR) and (oppostfix=PF_None)) or
  3735. ((op=A_STR) and (oppostfix=PF_None))) and
  3736. (ref.base<>NR_STACK_POINTER_REG) and
  3737. (abs(ref.offset)>124)
  3738. ) or
  3739. { LDRB/STRB limitations }
  3740. (
  3741. (((op=A_LDR) and (oppostfix=PF_B)) or
  3742. ((op=A_LDRB) and (oppostfix=PF_None)) or
  3743. ((op=A_STR) and (oppostfix=PF_B)) or
  3744. ((op=A_STRB) and (oppostfix=PF_None))) and
  3745. ((ref.base=NR_STACK_POINTER_REG) or
  3746. (ref.index=NR_STACK_POINTER_REG) or
  3747. (abs(ref.offset)>31)
  3748. )
  3749. ) or
  3750. { LDRH/STRH limitations }
  3751. (
  3752. (((op=A_LDR) and (oppostfix=PF_H)) or
  3753. ((op=A_LDRH) and (oppostfix=PF_None)) or
  3754. ((op=A_STR) and (oppostfix=PF_H)) or
  3755. ((op=A_STRH) and (oppostfix=PF_None))) and
  3756. ((ref.base=NR_STACK_POINTER_REG) or
  3757. (ref.index=NR_STACK_POINTER_REG) or
  3758. (abs(ref.offset)>62) or
  3759. ((abs(ref.offset) mod 2)<>0)
  3760. )
  3761. ) then
  3762. begin
  3763. tmpreg:=getintregister(list,OS_ADDR);
  3764. a_loadaddr_ref_reg(list,ref,tmpreg);
  3765. reference_reset_base(href,tmpreg,0,ref.alignment);
  3766. end
  3767. else if (op=A_LDR) and
  3768. (oppostfix in [PF_None]) and
  3769. (ref.base=NR_STACK_POINTER_REG) and
  3770. (abs(ref.offset)>1020) then
  3771. begin
  3772. tmpreg:=getintregister(list,OS_ADDR);
  3773. a_loadaddr_ref_reg(list,ref,tmpreg);
  3774. reference_reset_base(href,tmpreg,0,ref.alignment);
  3775. end
  3776. else if (op=A_LDR) and
  3777. ((oppostfix in [PF_SH,PF_SB]) or
  3778. (abs(ref.offset)>124)) then
  3779. begin
  3780. tmpreg:=getintregister(list,OS_ADDR);
  3781. a_loadaddr_ref_reg(list,ref,tmpreg);
  3782. reference_reset_base(href,tmpreg,0,ref.alignment);
  3783. end;
  3784. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3785. end;
  3786. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3787. var
  3788. tmpreg,overflowreg : tregister;
  3789. asmop : tasmop;
  3790. begin
  3791. case op of
  3792. OP_NEG:
  3793. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3794. OP_NOT:
  3795. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3796. OP_DIV,OP_IDIV:
  3797. internalerror(200308284);
  3798. OP_ROL:
  3799. begin
  3800. if not(size in [OS_32,OS_S32]) then
  3801. internalerror(2008072801);
  3802. { simulate ROL by ror'ing 32-value }
  3803. tmpreg:=getintregister(list,OS_32);
  3804. a_load_const_reg(list,OS_32,32,tmpreg);
  3805. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3806. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3807. end;
  3808. else
  3809. begin
  3810. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3811. list.concat(setoppostfix(
  3812. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3813. end;
  3814. end;
  3815. maybeadjustresult(list,op,size,dst);
  3816. end;
  3817. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3818. var
  3819. tmpreg : tregister;
  3820. so : tshifterop;
  3821. l1 : longint;
  3822. imm1, imm2: DWord;
  3823. begin
  3824. //!!! ovloc.loc:=LOC_VOID;
  3825. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3826. case op of
  3827. OP_ADD:
  3828. begin
  3829. op:=OP_SUB;
  3830. a:=aint(dword(-a));
  3831. end;
  3832. OP_SUB:
  3833. begin
  3834. op:=OP_ADD;
  3835. a:=aint(dword(-a));
  3836. end
  3837. end;
  3838. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3839. begin
  3840. // if cgsetflags or setflags then
  3841. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3842. list.concat(setoppostfix(
  3843. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3844. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3845. begin
  3846. //!!! ovloc.loc:=LOC_FLAGS;
  3847. case op of
  3848. OP_ADD:
  3849. //!!! ovloc.resflags:=F_CS;
  3850. ;
  3851. OP_SUB:
  3852. //!!! ovloc.resflags:=F_CC;
  3853. ;
  3854. end;
  3855. end;
  3856. end
  3857. else
  3858. begin
  3859. { there could be added some more sophisticated optimizations }
  3860. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3861. a_load_reg_reg(list,size,size,dst,dst)
  3862. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3863. a_load_const_reg(list,size,0,dst)
  3864. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3865. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3866. { we do this here instead in the peephole optimizer because
  3867. it saves us a register }
  3868. {$ifdef DUMMY}
  3869. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3870. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3871. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3872. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3873. begin
  3874. if l1>32 then{roozbeh does this ever happen?}
  3875. internalerror(200308296);
  3876. shifterop_reset(so);
  3877. so.shiftmode:=SM_LSL;
  3878. so.shiftimm:=l1;
  3879. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3880. end
  3881. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3882. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3883. begin
  3884. if l1>32 then{does this ever happen?}
  3885. internalerror(201205181);
  3886. shifterop_reset(so);
  3887. so.shiftmode:=SM_LSL;
  3888. so.shiftimm:=l1;
  3889. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3890. end
  3891. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,dst,dst) then
  3892. begin
  3893. { nothing to do on success }
  3894. end
  3895. {$endif DUMMY}
  3896. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3897. Just using mov x, #0 might allow some easier optimizations down the line. }
  3898. else if (op = OP_AND) and (dword(a)=0) then
  3899. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3900. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3901. else if (op = OP_AND) and (not(dword(a))=0) then
  3902. // do nothing
  3903. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3904. broader range of shifterconstants.}
  3905. {$ifdef DUMMY}
  3906. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3907. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3908. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3909. begin
  3910. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3911. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3912. end
  3913. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3914. not(cgsetflags or setflags) and
  3915. split_into_shifter_const(a, imm1, imm2) then
  3916. begin
  3917. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3918. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3919. end
  3920. {$endif DUMMY}
  3921. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3922. begin
  3923. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3924. end
  3925. else
  3926. begin
  3927. tmpreg:=getintregister(list,size);
  3928. a_load_const_reg(list,size,a,tmpreg);
  3929. a_op_reg_reg(list,op,size,tmpreg,dst);
  3930. end;
  3931. end;
  3932. maybeadjustresult(list,op,size,dst);
  3933. end;
  3934. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3935. begin
  3936. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3937. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3938. else
  3939. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3940. end;
  3941. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3942. var
  3943. l1,l2 : tasmlabel;
  3944. ai : taicpu;
  3945. begin
  3946. current_asmdata.getjumplabel(l1);
  3947. current_asmdata.getjumplabel(l2);
  3948. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3949. ai.is_jmp:=true;
  3950. list.concat(ai);
  3951. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3952. list.concat(taicpu.op_sym(A_B,l2));
  3953. cg.a_label(list,l1);
  3954. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3955. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3956. cg.a_label(list,l2);
  3957. end;
  3958. procedure tthumbcgarm.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  3959. var
  3960. tmpref : treference;
  3961. l : tasmlabel;
  3962. begin
  3963. { there is no branch instruction on thumb which allows big distances and which leaves LR as it is
  3964. and which allows to switch the instruction set }
  3965. { create const entry }
  3966. reference_reset(tmpref,4);
  3967. current_asmdata.getjumplabel(l);
  3968. tmpref.symbol:=l;
  3969. tmpref.base:=NR_PC;
  3970. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3971. list.concat(taicpu.op_reg_ref(A_LDR,NR_R0,tmpref));
  3972. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3973. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3974. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3975. { append const entry }
  3976. list.Concat(tai_align.Create(4));
  3977. list.Concat(tai_label.create(l));
  3978. list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(externalname)));
  3979. end;
  3980. procedure tthumb2cgarm.init_register_allocators;
  3981. begin
  3982. inherited init_register_allocators;
  3983. { currently, we save R14 always, so we can use it }
  3984. if (target_info.system<>system_arm_darwin) then
  3985. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3986. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3987. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3988. else
  3989. { r9 is not available on Darwin according to the llvm code generator }
  3990. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3991. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3992. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3993. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3994. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3995. if current_settings.fputype in [fpu_fpv4_s16,fpu_vfpv3_d16] then
  3996. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3997. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3998. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3999. ],first_mm_imreg,[])
  4000. else
  4001. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  4002. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  4003. end;
  4004. procedure tthumb2cgarm.done_register_allocators;
  4005. begin
  4006. rg[R_INTREGISTER].free;
  4007. rg[R_FPUREGISTER].free;
  4008. rg[R_MMREGISTER].free;
  4009. inherited done_register_allocators;
  4010. end;
  4011. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  4012. begin
  4013. list.concat(taicpu.op_reg(A_BLX, reg));
  4014. {
  4015. the compiler does not properly set this flag anymore in pass 1, and
  4016. for now we only need it after pass 2 (I hope) (JM)
  4017. if not(pi_do_call in current_procinfo.flags) then
  4018. internalerror(2003060703);
  4019. }
  4020. include(current_procinfo.flags,pi_do_call);
  4021. end;
  4022. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  4023. var
  4024. imm_shift : byte;
  4025. l : tasmlabel;
  4026. hr : treference;
  4027. begin
  4028. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  4029. internalerror(2002090902);
  4030. if is_thumb32_imm(a) then
  4031. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  4032. else if is_thumb32_imm(not(a)) then
  4033. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  4034. else if (a and $FFFF)=a then
  4035. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  4036. else
  4037. begin
  4038. reference_reset(hr,4);
  4039. current_asmdata.getjumplabel(l);
  4040. cg.a_label(current_procinfo.aktlocaldata,l);
  4041. hr.symboldata:=current_procinfo.aktlocaldata.last;
  4042. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  4043. hr.symbol:=l;
  4044. hr.base:=NR_PC;
  4045. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  4046. end;
  4047. end;
  4048. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  4049. var
  4050. oppostfix:toppostfix;
  4051. usedtmpref: treference;
  4052. tmpreg,tmpreg2 : tregister;
  4053. so : tshifterop;
  4054. dir : integer;
  4055. begin
  4056. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4057. FromSize := ToSize;
  4058. case FromSize of
  4059. { signed integer registers }
  4060. OS_8:
  4061. oppostfix:=PF_B;
  4062. OS_S8:
  4063. oppostfix:=PF_SB;
  4064. OS_16:
  4065. oppostfix:=PF_H;
  4066. OS_S16:
  4067. oppostfix:=PF_SH;
  4068. OS_32,
  4069. OS_S32:
  4070. oppostfix:=PF_None;
  4071. else
  4072. InternalError(200308299);
  4073. end;
  4074. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4075. begin
  4076. if target_info.endian=endian_big then
  4077. dir:=-1
  4078. else
  4079. dir:=1;
  4080. case FromSize of
  4081. OS_16,OS_S16:
  4082. begin
  4083. { only complicated references need an extra loadaddr }
  4084. if assigned(ref.symbol) or
  4085. (ref.index<>NR_NO) or
  4086. (ref.offset<-255) or
  4087. (ref.offset>4094) or
  4088. { sometimes the compiler reused registers }
  4089. (reg=ref.index) or
  4090. (reg=ref.base) then
  4091. begin
  4092. tmpreg2:=getintregister(list,OS_INT);
  4093. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4094. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4095. end
  4096. else
  4097. usedtmpref:=ref;
  4098. if target_info.endian=endian_big then
  4099. inc(usedtmpref.offset,1);
  4100. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4101. tmpreg:=getintregister(list,OS_INT);
  4102. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4103. inc(usedtmpref.offset,dir);
  4104. if FromSize=OS_16 then
  4105. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4106. else
  4107. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4108. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4109. end;
  4110. OS_32,OS_S32:
  4111. begin
  4112. tmpreg:=getintregister(list,OS_INT);
  4113. { only complicated references need an extra loadaddr }
  4114. if assigned(ref.symbol) or
  4115. (ref.index<>NR_NO) or
  4116. (ref.offset<-255) or
  4117. (ref.offset>4092) or
  4118. { sometimes the compiler reused registers }
  4119. (reg=ref.index) or
  4120. (reg=ref.base) then
  4121. begin
  4122. tmpreg2:=getintregister(list,OS_INT);
  4123. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4124. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4125. end
  4126. else
  4127. usedtmpref:=ref;
  4128. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4129. if ref.alignment=2 then
  4130. begin
  4131. if target_info.endian=endian_big then
  4132. inc(usedtmpref.offset,2);
  4133. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4134. inc(usedtmpref.offset,dir*2);
  4135. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4136. so.shiftimm:=16;
  4137. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4138. end
  4139. else
  4140. begin
  4141. if target_info.endian=endian_big then
  4142. inc(usedtmpref.offset,3);
  4143. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4144. inc(usedtmpref.offset,dir);
  4145. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4146. so.shiftimm:=8;
  4147. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4148. inc(usedtmpref.offset,dir);
  4149. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4150. so.shiftimm:=16;
  4151. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4152. inc(usedtmpref.offset,dir);
  4153. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4154. so.shiftimm:=24;
  4155. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4156. end;
  4157. end
  4158. else
  4159. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4160. end;
  4161. end
  4162. else
  4163. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4164. if (fromsize=OS_S8) and (tosize = OS_16) then
  4165. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4166. end;
  4167. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4168. begin
  4169. if op = OP_NOT then
  4170. begin
  4171. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4172. case size of
  4173. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4174. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4175. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4176. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4177. end;
  4178. end
  4179. else
  4180. inherited a_op_reg_reg(list, op, size, src, dst);
  4181. end;
  4182. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4183. var
  4184. shift, width : byte;
  4185. tmpreg : tregister;
  4186. so : tshifterop;
  4187. l1 : longint;
  4188. begin
  4189. ovloc.loc:=LOC_VOID;
  4190. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  4191. case op of
  4192. OP_ADD:
  4193. begin
  4194. op:=OP_SUB;
  4195. a:=aint(dword(-a));
  4196. end;
  4197. OP_SUB:
  4198. begin
  4199. op:=OP_ADD;
  4200. a:=aint(dword(-a));
  4201. end
  4202. end;
  4203. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4204. case op of
  4205. OP_NEG,OP_NOT,
  4206. OP_DIV,OP_IDIV:
  4207. internalerror(200308285);
  4208. OP_SHL:
  4209. begin
  4210. if a>32 then
  4211. internalerror(2014020703);
  4212. if a<>0 then
  4213. begin
  4214. shifterop_reset(so);
  4215. so.shiftmode:=SM_LSL;
  4216. so.shiftimm:=a;
  4217. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4218. end
  4219. else
  4220. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4221. end;
  4222. OP_ROL:
  4223. begin
  4224. if a>32 then
  4225. internalerror(2014020704);
  4226. if a<>0 then
  4227. begin
  4228. shifterop_reset(so);
  4229. so.shiftmode:=SM_ROR;
  4230. so.shiftimm:=32-a;
  4231. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4232. end
  4233. else
  4234. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4235. end;
  4236. OP_ROR:
  4237. begin
  4238. if a>32 then
  4239. internalerror(2014020705);
  4240. if a<>0 then
  4241. begin
  4242. shifterop_reset(so);
  4243. so.shiftmode:=SM_ROR;
  4244. so.shiftimm:=a;
  4245. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4246. end
  4247. else
  4248. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4249. end;
  4250. OP_SHR:
  4251. begin
  4252. if a>32 then
  4253. internalerror(200308292);
  4254. shifterop_reset(so);
  4255. if a<>0 then
  4256. begin
  4257. so.shiftmode:=SM_LSR;
  4258. so.shiftimm:=a;
  4259. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4260. end
  4261. else
  4262. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4263. end;
  4264. OP_SAR:
  4265. begin
  4266. if a>32 then
  4267. internalerror(200308295);
  4268. if a<>0 then
  4269. begin
  4270. shifterop_reset(so);
  4271. so.shiftmode:=SM_ASR;
  4272. so.shiftimm:=a;
  4273. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4274. end
  4275. else
  4276. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4277. end;
  4278. else
  4279. if (op in [OP_SUB, OP_ADD]) and
  4280. ((a < 0) or
  4281. (a > 4095)) then
  4282. begin
  4283. tmpreg:=getintregister(list,size);
  4284. a_load_const_reg(list, size, a, tmpreg);
  4285. if cgsetflags or setflags then
  4286. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4287. list.concat(setoppostfix(
  4288. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4289. end
  4290. else
  4291. begin
  4292. if cgsetflags or setflags then
  4293. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4294. list.concat(setoppostfix(
  4295. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4296. end;
  4297. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4298. begin
  4299. ovloc.loc:=LOC_FLAGS;
  4300. case op of
  4301. OP_ADD:
  4302. ovloc.resflags:=F_CS;
  4303. OP_SUB:
  4304. ovloc.resflags:=F_CC;
  4305. end;
  4306. end;
  4307. end
  4308. else
  4309. begin
  4310. { there could be added some more sophisticated optimizations }
  4311. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4312. a_load_reg_reg(list,size,size,src,dst)
  4313. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4314. a_load_const_reg(list,size,0,dst)
  4315. else if (op in [OP_IMUL]) and (a=-1) then
  4316. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4317. { we do this here instead in the peephole optimizer because
  4318. it saves us a register }
  4319. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4320. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4321. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4322. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4323. begin
  4324. if l1>32 then{roozbeh does this ever happen?}
  4325. internalerror(200308296);
  4326. shifterop_reset(so);
  4327. so.shiftmode:=SM_LSL;
  4328. so.shiftimm:=l1;
  4329. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4330. end
  4331. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4332. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4333. begin
  4334. if l1>32 then{does this ever happen?}
  4335. internalerror(201205181);
  4336. shifterop_reset(so);
  4337. so.shiftmode:=SM_LSL;
  4338. so.shiftimm:=l1;
  4339. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4340. end
  4341. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  4342. begin
  4343. { nothing to do on success }
  4344. end
  4345. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4346. Just using mov x, #0 might allow some easier optimizations down the line. }
  4347. else if (op = OP_AND) and (dword(a)=0) then
  4348. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4349. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4350. else if (op = OP_AND) and (not(dword(a))=0) then
  4351. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4352. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4353. broader range of shifterconstants.}
  4354. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4355. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4356. else if (op = OP_AND) and is_thumb32_imm(a) then
  4357. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4358. else if (op = OP_AND) and (a = $FFFF) then
  4359. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4360. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4361. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4362. else if (op = OP_AND) and is_continuous_mask(not(a), shift, width) then
  4363. begin
  4364. a_load_reg_reg(list,size,size,src,dst);
  4365. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4366. end
  4367. else
  4368. begin
  4369. tmpreg:=getintregister(list,size);
  4370. a_load_const_reg(list,size,a,tmpreg);
  4371. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4372. end;
  4373. end;
  4374. maybeadjustresult(list,op,size,dst);
  4375. end;
  4376. const
  4377. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4378. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4379. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4380. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4381. var
  4382. so : tshifterop;
  4383. tmpreg,overflowreg : tregister;
  4384. asmop : tasmop;
  4385. begin
  4386. ovloc.loc:=LOC_VOID;
  4387. case op of
  4388. OP_NEG,OP_NOT:
  4389. internalerror(200308286);
  4390. OP_ROL:
  4391. begin
  4392. if not(size in [OS_32,OS_S32]) then
  4393. internalerror(2008072801);
  4394. { simulate ROL by ror'ing 32-value }
  4395. tmpreg:=getintregister(list,OS_32);
  4396. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4397. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4398. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4399. end;
  4400. OP_ROR:
  4401. begin
  4402. if not(size in [OS_32,OS_S32]) then
  4403. internalerror(2008072802);
  4404. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4405. end;
  4406. OP_IMUL,
  4407. OP_MUL:
  4408. begin
  4409. if cgsetflags or setflags then
  4410. begin
  4411. overflowreg:=getintregister(list,size);
  4412. if op=OP_IMUL then
  4413. asmop:=A_SMULL
  4414. else
  4415. asmop:=A_UMULL;
  4416. { the arm doesn't allow that rd and rm are the same }
  4417. if dst=src2 then
  4418. begin
  4419. if dst<>src1 then
  4420. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4421. else
  4422. begin
  4423. tmpreg:=getintregister(list,size);
  4424. a_load_reg_reg(list,size,size,src2,dst);
  4425. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4426. end;
  4427. end
  4428. else
  4429. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4430. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4431. if op=OP_IMUL then
  4432. begin
  4433. shifterop_reset(so);
  4434. so.shiftmode:=SM_ASR;
  4435. so.shiftimm:=31;
  4436. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4437. end
  4438. else
  4439. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4440. ovloc.loc:=LOC_FLAGS;
  4441. ovloc.resflags:=F_NE;
  4442. end
  4443. else
  4444. begin
  4445. { the arm doesn't allow that rd and rm are the same }
  4446. if dst=src2 then
  4447. begin
  4448. if dst<>src1 then
  4449. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4450. else
  4451. begin
  4452. tmpreg:=getintregister(list,size);
  4453. a_load_reg_reg(list,size,size,src2,dst);
  4454. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4455. end;
  4456. end
  4457. else
  4458. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4459. end;
  4460. end;
  4461. else
  4462. begin
  4463. if cgsetflags or setflags then
  4464. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4465. {$ifdef dummy}
  4466. { R13 is not allowed for certain instruction operands }
  4467. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4468. begin
  4469. if getsupreg(dst)=RS_R13 then
  4470. begin
  4471. tmpreg:=getintregister(list,OS_INT);
  4472. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4473. dst:=tmpreg;
  4474. end;
  4475. if getsupreg(src1)=RS_R13 then
  4476. begin
  4477. tmpreg:=getintregister(list,OS_INT);
  4478. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4479. src1:=tmpreg;
  4480. end;
  4481. end;
  4482. {$endif}
  4483. list.concat(setoppostfix(
  4484. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4485. end;
  4486. end;
  4487. maybeadjustresult(list,op,size,dst);
  4488. end;
  4489. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4490. var item: taicpu;
  4491. begin
  4492. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4493. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4494. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4495. end;
  4496. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4497. var
  4498. ref : treference;
  4499. shift : byte;
  4500. firstfloatreg,lastfloatreg,
  4501. r : byte;
  4502. regs : tcpuregisterset;
  4503. stackmisalignment: pint;
  4504. begin
  4505. LocalSize:=align(LocalSize,4);
  4506. { call instruction does not put anything on the stack }
  4507. stackmisalignment:=0;
  4508. if not(nostackframe) then
  4509. begin
  4510. firstfloatreg:=RS_NO;
  4511. lastfloatreg:=RS_NO;
  4512. { save floating point registers? }
  4513. for r:=RS_F0 to RS_F7 do
  4514. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4515. begin
  4516. if firstfloatreg=RS_NO then
  4517. firstfloatreg:=r;
  4518. lastfloatreg:=r;
  4519. inc(stackmisalignment,12);
  4520. end;
  4521. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4522. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4523. begin
  4524. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4525. a_reg_alloc(list,NR_R12);
  4526. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4527. end;
  4528. { save int registers }
  4529. reference_reset(ref,4);
  4530. ref.index:=NR_STACK_POINTER_REG;
  4531. ref.addressmode:=AM_PREINDEXED;
  4532. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4533. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4534. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4535. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4536. include(regs,RS_R14);
  4537. if regs<>[] then
  4538. begin
  4539. for r:=RS_R0 to RS_R15 do
  4540. if (r in regs) then
  4541. inc(stackmisalignment,4);
  4542. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4543. end;
  4544. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4545. begin
  4546. { the framepointer now points to the saved R15, so the saved
  4547. framepointer is at R11-12 (for get_caller_frame) }
  4548. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4549. a_reg_dealloc(list,NR_R12);
  4550. end;
  4551. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4552. if (LocalSize<>0) or
  4553. ((stackmisalignment<>0) and
  4554. ((pi_do_call in current_procinfo.flags) or
  4555. (po_assembler in current_procinfo.procdef.procoptions))) then
  4556. begin
  4557. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4558. if not(is_shifter_const(localsize,shift)) then
  4559. begin
  4560. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4561. a_reg_alloc(list,NR_R12);
  4562. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4563. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4564. a_reg_dealloc(list,NR_R12);
  4565. end
  4566. else
  4567. begin
  4568. a_reg_dealloc(list,NR_R12);
  4569. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4570. end;
  4571. end;
  4572. if firstfloatreg<>RS_NO then
  4573. begin
  4574. reference_reset(ref,4);
  4575. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4576. begin
  4577. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4578. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4579. ref.base:=NR_R12;
  4580. end
  4581. else
  4582. begin
  4583. ref.base:=current_procinfo.framepointer;
  4584. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4585. end;
  4586. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4587. lastfloatreg-firstfloatreg+1,ref));
  4588. end;
  4589. end;
  4590. end;
  4591. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4592. var
  4593. ref : treference;
  4594. firstfloatreg,lastfloatreg,
  4595. r : byte;
  4596. shift : byte;
  4597. regs : tcpuregisterset;
  4598. LocalSize : longint;
  4599. stackmisalignment: pint;
  4600. begin
  4601. if not(nostackframe) then
  4602. begin
  4603. stackmisalignment:=0;
  4604. { restore floating point register }
  4605. firstfloatreg:=RS_NO;
  4606. lastfloatreg:=RS_NO;
  4607. { save floating point registers? }
  4608. for r:=RS_F0 to RS_F7 do
  4609. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4610. begin
  4611. if firstfloatreg=RS_NO then
  4612. firstfloatreg:=r;
  4613. lastfloatreg:=r;
  4614. { floating point register space is already included in
  4615. localsize below by calc_stackframe_size
  4616. inc(stackmisalignment,12);
  4617. }
  4618. end;
  4619. if firstfloatreg<>RS_NO then
  4620. begin
  4621. reference_reset(ref,4);
  4622. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4623. begin
  4624. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4625. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4626. ref.base:=NR_R12;
  4627. end
  4628. else
  4629. begin
  4630. ref.base:=current_procinfo.framepointer;
  4631. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4632. end;
  4633. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4634. lastfloatreg-firstfloatreg+1,ref));
  4635. end;
  4636. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4637. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4638. begin
  4639. exclude(regs,RS_R14);
  4640. include(regs,RS_R15);
  4641. end;
  4642. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4643. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4644. for r:=RS_R0 to RS_R15 do
  4645. if (r in regs) then
  4646. inc(stackmisalignment,4);
  4647. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4648. LocalSize:=current_procinfo.calc_stackframe_size;
  4649. if (LocalSize<>0) or
  4650. ((stackmisalignment<>0) and
  4651. ((pi_do_call in current_procinfo.flags) or
  4652. (po_assembler in current_procinfo.procdef.procoptions))) then
  4653. begin
  4654. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4655. if not(is_shifter_const(LocalSize,shift)) then
  4656. begin
  4657. a_reg_alloc(list,NR_R12);
  4658. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4659. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4660. a_reg_dealloc(list,NR_R12);
  4661. end
  4662. else
  4663. begin
  4664. a_reg_dealloc(list,NR_R12);
  4665. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4666. end;
  4667. end;
  4668. if regs=[] then
  4669. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  4670. else
  4671. begin
  4672. reference_reset(ref,4);
  4673. ref.index:=NR_STACK_POINTER_REG;
  4674. ref.addressmode:=AM_PREINDEXED;
  4675. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4676. end;
  4677. end
  4678. else
  4679. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  4680. end;
  4681. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4682. var
  4683. tmpreg : tregister;
  4684. tmpref : treference;
  4685. l : tasmlabel;
  4686. so: tshifterop;
  4687. begin
  4688. tmpreg:=NR_NO;
  4689. { Be sure to have a base register }
  4690. if (ref.base=NR_NO) then
  4691. begin
  4692. if ref.shiftmode<>SM_None then
  4693. internalerror(2014020706);
  4694. ref.base:=ref.index;
  4695. ref.index:=NR_NO;
  4696. end;
  4697. { absolute symbols can't be handled directly, we've to store the symbol reference
  4698. in the text segment and access it pc relative
  4699. For now, we assume that references where base or index equals to PC are already
  4700. relative, all other references are assumed to be absolute and thus they need
  4701. to be handled extra.
  4702. A proper solution would be to change refoptions to a set and store the information
  4703. if the symbol is absolute or relative there.
  4704. }
  4705. if (assigned(ref.symbol) and
  4706. not(is_pc(ref.base)) and
  4707. not(is_pc(ref.index))
  4708. ) or
  4709. { [#xxx] isn't a valid address operand }
  4710. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4711. //(ref.offset<-4095) or
  4712. (ref.offset<-255) or
  4713. (ref.offset>4095) or
  4714. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4715. ((ref.offset<-255) or
  4716. (ref.offset>255)
  4717. )
  4718. ) or
  4719. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4720. ((ref.offset<-1020) or
  4721. (ref.offset>1020) or
  4722. ((abs(ref.offset) mod 4)<>0) or
  4723. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4724. assigned(ref.symbol)
  4725. )
  4726. ) then
  4727. begin
  4728. reference_reset(tmpref,4);
  4729. { load symbol }
  4730. tmpreg:=getintregister(list,OS_INT);
  4731. if assigned(ref.symbol) then
  4732. begin
  4733. current_asmdata.getjumplabel(l);
  4734. cg.a_label(current_procinfo.aktlocaldata,l);
  4735. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4736. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4737. { load consts entry }
  4738. tmpref.symbol:=l;
  4739. tmpref.base:=NR_R15;
  4740. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4741. { in case of LDF/STF, we got rid of the NR_R15 }
  4742. if is_pc(ref.base) then
  4743. ref.base:=NR_NO;
  4744. if is_pc(ref.index) then
  4745. ref.index:=NR_NO;
  4746. end
  4747. else
  4748. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4749. if (ref.base<>NR_NO) then
  4750. begin
  4751. if ref.index<>NR_NO then
  4752. begin
  4753. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4754. ref.base:=tmpreg;
  4755. end
  4756. else
  4757. begin
  4758. ref.index:=tmpreg;
  4759. ref.shiftimm:=0;
  4760. ref.signindex:=1;
  4761. ref.shiftmode:=SM_None;
  4762. end;
  4763. end
  4764. else
  4765. ref.base:=tmpreg;
  4766. ref.offset:=0;
  4767. ref.symbol:=nil;
  4768. end;
  4769. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4770. begin
  4771. if tmpreg<>NR_NO then
  4772. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4773. else
  4774. begin
  4775. tmpreg:=getintregister(list,OS_ADDR);
  4776. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4777. ref.base:=tmpreg;
  4778. end;
  4779. ref.offset:=0;
  4780. end;
  4781. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4782. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4783. begin
  4784. tmpreg:=getintregister(list,OS_ADDR);
  4785. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4786. ref.base := tmpreg;
  4787. end;
  4788. { floating point operations have only limited references
  4789. we expect here, that a base is already set }
  4790. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  4791. begin
  4792. if ref.shiftmode<>SM_none then
  4793. internalerror(200309121);
  4794. if tmpreg<>NR_NO then
  4795. begin
  4796. if ref.base=tmpreg then
  4797. begin
  4798. if ref.signindex<0 then
  4799. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4800. else
  4801. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4802. ref.index:=NR_NO;
  4803. end
  4804. else
  4805. begin
  4806. if ref.index<>tmpreg then
  4807. internalerror(200403161);
  4808. if ref.signindex<0 then
  4809. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4810. else
  4811. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4812. ref.base:=tmpreg;
  4813. ref.index:=NR_NO;
  4814. end;
  4815. end
  4816. else
  4817. begin
  4818. tmpreg:=getintregister(list,OS_ADDR);
  4819. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4820. ref.base:=tmpreg;
  4821. ref.index:=NR_NO;
  4822. end;
  4823. end;
  4824. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4825. Result := ref;
  4826. end;
  4827. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4828. var
  4829. instr: taicpu;
  4830. begin
  4831. if (fromsize=OS_F32) and
  4832. (tosize=OS_F32) then
  4833. begin
  4834. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4835. list.Concat(instr);
  4836. add_move_instruction(instr);
  4837. end
  4838. else if (fromsize=OS_F64) and
  4839. (tosize=OS_F64) then
  4840. begin
  4841. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4842. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4843. end
  4844. else if (fromsize=OS_F32) and
  4845. (tosize=OS_F64) then
  4846. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4847. begin
  4848. //list.concat(nil);
  4849. end;
  4850. end;
  4851. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4852. begin
  4853. if fromsize=OS_F32 then
  4854. handle_load_store(list,A_VLDR,PF_F32,reg,ref)
  4855. else
  4856. handle_load_store(list,A_VLDR,PF_F64,reg,ref);
  4857. end;
  4858. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4859. begin
  4860. if fromsize=OS_F32 then
  4861. handle_load_store(list,A_VSTR,PF_F32,reg,ref)
  4862. else
  4863. handle_load_store(list,A_VSTR,PF_F64,reg,ref);
  4864. end;
  4865. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4866. begin
  4867. if //(shuffle=nil) and
  4868. (tosize=OS_F32) then
  4869. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4870. else
  4871. internalerror(2012100813);
  4872. end;
  4873. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4874. begin
  4875. if //(shuffle=nil) and
  4876. (fromsize=OS_F32) then
  4877. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  4878. else
  4879. internalerror(2012100814);
  4880. end;
  4881. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4882. var tmpreg: tregister;
  4883. begin
  4884. case op of
  4885. OP_NEG:
  4886. begin
  4887. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4888. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4889. tmpreg:=cg.getintregister(list,OS_32);
  4890. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4891. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4892. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4893. end;
  4894. else
  4895. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4896. end;
  4897. end;
  4898. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4899. begin
  4900. case op of
  4901. OP_NEG:
  4902. begin
  4903. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4904. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4905. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4906. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4907. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4908. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4909. end;
  4910. OP_NOT:
  4911. begin
  4912. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4913. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4914. end;
  4915. OP_AND,OP_OR,OP_XOR:
  4916. begin
  4917. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4918. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4919. end;
  4920. OP_ADD:
  4921. begin
  4922. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4923. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4924. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4925. end;
  4926. OP_SUB:
  4927. begin
  4928. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4929. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4930. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4931. end;
  4932. else
  4933. internalerror(2003083101);
  4934. end;
  4935. end;
  4936. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4937. var
  4938. tmpreg : tregister;
  4939. b : byte;
  4940. begin
  4941. case op of
  4942. OP_AND,OP_OR,OP_XOR:
  4943. begin
  4944. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4945. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4946. end;
  4947. OP_ADD:
  4948. begin
  4949. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4950. begin
  4951. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4952. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4953. end
  4954. else
  4955. begin
  4956. tmpreg:=cg.getintregister(list,OS_32);
  4957. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4958. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4959. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4960. end;
  4961. tmpreg:=cg.getintregister(list,OS_32);
  4962. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4963. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4964. end;
  4965. OP_SUB:
  4966. begin
  4967. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4968. begin
  4969. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4970. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4971. end
  4972. else
  4973. begin
  4974. tmpreg:=cg.getintregister(list,OS_32);
  4975. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4976. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4977. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4978. end;
  4979. tmpreg:=cg.getintregister(list,OS_32);
  4980. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4981. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4982. end;
  4983. else
  4984. internalerror(2003083101);
  4985. end;
  4986. end;
  4987. procedure create_codegen;
  4988. begin
  4989. if GenerateThumb2Code then
  4990. begin
  4991. cg:=tthumb2cgarm.create;
  4992. cg64:=tthumb2cg64farm.create;
  4993. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4994. end
  4995. else if GenerateThumbCode then
  4996. begin
  4997. cg:=tthumbcgarm.create;
  4998. cg64:=tthumbcg64farm.create;
  4999. // casmoptimizer:=TCpuThumbAsmOptimizer;
  5000. end
  5001. else
  5002. begin
  5003. cg:=tarmcgarm.create;
  5004. cg64:=tarmcg64farm.create;
  5005. casmoptimizer:=TCpuAsmOptimizer;
  5006. end;
  5007. end;
  5008. end.