cgcpu.pas 210 KB

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