cgcpu.pas 201 KB

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