cgcpu.pas 209 KB

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