cgcpu.pas 218 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438
  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(taicpu.op_ref_regset(A_VSTM,ref,R_MMREGISTER,R_SUBFD,mmregs));
  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(taicpu.op_ref_regset(A_VLDM,ref,R_MMREGISTER,R_SUBFD,mmregs));
  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_VMOV,A_VCVT,A_NONE,A_NONE,A_NONE),
  2671. (A_VCVT,A_VMOV,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. function get_scalar_mm_prefix(fromsize,tosize : tcgsize) : TOpPostfix;
  2681. const
  2682. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of TOpPostfix = (
  2683. (PF_F32, PF_F32F64,PF_None,PF_None,PF_None),
  2684. (PF_F64F32,PF_F64, PF_None,PF_None,PF_None),
  2685. (PF_None, PF_None, PF_None,PF_None,PF_None),
  2686. (PF_None, PF_None, PF_None,PF_None,PF_None),
  2687. (PF_None, PF_None, PF_None,PF_None,PF_None));
  2688. begin
  2689. result:=convertop[fromsize,tosize];
  2690. end;
  2691. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2692. var
  2693. instr: taicpu;
  2694. begin
  2695. if (shuffle=nil) or shufflescalar(shuffle) then
  2696. instr:=setoppostfix(taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1),get_scalar_mm_prefix(tosize,fromsize))
  2697. else
  2698. internalerror(2009112407);
  2699. list.concat(instr);
  2700. case instr.opcode of
  2701. A_VMOV:
  2702. add_move_instruction(instr);
  2703. end;
  2704. end;
  2705. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2706. var
  2707. intreg,
  2708. tmpmmreg : tregister;
  2709. reg64 : tregister64;
  2710. begin
  2711. if assigned(shuffle) and
  2712. not(shufflescalar(shuffle)) then
  2713. internalerror(2009112413);
  2714. case fromsize of
  2715. OS_32,OS_S32:
  2716. begin
  2717. fromsize:=OS_F32;
  2718. { since we are loading an integer, no conversion may be required }
  2719. if (fromsize<>tosize) then
  2720. internalerror(2009112801);
  2721. end;
  2722. OS_64,OS_S64:
  2723. begin
  2724. fromsize:=OS_F64;
  2725. { since we are loading an integer, no conversion may be required }
  2726. if (fromsize<>tosize) then
  2727. internalerror(2009112901);
  2728. end;
  2729. end;
  2730. if (fromsize<>tosize) then
  2731. tmpmmreg:=getmmregister(list,fromsize)
  2732. else
  2733. tmpmmreg:=reg;
  2734. if (ref.alignment in [1,2]) then
  2735. begin
  2736. case fromsize of
  2737. OS_F32:
  2738. begin
  2739. intreg:=getintregister(list,OS_32);
  2740. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2741. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2742. end;
  2743. OS_F64:
  2744. begin
  2745. reg64.reglo:=getintregister(list,OS_32);
  2746. reg64.reghi:=getintregister(list,OS_32);
  2747. cg64.a_load64_ref_reg(list,ref,reg64);
  2748. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2749. end;
  2750. else
  2751. internalerror(2009112412);
  2752. end;
  2753. end
  2754. else
  2755. begin
  2756. handle_load_store(list,A_VLDR,PF_None,tmpmmreg,ref);
  2757. end;
  2758. if (tmpmmreg<>reg) then
  2759. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2760. end;
  2761. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2762. var
  2763. intreg,
  2764. tmpmmreg : tregister;
  2765. reg64 : tregister64;
  2766. begin
  2767. if assigned(shuffle) and
  2768. not(shufflescalar(shuffle)) then
  2769. internalerror(2009112416);
  2770. case tosize of
  2771. OS_32,OS_S32:
  2772. begin
  2773. tosize:=OS_F32;
  2774. { since we are loading an integer, no conversion may be required }
  2775. if (fromsize<>tosize) then
  2776. internalerror(2009112801);
  2777. end;
  2778. OS_64,OS_S64:
  2779. begin
  2780. tosize:=OS_F64;
  2781. { since we are loading an integer, no conversion may be required }
  2782. if (fromsize<>tosize) then
  2783. internalerror(2009112901);
  2784. end;
  2785. end;
  2786. if (fromsize<>tosize) then
  2787. begin
  2788. tmpmmreg:=getmmregister(list,tosize);
  2789. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2790. end
  2791. else
  2792. tmpmmreg:=reg;
  2793. if (ref.alignment in [1,2]) then
  2794. begin
  2795. case tosize of
  2796. OS_F32:
  2797. begin
  2798. intreg:=getintregister(list,OS_32);
  2799. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2800. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2801. end;
  2802. OS_F64:
  2803. begin
  2804. reg64.reglo:=getintregister(list,OS_32);
  2805. reg64.reghi:=getintregister(list,OS_32);
  2806. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2807. cg64.a_load64_reg_ref(list,reg64,ref);
  2808. end;
  2809. else
  2810. internalerror(2009112417);
  2811. end;
  2812. end
  2813. else
  2814. begin
  2815. handle_load_store(list,A_VSTR,PF_None,tmpmmreg,ref);
  2816. end;
  2817. end;
  2818. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2819. begin
  2820. { this code can only be used to transfer raw data, not to perform
  2821. conversions }
  2822. if (tosize<>OS_F32) then
  2823. internalerror(2009112419);
  2824. if not(fromsize in [OS_32,OS_S32]) then
  2825. internalerror(2009112420);
  2826. if assigned(shuffle) and
  2827. not shufflescalar(shuffle) then
  2828. internalerror(2009112516);
  2829. list.concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg));
  2830. end;
  2831. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2832. begin
  2833. { this code can only be used to transfer raw data, not to perform
  2834. conversions }
  2835. if (fromsize<>OS_F32) then
  2836. internalerror(2009112430);
  2837. if not(tosize in [OS_32,OS_S32]) then
  2838. internalerror(2009112420);
  2839. if assigned(shuffle) and
  2840. not shufflescalar(shuffle) then
  2841. internalerror(2009112514);
  2842. list.concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg));
  2843. end;
  2844. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2845. var
  2846. tmpreg: tregister;
  2847. begin
  2848. { the vfp doesn't support xor nor any other logical operation, but
  2849. this routine is used to initialise global mm regvars. We can
  2850. easily initialise an mm reg with 0 though. }
  2851. case op of
  2852. OP_XOR:
  2853. begin
  2854. if (src<>dst) or
  2855. (reg_cgsize(src)<>size) or
  2856. assigned(shuffle) then
  2857. internalerror(2009112907);
  2858. tmpreg:=getintregister(list,OS_32);
  2859. a_load_const_reg(list,OS_32,0,tmpreg);
  2860. case size of
  2861. OS_F32:
  2862. list.concat(taicpu.op_reg_reg(A_VMOV,dst,tmpreg));
  2863. OS_F64:
  2864. list.concat(taicpu.op_reg_reg_reg(A_VMOV,dst,tmpreg,tmpreg));
  2865. else
  2866. internalerror(2009112908);
  2867. end;
  2868. end
  2869. else
  2870. internalerror(2009112906);
  2871. end;
  2872. end;
  2873. procedure tbasecgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2874. procedure loadvmttor12;
  2875. var
  2876. tmpref,
  2877. href : treference;
  2878. extrareg : boolean;
  2879. l : TAsmLabel;
  2880. begin
  2881. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2882. if GenerateThumbCode then
  2883. begin
  2884. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2885. begin
  2886. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2887. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2888. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2889. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2890. end
  2891. else
  2892. begin
  2893. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2894. { create consts entry }
  2895. reference_reset(tmpref,4);
  2896. current_asmdata.getjumplabel(l);
  2897. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2898. cg.a_label(current_procinfo.aktlocaldata,l);
  2899. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2900. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2901. tmpref.symbol:=l;
  2902. tmpref.base:=NR_PC;
  2903. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2904. href.offset:=0;
  2905. href.index:=NR_R1;
  2906. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2907. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2908. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2909. end;
  2910. end
  2911. else
  2912. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2913. end;
  2914. procedure op_onr12methodaddr;
  2915. var
  2916. tmpref,
  2917. href : treference;
  2918. l : TAsmLabel;
  2919. begin
  2920. if (procdef.extnumber=$ffff) then
  2921. Internalerror(200006139);
  2922. if GenerateThumbCode then
  2923. begin
  2924. reference_reset_base(href,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2925. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2926. begin
  2927. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2928. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  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]));
  2932. end
  2933. else
  2934. begin
  2935. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2936. { create consts entry }
  2937. reference_reset(tmpref,4);
  2938. current_asmdata.getjumplabel(l);
  2939. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2940. cg.a_label(current_procinfo.aktlocaldata,l);
  2941. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2942. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2943. tmpref.symbol:=l;
  2944. tmpref.base:=NR_PC;
  2945. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2946. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2947. href.offset:=0;
  2948. href.base:=NR_R0;
  2949. href.index:=NR_R1;
  2950. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2951. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2952. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2953. end;
  2954. end
  2955. else
  2956. begin
  2957. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2958. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2959. end;
  2960. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2961. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12))
  2962. else
  2963. list.concat(taicpu.op_reg(A_BX,NR_R12));
  2964. end;
  2965. var
  2966. make_global : boolean;
  2967. tmpref : treference;
  2968. l : TAsmLabel;
  2969. begin
  2970. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2971. Internalerror(200006137);
  2972. if not assigned(procdef.struct) or
  2973. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2974. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2975. Internalerror(200006138);
  2976. if procdef.owner.symtabletype<>ObjectSymtable then
  2977. Internalerror(200109191);
  2978. if GenerateThumbCode or GenerateThumb2Code then
  2979. list.concat(tai_directive.Create(asd_thumb_func,''));
  2980. make_global:=false;
  2981. if (not current_module.is_unit) or
  2982. create_smartlink or
  2983. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2984. make_global:=true;
  2985. if make_global then
  2986. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2987. else
  2988. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2989. { the wrapper might need aktlocaldata for the additional data to
  2990. load the constant }
  2991. current_procinfo:=cprocinfo.create(nil);
  2992. { set param1 interface to self }
  2993. g_adjust_self_value(list,procdef,ioffset);
  2994. { case 4 }
  2995. if (po_virtualmethod in procdef.procoptions) and
  2996. not is_objectpascal_helper(procdef.struct) then
  2997. begin
  2998. loadvmttor12;
  2999. op_onr12methodaddr;
  3000. end
  3001. { case 0 }
  3002. else if GenerateThumbCode then
  3003. begin
  3004. { bl cannot be used here because it destroys lr }
  3005. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3006. { create consts entry }
  3007. reference_reset(tmpref,4);
  3008. current_asmdata.getjumplabel(l);
  3009. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3010. cg.a_label(current_procinfo.aktlocaldata,l);
  3011. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3012. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3013. tmpref.symbol:=l;
  3014. tmpref.base:=NR_PC;
  3015. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  3016. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3017. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3018. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3019. end
  3020. else
  3021. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3022. list.concatlist(current_procinfo.aktlocaldata);
  3023. current_procinfo.Free;
  3024. current_procinfo:=nil;
  3025. list.concat(Tai_symbol_end.Createname(labelname));
  3026. end;
  3027. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3028. const
  3029. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3030. begin
  3031. if (op in overflowops) and
  3032. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3033. a_load_reg_reg(list,OS_32,size,dst,dst);
  3034. end;
  3035. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3036. procedure checkreg(var reg : TRegister);
  3037. var
  3038. tmpreg : TRegister;
  3039. begin
  3040. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3041. (getsupreg(reg)=RS_R15) then
  3042. begin
  3043. tmpreg:=getintregister(list,OS_INT);
  3044. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3045. reg:=tmpreg;
  3046. end;
  3047. end;
  3048. begin
  3049. checkreg(op1);
  3050. checkreg(op2);
  3051. checkreg(op3);
  3052. checkreg(op4);
  3053. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3054. end;
  3055. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3056. begin
  3057. case op of
  3058. OP_NEG:
  3059. begin
  3060. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3061. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3062. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3063. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3064. end;
  3065. OP_NOT:
  3066. begin
  3067. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3068. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3069. end;
  3070. else
  3071. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3072. end;
  3073. end;
  3074. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3075. begin
  3076. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3077. end;
  3078. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3079. var
  3080. ovloc : tlocation;
  3081. begin
  3082. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3083. end;
  3084. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3085. var
  3086. ovloc : tlocation;
  3087. begin
  3088. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3089. end;
  3090. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3091. begin
  3092. { this code can only be used to transfer raw data, not to perform
  3093. conversions }
  3094. if (mmsize<>OS_F64) then
  3095. internalerror(2009112405);
  3096. list.concat(taicpu.op_reg_reg_reg(A_VMOV,mmreg,intreg.reglo,intreg.reghi));
  3097. end;
  3098. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3099. begin
  3100. { this code can only be used to transfer raw data, not to perform
  3101. conversions }
  3102. if (mmsize<>OS_F64) then
  3103. internalerror(2009112406);
  3104. list.concat(taicpu.op_reg_reg_reg(A_VMOV,intreg.reglo,intreg.reghi,mmreg));
  3105. end;
  3106. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3107. var
  3108. tmpreg : tregister;
  3109. b : byte;
  3110. begin
  3111. ovloc.loc:=LOC_VOID;
  3112. case op of
  3113. OP_NEG,
  3114. OP_NOT :
  3115. internalerror(2012022501);
  3116. end;
  3117. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3118. begin
  3119. case op of
  3120. OP_ADD:
  3121. begin
  3122. if is_shifter_const(lo(value),b) then
  3123. begin
  3124. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3125. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3126. end
  3127. else
  3128. begin
  3129. tmpreg:=cg.getintregister(list,OS_32);
  3130. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3131. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3132. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3133. end;
  3134. if is_shifter_const(hi(value),b) then
  3135. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3136. else
  3137. begin
  3138. tmpreg:=cg.getintregister(list,OS_32);
  3139. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3140. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3141. end;
  3142. end;
  3143. OP_SUB:
  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_SUB,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_SUB,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_SBC,regdst.reghi,regsrc.reghi,aint(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_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3164. end;
  3165. end;
  3166. else
  3167. internalerror(200502131);
  3168. end;
  3169. if size=OS_64 then
  3170. begin
  3171. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3172. ovloc.loc:=LOC_FLAGS;
  3173. case op of
  3174. OP_ADD:
  3175. ovloc.resflags:=F_CS;
  3176. OP_SUB:
  3177. ovloc.resflags:=F_CC;
  3178. end;
  3179. end;
  3180. end
  3181. else
  3182. begin
  3183. case op of
  3184. OP_AND,OP_OR,OP_XOR:
  3185. begin
  3186. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3187. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3188. end;
  3189. OP_ADD:
  3190. begin
  3191. if is_shifter_const(aint(lo(value)),b) then
  3192. begin
  3193. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3194. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3195. end
  3196. else
  3197. begin
  3198. tmpreg:=cg.getintregister(list,OS_32);
  3199. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3200. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3201. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3202. end;
  3203. if is_shifter_const(aint(hi(value)),b) then
  3204. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3205. else
  3206. begin
  3207. tmpreg:=cg.getintregister(list,OS_32);
  3208. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3209. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3210. end;
  3211. end;
  3212. OP_SUB:
  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_SUB,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_SUB,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_SBC,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,hi(value),tmpreg);
  3232. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3233. end;
  3234. end;
  3235. else
  3236. internalerror(2003083101);
  3237. end;
  3238. end;
  3239. end;
  3240. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3241. begin
  3242. ovloc.loc:=LOC_VOID;
  3243. case op of
  3244. OP_NEG,
  3245. OP_NOT :
  3246. internalerror(2012022502);
  3247. end;
  3248. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3249. begin
  3250. case op of
  3251. OP_ADD:
  3252. begin
  3253. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3254. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3255. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3256. end;
  3257. OP_SUB:
  3258. begin
  3259. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3260. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3261. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3262. end;
  3263. else
  3264. internalerror(2003083101);
  3265. end;
  3266. if size=OS_64 then
  3267. begin
  3268. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3269. ovloc.loc:=LOC_FLAGS;
  3270. case op of
  3271. OP_ADD:
  3272. ovloc.resflags:=F_CS;
  3273. OP_SUB:
  3274. ovloc.resflags:=F_CC;
  3275. end;
  3276. end;
  3277. end
  3278. else
  3279. begin
  3280. case op of
  3281. OP_AND,OP_OR,OP_XOR:
  3282. begin
  3283. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3284. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3285. end;
  3286. OP_ADD:
  3287. begin
  3288. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3289. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3290. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3291. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3292. end;
  3293. OP_SUB:
  3294. begin
  3295. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3296. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3297. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3298. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3299. end;
  3300. else
  3301. internalerror(2003083101);
  3302. end;
  3303. end;
  3304. end;
  3305. procedure tthumbcgarm.init_register_allocators;
  3306. begin
  3307. inherited init_register_allocators;
  3308. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3309. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3310. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3311. else
  3312. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3313. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3314. end;
  3315. procedure tthumbcgarm.done_register_allocators;
  3316. begin
  3317. rg[R_INTREGISTER].free;
  3318. rg[R_FPUREGISTER].free;
  3319. rg[R_MMREGISTER].free;
  3320. inherited done_register_allocators;
  3321. end;
  3322. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3323. var
  3324. ref : treference;
  3325. shift : byte;
  3326. r : byte;
  3327. regs, saveregs : tcpuregisterset;
  3328. r7offset,
  3329. stackmisalignment : pint;
  3330. postfix: toppostfix;
  3331. registerarea,
  3332. imm1, imm2: DWord;
  3333. stack_parameters: Boolean;
  3334. begin
  3335. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3336. LocalSize:=align(LocalSize,4);
  3337. { call instruction does not put anything on the stack }
  3338. stackmisalignment:=0;
  3339. if not(nostackframe) then
  3340. begin
  3341. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3342. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3343. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3344. { save int registers }
  3345. reference_reset(ref,4);
  3346. ref.index:=NR_STACK_POINTER_REG;
  3347. ref.addressmode:=AM_PREINDEXED;
  3348. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3349. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3350. begin
  3351. //!!!! a_reg_alloc(list,NR_R12);
  3352. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3353. end;
  3354. { the (old) ARM APCS requires saving both the stack pointer (to
  3355. crawl the stack) and the PC (to identify the function this
  3356. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3357. and R15 -- still needs updating for EABI and Darwin, they don't
  3358. need that }
  3359. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3360. regs:=regs+[RS_R7,RS_R14]
  3361. else
  3362. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3363. include(regs,RS_R14);
  3364. { safely estimate stack size }
  3365. if localsize+current_settings.alignment.localalignmax+4>508 then
  3366. begin
  3367. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3368. include(regs,RS_R4);
  3369. end;
  3370. registerarea:=0;
  3371. if regs<>[] then
  3372. begin
  3373. for r:=RS_R0 to RS_R15 do
  3374. if r in regs then
  3375. inc(registerarea,4);
  3376. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3377. end;
  3378. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3379. if stack_parameters or (LocalSize<>0) or
  3380. ((stackmisalignment<>0) and
  3381. ((pi_do_call in current_procinfo.flags) or
  3382. (po_assembler in current_procinfo.procdef.procoptions))) then
  3383. begin
  3384. { do we access stack parameters?
  3385. if yes, the previously estimated stacksize must be used }
  3386. if stack_parameters then
  3387. begin
  3388. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  3389. begin
  3390. writeln(localsize);
  3391. writeln(tarmprocinfo(current_procinfo).stackframesize);
  3392. internalerror(2013040601);
  3393. end
  3394. else
  3395. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  3396. end
  3397. else
  3398. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3399. if localsize<508 then
  3400. begin
  3401. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3402. end
  3403. else if localsize<=1016 then
  3404. begin
  3405. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3406. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3407. end
  3408. else
  3409. begin
  3410. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3411. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3412. include(regs,RS_R4);
  3413. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3414. //!!!! a_reg_alloc(list,NR_R12);
  3415. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3416. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3417. //!!!! a_reg_dealloc(list,NR_R12);
  3418. end;
  3419. end;
  3420. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3421. begin
  3422. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3423. end;
  3424. end;
  3425. end;
  3426. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3427. var
  3428. ref : treference;
  3429. LocalSize : longint;
  3430. r,
  3431. shift : byte;
  3432. saveregs,
  3433. regs : tcpuregisterset;
  3434. registerarea : DWord;
  3435. stackmisalignment: pint;
  3436. imm1, imm2: DWord;
  3437. stack_parameters : Boolean;
  3438. begin
  3439. if not(nostackframe) then
  3440. begin
  3441. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3442. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3443. include(regs,RS_R15);
  3444. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3445. include(regs,getsupreg(current_procinfo.framepointer));
  3446. registerarea:=0;
  3447. for r:=RS_R0 to RS_R15 do
  3448. if r in regs then
  3449. inc(registerarea,4);
  3450. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3451. LocalSize:=current_procinfo.calc_stackframe_size;
  3452. if stack_parameters then
  3453. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  3454. else
  3455. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3456. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3457. (target_info.system in systems_darwin) then
  3458. begin
  3459. if (LocalSize<>0) or
  3460. ((stackmisalignment<>0) and
  3461. ((pi_do_call in current_procinfo.flags) or
  3462. (po_assembler in current_procinfo.procdef.procoptions))) then
  3463. begin
  3464. if LocalSize=0 then
  3465. else if LocalSize<=508 then
  3466. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3467. else if LocalSize<=1016 then
  3468. begin
  3469. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3470. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3471. end
  3472. else
  3473. begin
  3474. a_reg_alloc(list,NR_R3);
  3475. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3476. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3477. a_reg_dealloc(list,NR_R3);
  3478. end;
  3479. end;
  3480. if regs=[] then
  3481. begin
  3482. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3483. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3484. else
  3485. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3486. end
  3487. else
  3488. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3489. end;
  3490. end
  3491. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3492. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3493. else
  3494. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3495. end;
  3496. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3497. var
  3498. oppostfix:toppostfix;
  3499. usedtmpref: treference;
  3500. tmpreg,tmpreg2 : tregister;
  3501. dir : integer;
  3502. begin
  3503. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3504. FromSize := ToSize;
  3505. case FromSize of
  3506. { signed integer registers }
  3507. OS_8:
  3508. oppostfix:=PF_B;
  3509. OS_S8:
  3510. oppostfix:=PF_SB;
  3511. OS_16:
  3512. oppostfix:=PF_H;
  3513. OS_S16:
  3514. oppostfix:=PF_SH;
  3515. OS_32,
  3516. OS_S32:
  3517. oppostfix:=PF_None;
  3518. else
  3519. InternalError(200308298);
  3520. end;
  3521. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3522. begin
  3523. if target_info.endian=endian_big then
  3524. dir:=-1
  3525. else
  3526. dir:=1;
  3527. case FromSize of
  3528. OS_16,OS_S16:
  3529. begin
  3530. { only complicated references need an extra loadaddr }
  3531. if assigned(ref.symbol) or
  3532. (ref.index<>NR_NO) or
  3533. (ref.offset<-124) or
  3534. (ref.offset>124) or
  3535. { sometimes the compiler reused registers }
  3536. (reg=ref.index) or
  3537. (reg=ref.base) then
  3538. begin
  3539. tmpreg2:=getintregister(list,OS_INT);
  3540. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3541. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3542. end
  3543. else
  3544. usedtmpref:=ref;
  3545. if target_info.endian=endian_big then
  3546. inc(usedtmpref.offset,1);
  3547. tmpreg:=getintregister(list,OS_INT);
  3548. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3549. inc(usedtmpref.offset,dir);
  3550. if FromSize=OS_16 then
  3551. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3552. else
  3553. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3554. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3555. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3556. end;
  3557. OS_32,OS_S32:
  3558. begin
  3559. tmpreg:=getintregister(list,OS_INT);
  3560. { only complicated references need an extra loadaddr }
  3561. if assigned(ref.symbol) or
  3562. (ref.index<>NR_NO) or
  3563. (ref.offset<-124) or
  3564. (ref.offset>124) or
  3565. { sometimes the compiler reused registers }
  3566. (reg=ref.index) or
  3567. (reg=ref.base) then
  3568. begin
  3569. tmpreg2:=getintregister(list,OS_INT);
  3570. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3571. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3572. end
  3573. else
  3574. usedtmpref:=ref;
  3575. if ref.alignment=2 then
  3576. begin
  3577. if target_info.endian=endian_big then
  3578. inc(usedtmpref.offset,2);
  3579. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3580. inc(usedtmpref.offset,dir*2);
  3581. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3582. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3583. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3584. end
  3585. else
  3586. begin
  3587. if target_info.endian=endian_big then
  3588. inc(usedtmpref.offset,3);
  3589. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3590. inc(usedtmpref.offset,dir);
  3591. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3592. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3593. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3594. inc(usedtmpref.offset,dir);
  3595. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3596. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3597. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3598. inc(usedtmpref.offset,dir);
  3599. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3600. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3601. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3602. end;
  3603. end
  3604. else
  3605. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3606. end;
  3607. end
  3608. else
  3609. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3610. if (fromsize=OS_S8) and (tosize = OS_16) then
  3611. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3612. end;
  3613. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3614. var
  3615. imm_shift : byte;
  3616. l : tasmlabel;
  3617. hr : treference;
  3618. begin
  3619. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3620. internalerror(2002090902);
  3621. if is_thumb_imm(a) then
  3622. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3623. else
  3624. begin
  3625. reference_reset(hr,4);
  3626. current_asmdata.getjumplabel(l);
  3627. cg.a_label(current_procinfo.aktlocaldata,l);
  3628. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3629. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3630. hr.symbol:=l;
  3631. hr.base:=NR_PC;
  3632. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3633. end;
  3634. end;
  3635. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3636. var
  3637. hsym : tsym;
  3638. href,
  3639. tmpref : treference;
  3640. paraloc : Pcgparalocation;
  3641. l : TAsmLabel;
  3642. begin
  3643. { calculate the parameter info for the procdef }
  3644. procdef.init_paraloc_info(callerside);
  3645. hsym:=tsym(procdef.parast.Find('self'));
  3646. if not(assigned(hsym) and
  3647. (hsym.typ=paravarsym)) then
  3648. internalerror(200305251);
  3649. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3650. while paraloc<>nil do
  3651. with paraloc^ do
  3652. begin
  3653. case loc of
  3654. LOC_REGISTER:
  3655. begin
  3656. if is_thumb_imm(ioffset) then
  3657. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3658. else
  3659. begin
  3660. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3661. reference_reset(tmpref,4);
  3662. current_asmdata.getjumplabel(l);
  3663. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3664. cg.a_label(current_procinfo.aktlocaldata,l);
  3665. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3666. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3667. tmpref.symbol:=l;
  3668. tmpref.base:=NR_PC;
  3669. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3670. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3671. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3672. end;
  3673. end;
  3674. LOC_REFERENCE:
  3675. begin
  3676. { offset in the wrapper needs to be adjusted for the stored
  3677. return address }
  3678. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  3679. if is_thumb_imm(ioffset) then
  3680. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  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_ref(list,OP_SUB,size,NR_R4,href);
  3694. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3695. end;
  3696. end
  3697. else
  3698. internalerror(200309189);
  3699. end;
  3700. paraloc:=next;
  3701. end;
  3702. end;
  3703. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3704. var
  3705. href : treference;
  3706. tmpreg : TRegister;
  3707. begin
  3708. href:=ref;
  3709. if { LDR/STR limitations }
  3710. (
  3711. (((op=A_LDR) and (oppostfix=PF_None)) or
  3712. ((op=A_STR) and (oppostfix=PF_None))) and
  3713. (ref.base<>NR_STACK_POINTER_REG) and
  3714. (abs(ref.offset)>124)
  3715. ) or
  3716. { LDRB/STRB limitations }
  3717. (
  3718. (((op=A_LDR) and (oppostfix=PF_B)) or
  3719. ((op=A_LDRB) and (oppostfix=PF_None)) or
  3720. ((op=A_STR) and (oppostfix=PF_B)) or
  3721. ((op=A_STRB) and (oppostfix=PF_None))) and
  3722. ((ref.base=NR_STACK_POINTER_REG) or
  3723. (ref.index=NR_STACK_POINTER_REG) or
  3724. (abs(ref.offset)>31)
  3725. )
  3726. ) or
  3727. { LDRH/STRH limitations }
  3728. (
  3729. (((op=A_LDR) and (oppostfix=PF_H)) or
  3730. ((op=A_LDRH) and (oppostfix=PF_None)) or
  3731. ((op=A_STR) and (oppostfix=PF_H)) or
  3732. ((op=A_STRH) and (oppostfix=PF_None))) and
  3733. ((ref.base=NR_STACK_POINTER_REG) or
  3734. (ref.index=NR_STACK_POINTER_REG) or
  3735. (abs(ref.offset)>62) or
  3736. ((abs(ref.offset) mod 2)<>0)
  3737. )
  3738. ) then
  3739. begin
  3740. tmpreg:=getintregister(list,OS_ADDR);
  3741. a_loadaddr_ref_reg(list,ref,tmpreg);
  3742. reference_reset_base(href,tmpreg,0,ref.alignment);
  3743. end
  3744. else if (op=A_LDR) and
  3745. (oppostfix in [PF_None]) and
  3746. (ref.base=NR_STACK_POINTER_REG) and
  3747. (abs(ref.offset)>1020) then
  3748. begin
  3749. tmpreg:=getintregister(list,OS_ADDR);
  3750. a_loadaddr_ref_reg(list,ref,tmpreg);
  3751. reference_reset_base(href,tmpreg,0,ref.alignment);
  3752. end
  3753. else if (op=A_LDR) and
  3754. ((oppostfix in [PF_SH,PF_SB]) or
  3755. (abs(ref.offset)>124)) then
  3756. begin
  3757. tmpreg:=getintregister(list,OS_ADDR);
  3758. a_loadaddr_ref_reg(list,ref,tmpreg);
  3759. reference_reset_base(href,tmpreg,0,ref.alignment);
  3760. end;
  3761. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3762. end;
  3763. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3764. var
  3765. tmpreg,overflowreg : tregister;
  3766. asmop : tasmop;
  3767. begin
  3768. case op of
  3769. OP_NEG:
  3770. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3771. OP_NOT:
  3772. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3773. OP_DIV,OP_IDIV:
  3774. internalerror(200308284);
  3775. OP_ROL:
  3776. begin
  3777. if not(size in [OS_32,OS_S32]) then
  3778. internalerror(2008072801);
  3779. { simulate ROL by ror'ing 32-value }
  3780. tmpreg:=getintregister(list,OS_32);
  3781. a_load_const_reg(list,OS_32,32,tmpreg);
  3782. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3783. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3784. end;
  3785. else
  3786. begin
  3787. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3788. list.concat(setoppostfix(
  3789. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3790. end;
  3791. end;
  3792. maybeadjustresult(list,op,size,dst);
  3793. end;
  3794. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3795. var
  3796. tmpreg : tregister;
  3797. so : tshifterop;
  3798. l1 : longint;
  3799. imm1, imm2: DWord;
  3800. begin
  3801. //!!! ovloc.loc:=LOC_VOID;
  3802. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3803. case op of
  3804. OP_ADD:
  3805. begin
  3806. op:=OP_SUB;
  3807. a:=aint(dword(-a));
  3808. end;
  3809. OP_SUB:
  3810. begin
  3811. op:=OP_ADD;
  3812. a:=aint(dword(-a));
  3813. end
  3814. end;
  3815. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3816. begin
  3817. // if cgsetflags or setflags then
  3818. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3819. list.concat(setoppostfix(
  3820. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3821. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3822. begin
  3823. //!!! ovloc.loc:=LOC_FLAGS;
  3824. case op of
  3825. OP_ADD:
  3826. //!!! ovloc.resflags:=F_CS;
  3827. ;
  3828. OP_SUB:
  3829. //!!! ovloc.resflags:=F_CC;
  3830. ;
  3831. end;
  3832. end;
  3833. end
  3834. else
  3835. begin
  3836. { there could be added some more sophisticated optimizations }
  3837. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3838. a_load_reg_reg(list,size,size,dst,dst)
  3839. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3840. a_load_const_reg(list,size,0,dst)
  3841. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3842. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3843. { we do this here instead in the peephole optimizer because
  3844. it saves us a register }
  3845. {$ifdef DUMMY}
  3846. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3847. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3848. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3849. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3850. begin
  3851. if l1>32 then{roozbeh does this ever happen?}
  3852. internalerror(200308296);
  3853. shifterop_reset(so);
  3854. so.shiftmode:=SM_LSL;
  3855. so.shiftimm:=l1;
  3856. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3857. end
  3858. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3859. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3860. begin
  3861. if l1>32 then{does this ever happen?}
  3862. internalerror(201205181);
  3863. shifterop_reset(so);
  3864. so.shiftmode:=SM_LSL;
  3865. so.shiftimm:=l1;
  3866. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3867. end
  3868. 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
  3869. begin
  3870. { nothing to do on success }
  3871. end
  3872. {$endif DUMMY}
  3873. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3874. Just using mov x, #0 might allow some easier optimizations down the line. }
  3875. else if (op = OP_AND) and (dword(a)=0) then
  3876. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3877. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3878. else if (op = OP_AND) and (not(dword(a))=0) then
  3879. // do nothing
  3880. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3881. broader range of shifterconstants.}
  3882. {$ifdef DUMMY}
  3883. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3884. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3885. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3886. begin
  3887. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3888. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3889. end
  3890. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3891. not(cgsetflags or setflags) and
  3892. split_into_shifter_const(a, imm1, imm2) then
  3893. begin
  3894. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3895. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3896. end
  3897. {$endif DUMMY}
  3898. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3899. begin
  3900. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3901. end
  3902. else
  3903. begin
  3904. tmpreg:=getintregister(list,size);
  3905. a_load_const_reg(list,size,a,tmpreg);
  3906. a_op_reg_reg(list,op,size,tmpreg,dst);
  3907. end;
  3908. end;
  3909. maybeadjustresult(list,op,size,dst);
  3910. end;
  3911. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3912. begin
  3913. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3914. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3915. else
  3916. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3917. end;
  3918. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3919. var
  3920. l1,l2 : tasmlabel;
  3921. ai : taicpu;
  3922. begin
  3923. current_asmdata.getjumplabel(l1);
  3924. current_asmdata.getjumplabel(l2);
  3925. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3926. ai.is_jmp:=true;
  3927. list.concat(ai);
  3928. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3929. list.concat(taicpu.op_sym(A_B,l2));
  3930. cg.a_label(list,l1);
  3931. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3932. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3933. cg.a_label(list,l2);
  3934. end;
  3935. procedure tthumbcgarm.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  3936. var
  3937. tmpref : treference;
  3938. l : tasmlabel;
  3939. begin
  3940. { there is no branch instruction on thumb which allows big distances and which leaves LR as it is
  3941. and which allows to switch the instruction set }
  3942. { create const entry }
  3943. reference_reset(tmpref,4);
  3944. current_asmdata.getjumplabel(l);
  3945. tmpref.symbol:=l;
  3946. tmpref.base:=NR_PC;
  3947. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3948. list.concat(taicpu.op_reg_ref(A_LDR,NR_R0,tmpref));
  3949. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3950. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3951. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3952. { append const entry }
  3953. list.Concat(tai_align.Create(4));
  3954. list.Concat(tai_label.create(l));
  3955. list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(externalname)));
  3956. end;
  3957. procedure tthumb2cgarm.init_register_allocators;
  3958. begin
  3959. inherited init_register_allocators;
  3960. { currently, we save R14 always, so we can use it }
  3961. if (target_info.system<>system_arm_darwin) then
  3962. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3963. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3964. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3965. else
  3966. { r9 is not available on Darwin according to the llvm code generator }
  3967. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3968. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3969. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3970. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3971. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3972. if current_settings.fputype=fpu_vfpv3 then
  3973. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3974. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3975. 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,
  3976. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3977. ],first_mm_imreg,[])
  3978. else if current_settings.fputype in [fpu_fpv4_s16,fpu_vfpv3_d16] then
  3979. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3980. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3981. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3982. ],first_mm_imreg,[])
  3983. else
  3984. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  3985. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  3986. end;
  3987. procedure tthumb2cgarm.done_register_allocators;
  3988. begin
  3989. rg[R_INTREGISTER].free;
  3990. rg[R_FPUREGISTER].free;
  3991. rg[R_MMREGISTER].free;
  3992. inherited done_register_allocators;
  3993. end;
  3994. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  3995. begin
  3996. list.concat(taicpu.op_reg(A_BLX, reg));
  3997. {
  3998. the compiler does not properly set this flag anymore in pass 1, and
  3999. for now we only need it after pass 2 (I hope) (JM)
  4000. if not(pi_do_call in current_procinfo.flags) then
  4001. internalerror(2003060703);
  4002. }
  4003. include(current_procinfo.flags,pi_do_call);
  4004. end;
  4005. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  4006. var
  4007. imm_shift : byte;
  4008. l : tasmlabel;
  4009. hr : treference;
  4010. begin
  4011. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  4012. internalerror(2002090902);
  4013. if is_thumb32_imm(a) then
  4014. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  4015. else if is_thumb32_imm(not(a)) then
  4016. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  4017. else if (a and $FFFF)=a then
  4018. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  4019. else
  4020. begin
  4021. reference_reset(hr,4);
  4022. current_asmdata.getjumplabel(l);
  4023. cg.a_label(current_procinfo.aktlocaldata,l);
  4024. hr.symboldata:=current_procinfo.aktlocaldata.last;
  4025. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  4026. hr.symbol:=l;
  4027. hr.base:=NR_PC;
  4028. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  4029. end;
  4030. end;
  4031. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  4032. var
  4033. oppostfix:toppostfix;
  4034. usedtmpref: treference;
  4035. tmpreg,tmpreg2 : tregister;
  4036. so : tshifterop;
  4037. dir : integer;
  4038. begin
  4039. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4040. FromSize := ToSize;
  4041. case FromSize of
  4042. { signed integer registers }
  4043. OS_8:
  4044. oppostfix:=PF_B;
  4045. OS_S8:
  4046. oppostfix:=PF_SB;
  4047. OS_16:
  4048. oppostfix:=PF_H;
  4049. OS_S16:
  4050. oppostfix:=PF_SH;
  4051. OS_32,
  4052. OS_S32:
  4053. oppostfix:=PF_None;
  4054. else
  4055. InternalError(200308299);
  4056. end;
  4057. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4058. begin
  4059. if target_info.endian=endian_big then
  4060. dir:=-1
  4061. else
  4062. dir:=1;
  4063. case FromSize of
  4064. OS_16,OS_S16:
  4065. begin
  4066. { only complicated references need an extra loadaddr }
  4067. if assigned(ref.symbol) or
  4068. (ref.index<>NR_NO) or
  4069. (ref.offset<-255) or
  4070. (ref.offset>4094) or
  4071. { sometimes the compiler reused registers }
  4072. (reg=ref.index) or
  4073. (reg=ref.base) then
  4074. begin
  4075. tmpreg2:=getintregister(list,OS_INT);
  4076. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4077. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4078. end
  4079. else
  4080. usedtmpref:=ref;
  4081. if target_info.endian=endian_big then
  4082. inc(usedtmpref.offset,1);
  4083. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4084. tmpreg:=getintregister(list,OS_INT);
  4085. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4086. inc(usedtmpref.offset,dir);
  4087. if FromSize=OS_16 then
  4088. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4089. else
  4090. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4091. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4092. end;
  4093. OS_32,OS_S32:
  4094. begin
  4095. tmpreg:=getintregister(list,OS_INT);
  4096. { only complicated references need an extra loadaddr }
  4097. if assigned(ref.symbol) or
  4098. (ref.index<>NR_NO) or
  4099. (ref.offset<-255) or
  4100. (ref.offset>4092) or
  4101. { sometimes the compiler reused registers }
  4102. (reg=ref.index) or
  4103. (reg=ref.base) then
  4104. begin
  4105. tmpreg2:=getintregister(list,OS_INT);
  4106. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4107. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4108. end
  4109. else
  4110. usedtmpref:=ref;
  4111. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4112. if ref.alignment=2 then
  4113. begin
  4114. if target_info.endian=endian_big then
  4115. inc(usedtmpref.offset,2);
  4116. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4117. inc(usedtmpref.offset,dir*2);
  4118. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4119. so.shiftimm:=16;
  4120. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4121. end
  4122. else
  4123. begin
  4124. if target_info.endian=endian_big then
  4125. inc(usedtmpref.offset,3);
  4126. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4127. inc(usedtmpref.offset,dir);
  4128. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4129. so.shiftimm:=8;
  4130. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4131. inc(usedtmpref.offset,dir);
  4132. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4133. so.shiftimm:=16;
  4134. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4135. inc(usedtmpref.offset,dir);
  4136. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4137. so.shiftimm:=24;
  4138. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4139. end;
  4140. end
  4141. else
  4142. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4143. end;
  4144. end
  4145. else
  4146. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4147. if (fromsize=OS_S8) and (tosize = OS_16) then
  4148. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4149. end;
  4150. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4151. begin
  4152. if op = OP_NOT then
  4153. begin
  4154. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4155. case size of
  4156. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4157. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4158. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4159. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4160. end;
  4161. end
  4162. else
  4163. inherited a_op_reg_reg(list, op, size, src, dst);
  4164. end;
  4165. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4166. var
  4167. shift, width : byte;
  4168. tmpreg : tregister;
  4169. so : tshifterop;
  4170. l1 : longint;
  4171. begin
  4172. ovloc.loc:=LOC_VOID;
  4173. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  4174. case op of
  4175. OP_ADD:
  4176. begin
  4177. op:=OP_SUB;
  4178. a:=aint(dword(-a));
  4179. end;
  4180. OP_SUB:
  4181. begin
  4182. op:=OP_ADD;
  4183. a:=aint(dword(-a));
  4184. end
  4185. end;
  4186. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4187. case op of
  4188. OP_NEG,OP_NOT,
  4189. OP_DIV,OP_IDIV:
  4190. internalerror(200308285);
  4191. OP_SHL:
  4192. begin
  4193. if a>32 then
  4194. internalerror(2014020703);
  4195. if a<>0 then
  4196. begin
  4197. shifterop_reset(so);
  4198. so.shiftmode:=SM_LSL;
  4199. so.shiftimm:=a;
  4200. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4201. end
  4202. else
  4203. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4204. end;
  4205. OP_ROL:
  4206. begin
  4207. if a>32 then
  4208. internalerror(2014020704);
  4209. if a<>0 then
  4210. begin
  4211. shifterop_reset(so);
  4212. so.shiftmode:=SM_ROR;
  4213. so.shiftimm:=32-a;
  4214. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4215. end
  4216. else
  4217. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4218. end;
  4219. OP_ROR:
  4220. begin
  4221. if a>32 then
  4222. internalerror(2014020705);
  4223. if a<>0 then
  4224. begin
  4225. shifterop_reset(so);
  4226. so.shiftmode:=SM_ROR;
  4227. so.shiftimm:=a;
  4228. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4229. end
  4230. else
  4231. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4232. end;
  4233. OP_SHR:
  4234. begin
  4235. if a>32 then
  4236. internalerror(200308292);
  4237. shifterop_reset(so);
  4238. if a<>0 then
  4239. begin
  4240. so.shiftmode:=SM_LSR;
  4241. so.shiftimm:=a;
  4242. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4243. end
  4244. else
  4245. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4246. end;
  4247. OP_SAR:
  4248. begin
  4249. if a>32 then
  4250. internalerror(200308295);
  4251. if a<>0 then
  4252. begin
  4253. shifterop_reset(so);
  4254. so.shiftmode:=SM_ASR;
  4255. so.shiftimm:=a;
  4256. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4257. end
  4258. else
  4259. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4260. end;
  4261. else
  4262. if (op in [OP_SUB, OP_ADD]) and
  4263. ((a < 0) or
  4264. (a > 4095)) then
  4265. begin
  4266. tmpreg:=getintregister(list,size);
  4267. a_load_const_reg(list, size, a, tmpreg);
  4268. if cgsetflags or setflags then
  4269. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4270. list.concat(setoppostfix(
  4271. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4272. end
  4273. else
  4274. begin
  4275. if cgsetflags or setflags then
  4276. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4277. list.concat(setoppostfix(
  4278. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4279. end;
  4280. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4281. begin
  4282. ovloc.loc:=LOC_FLAGS;
  4283. case op of
  4284. OP_ADD:
  4285. ovloc.resflags:=F_CS;
  4286. OP_SUB:
  4287. ovloc.resflags:=F_CC;
  4288. end;
  4289. end;
  4290. end
  4291. else
  4292. begin
  4293. { there could be added some more sophisticated optimizations }
  4294. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4295. a_load_reg_reg(list,size,size,src,dst)
  4296. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4297. a_load_const_reg(list,size,0,dst)
  4298. else if (op in [OP_IMUL]) and (a=-1) then
  4299. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4300. { we do this here instead in the peephole optimizer because
  4301. it saves us a register }
  4302. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4303. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4304. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4305. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4306. begin
  4307. if l1>32 then{roozbeh does this ever happen?}
  4308. internalerror(200308296);
  4309. shifterop_reset(so);
  4310. so.shiftmode:=SM_LSL;
  4311. so.shiftimm:=l1;
  4312. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4313. end
  4314. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4315. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4316. begin
  4317. if l1>32 then{does this ever happen?}
  4318. internalerror(201205181);
  4319. shifterop_reset(so);
  4320. so.shiftmode:=SM_LSL;
  4321. so.shiftimm:=l1;
  4322. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4323. end
  4324. 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
  4325. begin
  4326. { nothing to do on success }
  4327. end
  4328. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4329. Just using mov x, #0 might allow some easier optimizations down the line. }
  4330. else if (op = OP_AND) and (dword(a)=0) then
  4331. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4332. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4333. else if (op = OP_AND) and (not(dword(a))=0) then
  4334. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4335. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4336. broader range of shifterconstants.}
  4337. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4338. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4339. else if (op = OP_AND) and is_thumb32_imm(a) then
  4340. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4341. else if (op = OP_AND) and (a = $FFFF) then
  4342. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4343. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4344. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4345. else if (op = OP_AND) and is_continuous_mask(not(a), shift, width) then
  4346. begin
  4347. a_load_reg_reg(list,size,size,src,dst);
  4348. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4349. end
  4350. else
  4351. begin
  4352. tmpreg:=getintregister(list,size);
  4353. a_load_const_reg(list,size,a,tmpreg);
  4354. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4355. end;
  4356. end;
  4357. maybeadjustresult(list,op,size,dst);
  4358. end;
  4359. const
  4360. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4361. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4362. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4363. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4364. var
  4365. so : tshifterop;
  4366. tmpreg,overflowreg : tregister;
  4367. asmop : tasmop;
  4368. begin
  4369. ovloc.loc:=LOC_VOID;
  4370. case op of
  4371. OP_NEG,OP_NOT:
  4372. internalerror(200308286);
  4373. OP_ROL:
  4374. begin
  4375. if not(size in [OS_32,OS_S32]) then
  4376. internalerror(2008072801);
  4377. { simulate ROL by ror'ing 32-value }
  4378. tmpreg:=getintregister(list,OS_32);
  4379. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4380. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4381. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4382. end;
  4383. OP_ROR:
  4384. begin
  4385. if not(size in [OS_32,OS_S32]) then
  4386. internalerror(2008072802);
  4387. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4388. end;
  4389. OP_IMUL,
  4390. OP_MUL:
  4391. begin
  4392. if cgsetflags or setflags then
  4393. begin
  4394. overflowreg:=getintregister(list,size);
  4395. if op=OP_IMUL then
  4396. asmop:=A_SMULL
  4397. else
  4398. asmop:=A_UMULL;
  4399. { the arm doesn't allow that rd and rm are the same }
  4400. if dst=src2 then
  4401. begin
  4402. if dst<>src1 then
  4403. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4404. else
  4405. begin
  4406. tmpreg:=getintregister(list,size);
  4407. a_load_reg_reg(list,size,size,src2,dst);
  4408. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4409. end;
  4410. end
  4411. else
  4412. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4413. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4414. if op=OP_IMUL then
  4415. begin
  4416. shifterop_reset(so);
  4417. so.shiftmode:=SM_ASR;
  4418. so.shiftimm:=31;
  4419. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4420. end
  4421. else
  4422. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4423. ovloc.loc:=LOC_FLAGS;
  4424. ovloc.resflags:=F_NE;
  4425. end
  4426. else
  4427. begin
  4428. { the arm doesn't allow that rd and rm are the same }
  4429. if dst=src2 then
  4430. begin
  4431. if dst<>src1 then
  4432. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4433. else
  4434. begin
  4435. tmpreg:=getintregister(list,size);
  4436. a_load_reg_reg(list,size,size,src2,dst);
  4437. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4438. end;
  4439. end
  4440. else
  4441. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4442. end;
  4443. end;
  4444. else
  4445. begin
  4446. if cgsetflags or setflags then
  4447. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4448. {$ifdef dummy}
  4449. { R13 is not allowed for certain instruction operands }
  4450. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4451. begin
  4452. if getsupreg(dst)=RS_R13 then
  4453. begin
  4454. tmpreg:=getintregister(list,OS_INT);
  4455. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4456. dst:=tmpreg;
  4457. end;
  4458. if getsupreg(src1)=RS_R13 then
  4459. begin
  4460. tmpreg:=getintregister(list,OS_INT);
  4461. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4462. src1:=tmpreg;
  4463. end;
  4464. end;
  4465. {$endif}
  4466. list.concat(setoppostfix(
  4467. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4468. end;
  4469. end;
  4470. maybeadjustresult(list,op,size,dst);
  4471. end;
  4472. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4473. var item: taicpu;
  4474. begin
  4475. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4476. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4477. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4478. end;
  4479. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4480. var
  4481. ref : treference;
  4482. shift : byte;
  4483. firstfloatreg,lastfloatreg,
  4484. r : byte;
  4485. regs : tcpuregisterset;
  4486. stackmisalignment: pint;
  4487. begin
  4488. LocalSize:=align(LocalSize,4);
  4489. { call instruction does not put anything on the stack }
  4490. stackmisalignment:=0;
  4491. if not(nostackframe) then
  4492. begin
  4493. firstfloatreg:=RS_NO;
  4494. lastfloatreg:=RS_NO;
  4495. { save floating point registers? }
  4496. for r:=RS_F0 to RS_F7 do
  4497. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4498. begin
  4499. if firstfloatreg=RS_NO then
  4500. firstfloatreg:=r;
  4501. lastfloatreg:=r;
  4502. inc(stackmisalignment,12);
  4503. end;
  4504. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4505. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4506. begin
  4507. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4508. a_reg_alloc(list,NR_R12);
  4509. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4510. end;
  4511. { save int registers }
  4512. reference_reset(ref,4);
  4513. ref.index:=NR_STACK_POINTER_REG;
  4514. ref.addressmode:=AM_PREINDEXED;
  4515. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4516. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4517. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4518. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4519. include(regs,RS_R14);
  4520. if regs<>[] then
  4521. begin
  4522. for r:=RS_R0 to RS_R15 do
  4523. if (r in regs) then
  4524. inc(stackmisalignment,4);
  4525. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4526. end;
  4527. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4528. begin
  4529. { the framepointer now points to the saved R15, so the saved
  4530. framepointer is at R11-12 (for get_caller_frame) }
  4531. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4532. a_reg_dealloc(list,NR_R12);
  4533. end;
  4534. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4535. if (LocalSize<>0) or
  4536. ((stackmisalignment<>0) and
  4537. ((pi_do_call in current_procinfo.flags) or
  4538. (po_assembler in current_procinfo.procdef.procoptions))) then
  4539. begin
  4540. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4541. if not(is_shifter_const(localsize,shift)) then
  4542. begin
  4543. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4544. a_reg_alloc(list,NR_R12);
  4545. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4546. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4547. a_reg_dealloc(list,NR_R12);
  4548. end
  4549. else
  4550. begin
  4551. a_reg_dealloc(list,NR_R12);
  4552. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4553. end;
  4554. end;
  4555. if firstfloatreg<>RS_NO then
  4556. begin
  4557. reference_reset(ref,4);
  4558. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4559. begin
  4560. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4561. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4562. ref.base:=NR_R12;
  4563. end
  4564. else
  4565. begin
  4566. ref.base:=current_procinfo.framepointer;
  4567. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4568. end;
  4569. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4570. lastfloatreg-firstfloatreg+1,ref));
  4571. end;
  4572. end;
  4573. end;
  4574. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4575. var
  4576. ref : treference;
  4577. firstfloatreg,lastfloatreg,
  4578. r : byte;
  4579. shift : byte;
  4580. regs : tcpuregisterset;
  4581. LocalSize : longint;
  4582. stackmisalignment: pint;
  4583. begin
  4584. if not(nostackframe) then
  4585. begin
  4586. stackmisalignment:=0;
  4587. { restore floating point register }
  4588. firstfloatreg:=RS_NO;
  4589. lastfloatreg:=RS_NO;
  4590. { save floating point registers? }
  4591. for r:=RS_F0 to RS_F7 do
  4592. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4593. begin
  4594. if firstfloatreg=RS_NO then
  4595. firstfloatreg:=r;
  4596. lastfloatreg:=r;
  4597. { floating point register space is already included in
  4598. localsize below by calc_stackframe_size
  4599. inc(stackmisalignment,12);
  4600. }
  4601. end;
  4602. if firstfloatreg<>RS_NO then
  4603. begin
  4604. reference_reset(ref,4);
  4605. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4606. begin
  4607. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4608. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4609. ref.base:=NR_R12;
  4610. end
  4611. else
  4612. begin
  4613. ref.base:=current_procinfo.framepointer;
  4614. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4615. end;
  4616. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4617. lastfloatreg-firstfloatreg+1,ref));
  4618. end;
  4619. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4620. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4621. begin
  4622. exclude(regs,RS_R14);
  4623. include(regs,RS_R15);
  4624. end;
  4625. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4626. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4627. for r:=RS_R0 to RS_R15 do
  4628. if (r in regs) then
  4629. inc(stackmisalignment,4);
  4630. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4631. LocalSize:=current_procinfo.calc_stackframe_size;
  4632. if (LocalSize<>0) or
  4633. ((stackmisalignment<>0) and
  4634. ((pi_do_call in current_procinfo.flags) or
  4635. (po_assembler in current_procinfo.procdef.procoptions))) then
  4636. begin
  4637. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4638. if not(is_shifter_const(LocalSize,shift)) then
  4639. begin
  4640. a_reg_alloc(list,NR_R12);
  4641. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4642. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4643. a_reg_dealloc(list,NR_R12);
  4644. end
  4645. else
  4646. begin
  4647. a_reg_dealloc(list,NR_R12);
  4648. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4649. end;
  4650. end;
  4651. if regs=[] then
  4652. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  4653. else
  4654. begin
  4655. reference_reset(ref,4);
  4656. ref.index:=NR_STACK_POINTER_REG;
  4657. ref.addressmode:=AM_PREINDEXED;
  4658. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4659. end;
  4660. end
  4661. else
  4662. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  4663. end;
  4664. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4665. var
  4666. tmpreg : tregister;
  4667. tmpref : treference;
  4668. l : tasmlabel;
  4669. so: tshifterop;
  4670. begin
  4671. tmpreg:=NR_NO;
  4672. { Be sure to have a base register }
  4673. if (ref.base=NR_NO) then
  4674. begin
  4675. if ref.shiftmode<>SM_None then
  4676. internalerror(2014020706);
  4677. ref.base:=ref.index;
  4678. ref.index:=NR_NO;
  4679. end;
  4680. { absolute symbols can't be handled directly, we've to store the symbol reference
  4681. in the text segment and access it pc relative
  4682. For now, we assume that references where base or index equals to PC are already
  4683. relative, all other references are assumed to be absolute and thus they need
  4684. to be handled extra.
  4685. A proper solution would be to change refoptions to a set and store the information
  4686. if the symbol is absolute or relative there.
  4687. }
  4688. if (assigned(ref.symbol) and
  4689. not(is_pc(ref.base)) and
  4690. not(is_pc(ref.index))
  4691. ) or
  4692. { [#xxx] isn't a valid address operand }
  4693. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4694. //(ref.offset<-4095) or
  4695. (ref.offset<-255) or
  4696. (ref.offset>4095) or
  4697. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4698. ((ref.offset<-255) or
  4699. (ref.offset>255)
  4700. )
  4701. ) or
  4702. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4703. ((ref.offset<-1020) or
  4704. (ref.offset>1020) or
  4705. ((abs(ref.offset) mod 4)<>0) or
  4706. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4707. assigned(ref.symbol)
  4708. )
  4709. ) then
  4710. begin
  4711. reference_reset(tmpref,4);
  4712. { load symbol }
  4713. tmpreg:=getintregister(list,OS_INT);
  4714. if assigned(ref.symbol) then
  4715. begin
  4716. current_asmdata.getjumplabel(l);
  4717. cg.a_label(current_procinfo.aktlocaldata,l);
  4718. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4719. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4720. { load consts entry }
  4721. tmpref.symbol:=l;
  4722. tmpref.base:=NR_R15;
  4723. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4724. { in case of LDF/STF, we got rid of the NR_R15 }
  4725. if is_pc(ref.base) then
  4726. ref.base:=NR_NO;
  4727. if is_pc(ref.index) then
  4728. ref.index:=NR_NO;
  4729. end
  4730. else
  4731. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4732. if (ref.base<>NR_NO) then
  4733. begin
  4734. if ref.index<>NR_NO then
  4735. begin
  4736. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4737. ref.base:=tmpreg;
  4738. end
  4739. else
  4740. begin
  4741. ref.index:=tmpreg;
  4742. ref.shiftimm:=0;
  4743. ref.signindex:=1;
  4744. ref.shiftmode:=SM_None;
  4745. end;
  4746. end
  4747. else
  4748. ref.base:=tmpreg;
  4749. ref.offset:=0;
  4750. ref.symbol:=nil;
  4751. end;
  4752. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4753. begin
  4754. if tmpreg<>NR_NO then
  4755. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4756. else
  4757. begin
  4758. tmpreg:=getintregister(list,OS_ADDR);
  4759. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4760. ref.base:=tmpreg;
  4761. end;
  4762. ref.offset:=0;
  4763. end;
  4764. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4765. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4766. begin
  4767. tmpreg:=getintregister(list,OS_ADDR);
  4768. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4769. ref.base := tmpreg;
  4770. end;
  4771. { floating point operations have only limited references
  4772. we expect here, that a base is already set }
  4773. 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
  4774. begin
  4775. if ref.shiftmode<>SM_none then
  4776. internalerror(200309121);
  4777. if tmpreg<>NR_NO then
  4778. begin
  4779. if ref.base=tmpreg then
  4780. begin
  4781. if ref.signindex<0 then
  4782. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4783. else
  4784. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4785. ref.index:=NR_NO;
  4786. end
  4787. else
  4788. begin
  4789. if ref.index<>tmpreg then
  4790. internalerror(200403161);
  4791. if ref.signindex<0 then
  4792. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4793. else
  4794. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4795. ref.base:=tmpreg;
  4796. ref.index:=NR_NO;
  4797. end;
  4798. end
  4799. else
  4800. begin
  4801. tmpreg:=getintregister(list,OS_ADDR);
  4802. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4803. ref.base:=tmpreg;
  4804. ref.index:=NR_NO;
  4805. end;
  4806. end;
  4807. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4808. Result := ref;
  4809. end;
  4810. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4811. var
  4812. instr: taicpu;
  4813. begin
  4814. if (fromsize=OS_F32) and
  4815. (tosize=OS_F32) then
  4816. begin
  4817. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4818. list.Concat(instr);
  4819. add_move_instruction(instr);
  4820. end
  4821. else if (fromsize=OS_F64) and
  4822. (tosize=OS_F64) then
  4823. begin
  4824. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4825. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4826. end
  4827. else if (fromsize=OS_F32) and
  4828. (tosize=OS_F64) then
  4829. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4830. begin
  4831. //list.concat(nil);
  4832. end;
  4833. end;
  4834. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4835. begin
  4836. handle_load_store(list,A_VLDR,PF_None,reg,ref);
  4837. end;
  4838. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4839. begin
  4840. handle_load_store(list,A_VSTR,PF_None,reg,ref);
  4841. end;
  4842. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4843. begin
  4844. if //(shuffle=nil) and
  4845. (tosize=OS_F32) then
  4846. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4847. else
  4848. internalerror(2012100813);
  4849. end;
  4850. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4851. begin
  4852. if //(shuffle=nil) and
  4853. (fromsize=OS_F32) then
  4854. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  4855. else
  4856. internalerror(2012100814);
  4857. end;
  4858. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4859. var tmpreg: tregister;
  4860. begin
  4861. case op of
  4862. OP_NEG:
  4863. begin
  4864. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4865. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4866. tmpreg:=cg.getintregister(list,OS_32);
  4867. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4868. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4869. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4870. end;
  4871. else
  4872. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4873. end;
  4874. end;
  4875. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4876. begin
  4877. case op of
  4878. OP_NEG:
  4879. begin
  4880. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4881. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4882. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4883. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4884. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4885. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4886. end;
  4887. OP_NOT:
  4888. begin
  4889. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4890. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4891. end;
  4892. OP_AND,OP_OR,OP_XOR:
  4893. begin
  4894. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4895. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4896. end;
  4897. OP_ADD:
  4898. begin
  4899. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4900. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4901. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4902. end;
  4903. OP_SUB:
  4904. begin
  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. end;
  4909. else
  4910. internalerror(2003083101);
  4911. end;
  4912. end;
  4913. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4914. var
  4915. tmpreg : tregister;
  4916. b : byte;
  4917. begin
  4918. case op of
  4919. OP_AND,OP_OR,OP_XOR:
  4920. begin
  4921. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4922. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4923. end;
  4924. OP_ADD:
  4925. begin
  4926. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4927. begin
  4928. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4929. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4930. end
  4931. else
  4932. begin
  4933. tmpreg:=cg.getintregister(list,OS_32);
  4934. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4935. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4936. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4937. end;
  4938. tmpreg:=cg.getintregister(list,OS_32);
  4939. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4940. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4941. end;
  4942. OP_SUB:
  4943. begin
  4944. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4945. begin
  4946. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4947. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4948. end
  4949. else
  4950. begin
  4951. tmpreg:=cg.getintregister(list,OS_32);
  4952. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4953. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4954. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4955. end;
  4956. tmpreg:=cg.getintregister(list,OS_32);
  4957. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4958. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4959. end;
  4960. else
  4961. internalerror(2003083101);
  4962. end;
  4963. end;
  4964. procedure create_codegen;
  4965. begin
  4966. if GenerateThumb2Code then
  4967. begin
  4968. cg:=tthumb2cgarm.create;
  4969. cg64:=tthumb2cg64farm.create;
  4970. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4971. end
  4972. else if GenerateThumbCode then
  4973. begin
  4974. cg:=tthumbcgarm.create;
  4975. cg64:=tthumbcg64farm.create;
  4976. // casmoptimizer:=TCpuThumbAsmOptimizer;
  4977. end
  4978. else
  4979. begin
  4980. cg:=tarmcgarm.create;
  4981. cg64:=tarmcg64farm.create;
  4982. casmoptimizer:=TCpuAsmOptimizer;
  4983. end;
  4984. end;
  4985. end.