aasmcpu.pas 202 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils,
  26. sysutils;
  27. const
  28. { "mov reg,reg" source operand number }
  29. O_MOV_SOURCE = 1;
  30. { "mov reg,reg" source operand number }
  31. O_MOV_DEST = 0;
  32. { Operand types }
  33. OT_NONE = $00000000;
  34. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  35. OT_BITS16 = $00000002;
  36. OT_BITS32 = $00000004;
  37. OT_BITS64 = $00000008; { FPU only }
  38. OT_BITS80 = $00000010;
  39. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  40. OT_NEAR = $00000040;
  41. OT_SHORT = $00000080;
  42. OT_BITSTINY = $00000100; { fpu constant }
  43. OT_BITSSHIFTER =
  44. $00000200;
  45. OT_SIZE_MASK = $000003FF; { all the size attributes }
  46. OT_NON_SIZE = $0FFFF800;
  47. OT_OPT_SIZE = $F0000000;
  48. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  49. OT_TO = $00000200; { operand is followed by a colon }
  50. { reverse effect in FADD, FSUB &c }
  51. OT_COLON = $00000400;
  52. OT_SHIFTEROP = $00000800;
  53. OT_REGISTER = $00001000;
  54. OT_IMMEDIATE = $00002000;
  55. OT_REGLIST = $00008000;
  56. OT_IMM8 = $00002001;
  57. OT_IMM24 = $00002002;
  58. OT_IMM32 = $00002004;
  59. OT_IMM64 = $00002008;
  60. OT_IMM80 = $00002010;
  61. OT_IMMTINY = $00002100;
  62. OT_IMMSHIFTER= $00002200;
  63. OT_IMMEDIATEZERO = $10002200;
  64. OT_IMMEDIATE24 = OT_IMM24;
  65. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  66. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  67. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  68. OT_IMMEDIATEFPU = OT_IMMTINY;
  69. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  70. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  71. OT_REG8 = $00201001;
  72. OT_REG16 = $00201002;
  73. OT_REG32 = $00201004;
  74. OT_REGLO = $10201004; { lower reg (r0-r7) }
  75. OT_REGSP = $20201004;
  76. OT_REG64 = $00201008;
  77. OT_VREG = $00201010; { vector register }
  78. OT_REGF = $00201020; { coproc register }
  79. OT_REGS = $00201040; { special register with mask }
  80. OT_MEMORY = $00204000; { register number in 'basereg' }
  81. OT_MEM8 = $00204001;
  82. OT_MEM16 = $00204002;
  83. OT_MEM32 = $00204004;
  84. OT_MEM64 = $00204008;
  85. OT_MEM80 = $00204010;
  86. { word/byte load/store }
  87. OT_AM2 = $00010000;
  88. { misc ld/st operations, thumb reg indexed }
  89. OT_AM3 = $00020000;
  90. { multiple ld/st operations or thumb imm indexed }
  91. OT_AM4 = $00040000;
  92. { co proc. ld/st operations or thumb sp+imm indexed }
  93. OT_AM5 = $00080000;
  94. { exclusive ld/st operations or thumb pc+imm indexed }
  95. OT_AM6 = $00100000;
  96. OT_AMMASK = $001f0000;
  97. { IT instruction }
  98. OT_CONDITION = $00200000;
  99. OT_MODEFLAGS = $00400000;
  100. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  101. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  102. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  103. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  104. OT_MEMORYAM6 = OT_MEMORY or OT_AM6;
  105. OT_FPUREG = $01000000; { floating point stack registers }
  106. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  107. { a mask for the following }
  108. OT_MEM_OFFS = $00604000; { special type of EA }
  109. { simple [address] offset }
  110. OT_ONENESS = $00800000; { special type of immediate operand }
  111. { so UNITY == IMMEDIATE | ONENESS }
  112. OT_UNITY = $00802000; { for shift/rotate instructions }
  113. instabentries = {$i armnop.inc}
  114. maxinfolen = 5;
  115. IF_NONE = $00000000;
  116. IF_ARMMASK = $000F0000;
  117. IF_ARM32 = $00010000;
  118. IF_THUMB = $00020000;
  119. IF_THUMB32 = $00040000;
  120. IF_WIDE = $00080000;
  121. IF_ARMvMASK = $0FF00000;
  122. IF_ARMv4 = $00100000;
  123. IF_ARMv4T = $00200000;
  124. IF_ARMv5 = $00300000;
  125. IF_ARMv5T = $00400000;
  126. IF_ARMv5TE = $00500000;
  127. IF_ARMv5TEJ = $00600000;
  128. IF_ARMv6 = $00700000;
  129. IF_ARMv6K = $00800000;
  130. IF_ARMv6T2 = $00900000;
  131. IF_ARMv6Z = $00A00000;
  132. IF_ARMv6M = $00B00000;
  133. IF_ARMv7 = $00C00000;
  134. IF_ARMv7A = $00D00000;
  135. IF_ARMv7R = $00E00000;
  136. IF_ARMv7M = $00F00000;
  137. IF_ARMv7EM = $01000000;
  138. IF_FPMASK = $F0000000;
  139. IF_FPA = $10000000;
  140. IF_VFPv2 = $20000000;
  141. IF_VFPv3 = $40000000;
  142. IF_VFPv4 = $80000000;
  143. { if the instruction can change in a second pass }
  144. IF_PASS2 = longint($80000000);
  145. type
  146. TInsTabCache=array[TasmOp] of longint;
  147. PInsTabCache=^TInsTabCache;
  148. tinsentry = record
  149. opcode : tasmop;
  150. ops : byte;
  151. optypes : array[0..5] of longint;
  152. code : array[0..maxinfolen] of char;
  153. flags : longword;
  154. end;
  155. pinsentry=^tinsentry;
  156. const
  157. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  158. var
  159. InsTabCache : PInsTabCache;
  160. type
  161. taicpu = class(tai_cpu_abstract_sym)
  162. oppostfix : TOpPostfix;
  163. wideformat : boolean;
  164. roundingmode : troundingmode;
  165. procedure loadshifterop(opidx:longint;const so:tshifterop);
  166. procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
  167. procedure loadconditioncode(opidx:longint;const cond:tasmcond);
  168. procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
  169. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  170. constructor op_none(op : tasmop);
  171. constructor op_reg(op : tasmop;_op1 : tregister);
  172. constructor op_ref(op : tasmop;const _op1 : treference);
  173. constructor op_const(op : tasmop;_op1 : longint);
  174. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  175. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  176. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  177. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  178. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  179. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  180. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  181. constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  182. constructor op_reg_reg_const_const(op : tasmop;_op1,_op2 : tregister; _op3,_op4: aint);
  183. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  184. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  185. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  186. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  187. { SFM/LFM }
  188. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  189. { ITxxx }
  190. constructor op_cond(op: tasmop; cond: tasmcond);
  191. { CPSxx }
  192. constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
  193. constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  194. { MSR }
  195. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  196. { *M*LL }
  197. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  198. { this is for Jmp instructions }
  199. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  200. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  201. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  202. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  203. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  204. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  205. function spilling_get_operation_type(opnr: longint): topertype;override;
  206. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  207. { assembler }
  208. public
  209. { the next will reset all instructions that can change in pass 2 }
  210. procedure ResetPass1;override;
  211. procedure ResetPass2;override;
  212. function CheckIfValid:boolean;
  213. function GetString:string;
  214. function Pass1(objdata:TObjData):longint;override;
  215. procedure Pass2(objdata:TObjData);override;
  216. protected
  217. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  218. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  219. procedure ppubuildderefimploper(var o:toper);override;
  220. procedure ppuderefoper(var o:toper);override;
  221. private
  222. { pass1 info }
  223. inIT,
  224. lastinIT: boolean;
  225. { arm version info }
  226. fArmVMask,
  227. fArmMask : longint;
  228. { next fields are filled in pass1, so pass2 is faster }
  229. inssize : shortint;
  230. insoffset : longint;
  231. LastInsOffset : longint; { need to be public to be reset }
  232. insentry : PInsEntry;
  233. procedure BuildArmMasks;
  234. function InsEnd:longint;
  235. procedure create_ot(objdata:TObjData);
  236. function Matches(p:PInsEntry):longint;
  237. function calcsize(p:PInsEntry):shortint;
  238. procedure gencode(objdata:TObjData);
  239. function NeedAddrPrefix(opidx:byte):boolean;
  240. procedure Swapoperands;
  241. function FindInsentry(objdata:TObjData):boolean;
  242. end;
  243. tai_align = class(tai_align_abstract)
  244. { nothing to add }
  245. end;
  246. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  247. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  248. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  249. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  250. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  251. { inserts pc relative symbols at places where they are reachable
  252. and transforms special instructions to valid instruction encodings }
  253. procedure finalizearmcode(list,listtoinsert : TAsmList);
  254. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  255. procedure InsertPData;
  256. procedure InitAsm;
  257. procedure DoneAsm;
  258. implementation
  259. uses
  260. itcpugas,aoptcpu,
  261. systems;
  262. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  263. begin
  264. allocate_oper(opidx+1);
  265. with oper[opidx]^ do
  266. begin
  267. if typ<>top_shifterop then
  268. begin
  269. clearop(opidx);
  270. new(shifterop);
  271. end;
  272. shifterop^:=so;
  273. typ:=top_shifterop;
  274. if assigned(add_reg_instruction_hook) then
  275. add_reg_instruction_hook(self,shifterop^.rs);
  276. end;
  277. end;
  278. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  279. var
  280. i : byte;
  281. begin
  282. allocate_oper(opidx+1);
  283. with oper[opidx]^ do
  284. begin
  285. if typ<>top_regset then
  286. begin
  287. clearop(opidx);
  288. new(regset);
  289. end;
  290. regset^:=s;
  291. regtyp:=regsetregtype;
  292. subreg:=regsetsubregtype;
  293. usermode:=ausermode;
  294. typ:=top_regset;
  295. case regsetregtype of
  296. R_INTREGISTER:
  297. for i:=RS_R0 to RS_R15 do
  298. begin
  299. if assigned(add_reg_instruction_hook) and (i in regset^) then
  300. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  301. end;
  302. R_MMREGISTER:
  303. { both RS_S0 and RS_D0 range from 0 to 31 }
  304. for i:=RS_D0 to RS_D31 do
  305. begin
  306. if assigned(add_reg_instruction_hook) and (i in regset^) then
  307. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  308. end;
  309. end;
  310. end;
  311. end;
  312. procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
  313. begin
  314. allocate_oper(opidx+1);
  315. with oper[opidx]^ do
  316. begin
  317. if typ<>top_conditioncode then
  318. clearop(opidx);
  319. cc:=cond;
  320. typ:=top_conditioncode;
  321. end;
  322. end;
  323. procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
  324. begin
  325. allocate_oper(opidx+1);
  326. with oper[opidx]^ do
  327. begin
  328. if typ<>top_modeflags then
  329. clearop(opidx);
  330. modeflags:=flags;
  331. typ:=top_modeflags;
  332. end;
  333. end;
  334. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  335. begin
  336. allocate_oper(opidx+1);
  337. with oper[opidx]^ do
  338. begin
  339. if typ<>top_specialreg then
  340. clearop(opidx);
  341. specialreg:=areg;
  342. specialflags:=aflags;
  343. typ:=top_specialreg;
  344. end;
  345. end;
  346. {*****************************************************************************
  347. taicpu Constructors
  348. *****************************************************************************}
  349. constructor taicpu.op_none(op : tasmop);
  350. begin
  351. inherited create(op);
  352. end;
  353. { for pld }
  354. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  355. begin
  356. inherited create(op);
  357. ops:=1;
  358. loadref(0,_op1);
  359. end;
  360. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  361. begin
  362. inherited create(op);
  363. ops:=1;
  364. loadreg(0,_op1);
  365. end;
  366. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  367. begin
  368. inherited create(op);
  369. ops:=1;
  370. loadconst(0,aint(_op1));
  371. end;
  372. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  373. begin
  374. inherited create(op);
  375. ops:=2;
  376. loadreg(0,_op1);
  377. loadreg(1,_op2);
  378. end;
  379. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  380. begin
  381. inherited create(op);
  382. ops:=2;
  383. loadreg(0,_op1);
  384. loadconst(1,aint(_op2));
  385. end;
  386. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  387. begin
  388. inherited create(op);
  389. ops:=1;
  390. loadregset(0,regtype,subreg,_op1);
  391. end;
  392. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  393. begin
  394. inherited create(op);
  395. ops:=2;
  396. loadref(0,_op1);
  397. loadregset(1,regtype,subreg,_op2);
  398. end;
  399. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  400. begin
  401. inherited create(op);
  402. ops:=2;
  403. loadreg(0,_op1);
  404. loadref(1,_op2);
  405. end;
  406. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  407. begin
  408. inherited create(op);
  409. ops:=3;
  410. loadreg(0,_op1);
  411. loadreg(1,_op2);
  412. loadreg(2,_op3);
  413. end;
  414. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  415. begin
  416. inherited create(op);
  417. ops:=4;
  418. loadreg(0,_op1);
  419. loadreg(1,_op2);
  420. loadreg(2,_op3);
  421. loadreg(3,_op4);
  422. end;
  423. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  424. begin
  425. inherited create(op);
  426. ops:=3;
  427. loadreg(0,_op1);
  428. loadreg(1,_op2);
  429. loadconst(2,aint(_op3));
  430. end;
  431. constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  432. begin
  433. inherited create(op);
  434. ops:=3;
  435. loadreg(0,_op1);
  436. loadconst(1,aint(_op2));
  437. loadconst(2,aint(_op3));
  438. end;
  439. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  440. begin
  441. inherited create(op);
  442. ops:=4;
  443. loadreg(0,_op1);
  444. loadreg(1,_op2);
  445. loadconst(2,aint(_op3));
  446. loadconst(3,aint(_op4));
  447. end;
  448. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  449. begin
  450. inherited create(op);
  451. ops:=3;
  452. loadreg(0,_op1);
  453. loadconst(1,_op2);
  454. loadref(2,_op3);
  455. end;
  456. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  457. begin
  458. inherited create(op);
  459. ops:=1;
  460. loadconditioncode(0, cond);
  461. end;
  462. constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
  463. begin
  464. inherited create(op);
  465. ops := 1;
  466. loadmodeflags(0,flags);
  467. end;
  468. constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  469. begin
  470. inherited create(op);
  471. ops := 2;
  472. loadmodeflags(0,flags);
  473. loadconst(1,a);
  474. end;
  475. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  476. begin
  477. inherited create(op);
  478. ops:=2;
  479. loadspecialreg(0,specialreg,specialregflags);
  480. loadreg(1,_op2);
  481. end;
  482. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  483. begin
  484. inherited create(op);
  485. ops:=3;
  486. loadreg(0,_op1);
  487. loadreg(1,_op2);
  488. loadsymbol(0,_op3,_op3ofs);
  489. end;
  490. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  491. begin
  492. inherited create(op);
  493. ops:=3;
  494. loadreg(0,_op1);
  495. loadreg(1,_op2);
  496. loadref(2,_op3);
  497. end;
  498. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  499. begin
  500. inherited create(op);
  501. ops:=3;
  502. loadreg(0,_op1);
  503. loadreg(1,_op2);
  504. loadshifterop(2,_op3);
  505. end;
  506. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  507. begin
  508. inherited create(op);
  509. ops:=4;
  510. loadreg(0,_op1);
  511. loadreg(1,_op2);
  512. loadreg(2,_op3);
  513. loadshifterop(3,_op4);
  514. end;
  515. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  516. begin
  517. inherited create(op);
  518. condition:=cond;
  519. ops:=1;
  520. loadsymbol(0,_op1,0);
  521. end;
  522. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  523. begin
  524. inherited create(op);
  525. ops:=1;
  526. loadsymbol(0,_op1,0);
  527. end;
  528. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  529. begin
  530. inherited create(op);
  531. ops:=1;
  532. loadsymbol(0,_op1,_op1ofs);
  533. end;
  534. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  535. begin
  536. inherited create(op);
  537. ops:=2;
  538. loadreg(0,_op1);
  539. loadsymbol(1,_op2,_op2ofs);
  540. end;
  541. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  542. begin
  543. inherited create(op);
  544. ops:=2;
  545. loadsymbol(0,_op1,_op1ofs);
  546. loadref(1,_op2);
  547. end;
  548. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  549. begin
  550. { allow the register allocator to remove unnecessary moves }
  551. result:=(
  552. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  553. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  554. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER)) or
  555. ((opcode in [A_VMOV]) and (regtype = R_MMREGISTER) and (oppostfix in [PF_F32,PF_F64]))
  556. ) and
  557. ((oppostfix in [PF_None,PF_D]) or (opcode = A_VMOV)) and
  558. (condition=C_None) and
  559. (ops=2) and
  560. (oper[0]^.typ=top_reg) and
  561. (oper[1]^.typ=top_reg) and
  562. (oper[0]^.reg=oper[1]^.reg);
  563. end;
  564. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  565. begin
  566. case getregtype(r) of
  567. R_INTREGISTER :
  568. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  569. R_FPUREGISTER :
  570. { use lfm because we don't know the current internal format
  571. and avoid exceptions
  572. }
  573. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  574. R_MMREGISTER :
  575. result:=taicpu.op_reg_ref(A_VLDR,r,ref);
  576. else
  577. internalerror(200401041);
  578. end;
  579. end;
  580. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  581. begin
  582. case getregtype(r) of
  583. R_INTREGISTER :
  584. result:=taicpu.op_reg_ref(A_STR,r,ref);
  585. R_FPUREGISTER :
  586. { use sfm because we don't know the current internal format
  587. and avoid exceptions
  588. }
  589. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  590. R_MMREGISTER :
  591. result:=taicpu.op_reg_ref(A_VSTR,r,ref);
  592. else
  593. internalerror(200401041);
  594. end;
  595. end;
  596. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  597. begin
  598. case opcode of
  599. A_ADC,A_ADD,A_AND,A_BIC,
  600. A_EOR,A_CLZ,A_RBIT,
  601. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  602. A_LDRSH,A_LDRT,
  603. A_MOV,A_MVN,A_MLA,A_MUL,
  604. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  605. A_SWP,A_SWPB,
  606. A_LDF,A_FLT,A_FIX,
  607. A_ADF,A_DVF,A_FDV,A_FML,
  608. A_RFS,A_RFC,A_RDF,
  609. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  610. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  611. A_LFM,
  612. A_FLDS,A_FLDD,
  613. A_FMRX,A_FMXR,A_FMSTAT,
  614. A_FMSR,A_FMRS,A_FMDRR,
  615. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  616. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  617. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  618. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  619. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  620. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  621. A_FNEGS,A_FNEGD,
  622. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  623. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  624. A_SXTB16,A_UXTB16,
  625. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  626. A_NEG,
  627. A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB:
  628. if opnr=0 then
  629. result:=operand_write
  630. else
  631. result:=operand_read;
  632. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  633. A_CMN,A_CMP,A_TEQ,A_TST,
  634. A_CMF,A_CMFE,A_WFS,A_CNF,
  635. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  636. A_FCMPZS,A_FCMPZD,
  637. A_VCMP,A_VCMPE:
  638. result:=operand_read;
  639. A_SMLAL,A_UMLAL:
  640. if opnr in [0,1] then
  641. result:=operand_readwrite
  642. else
  643. result:=operand_read;
  644. A_SMULL,A_UMULL,
  645. A_FMRRD:
  646. if opnr in [0,1] then
  647. result:=operand_write
  648. else
  649. result:=operand_read;
  650. A_STR,A_STRB,A_STRBT,
  651. A_STRH,A_STRT,A_STF,A_SFM,
  652. A_FSTS,A_FSTD,
  653. A_VSTR:
  654. { important is what happens with the involved registers }
  655. if opnr=0 then
  656. result := operand_read
  657. else
  658. { check for pre/post indexed }
  659. result := operand_read;
  660. //Thumb2
  661. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI,
  662. A_SMMLA,A_SMMLS:
  663. if opnr in [0] then
  664. result:=operand_write
  665. else
  666. result:=operand_read;
  667. A_BFC:
  668. if opnr in [0] then
  669. result:=operand_readwrite
  670. else
  671. result:=operand_read;
  672. A_LDREX:
  673. if opnr in [0] then
  674. result:=operand_write
  675. else
  676. result:=operand_read;
  677. A_STREX:
  678. result:=operand_write;
  679. else
  680. internalerror(200403151);
  681. end;
  682. end;
  683. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  684. begin
  685. result := operand_read;
  686. if (oper[opnr]^.ref^.base = reg) and
  687. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  688. result := operand_readwrite;
  689. end;
  690. procedure BuildInsTabCache;
  691. var
  692. i : longint;
  693. begin
  694. new(instabcache);
  695. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  696. i:=0;
  697. while (i<InsTabEntries) do
  698. begin
  699. if InsTabCache^[InsTab[i].Opcode]=-1 then
  700. InsTabCache^[InsTab[i].Opcode]:=i;
  701. inc(i);
  702. end;
  703. end;
  704. procedure InitAsm;
  705. begin
  706. if not assigned(instabcache) then
  707. BuildInsTabCache;
  708. end;
  709. procedure DoneAsm;
  710. begin
  711. if assigned(instabcache) then
  712. begin
  713. dispose(instabcache);
  714. instabcache:=nil;
  715. end;
  716. end;
  717. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  718. begin
  719. i.oppostfix:=pf;
  720. result:=i;
  721. end;
  722. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  723. begin
  724. i.roundingmode:=rm;
  725. result:=i;
  726. end;
  727. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  728. begin
  729. i.condition:=c;
  730. result:=i;
  731. end;
  732. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  733. Begin
  734. Current:=tai(Current.Next);
  735. While Assigned(Current) And (Current.typ In SkipInstr) Do
  736. Current:=tai(Current.Next);
  737. Next:=Current;
  738. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  739. Result:=True
  740. Else
  741. Begin
  742. Next:=Nil;
  743. Result:=False;
  744. End;
  745. End;
  746. (*
  747. function armconstequal(hp1,hp2: tai): boolean;
  748. begin
  749. result:=false;
  750. if hp1.typ<>hp2.typ then
  751. exit;
  752. case hp1.typ of
  753. tai_const:
  754. result:=
  755. (tai_const(hp2).sym=tai_const(hp).sym) and
  756. (tai_const(hp2).value=tai_const(hp).value) and
  757. (tai(hp2.previous).typ=ait_label);
  758. tai_const:
  759. result:=
  760. (tai_const(hp2).sym=tai_const(hp).sym) and
  761. (tai_const(hp2).value=tai_const(hp).value) and
  762. (tai(hp2.previous).typ=ait_label);
  763. end;
  764. end;
  765. *)
  766. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  767. var
  768. limit: longint;
  769. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096, this
  770. function checks the next count instructions if the limit must be
  771. decreased }
  772. procedure CheckLimit(hp : tai;count : integer);
  773. var
  774. i : Integer;
  775. begin
  776. for i:=1 to count do
  777. if SimpleGetNextInstruction(hp,hp) and
  778. (tai(hp).typ=ait_instruction) and
  779. ((taicpu(hp).opcode=A_FLDS) or
  780. (taicpu(hp).opcode=A_FLDD) or
  781. (taicpu(hp).opcode=A_VLDR)) then
  782. limit:=254;
  783. end;
  784. function is_case_dispatch(hp: taicpu): boolean;
  785. begin
  786. result:=
  787. ((taicpu(hp).opcode in [A_ADD,A_LDR]) and
  788. not(GenerateThumbCode or GenerateThumb2Code) and
  789. (taicpu(hp).oper[0]^.typ=top_reg) and
  790. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  791. ((taicpu(hp).opcode=A_MOV) and (GenerateThumbCode) and
  792. (taicpu(hp).oper[0]^.typ=top_reg) and
  793. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  794. (taicpu(hp).opcode=A_TBH) or
  795. (taicpu(hp).opcode=A_TBB);
  796. end;
  797. var
  798. curinspos,
  799. penalty,
  800. lastinspos,
  801. { increased for every data element > 4 bytes inserted }
  802. currentsize,
  803. extradataoffset,
  804. curop : longint;
  805. curtai,
  806. inserttai : tai;
  807. ai_label : tai_label;
  808. curdatatai,hp,hp2 : tai;
  809. curdata : TAsmList;
  810. l : tasmlabel;
  811. doinsert,
  812. removeref : boolean;
  813. multiplier : byte;
  814. begin
  815. curdata:=TAsmList.create;
  816. lastinspos:=-1;
  817. curinspos:=0;
  818. extradataoffset:=0;
  819. if GenerateThumbCode then
  820. begin
  821. multiplier:=2;
  822. limit:=504;
  823. end
  824. else
  825. begin
  826. limit:=1016;
  827. multiplier:=1;
  828. end;
  829. curtai:=tai(list.first);
  830. doinsert:=false;
  831. while assigned(curtai) do
  832. begin
  833. { instruction? }
  834. case curtai.typ of
  835. ait_instruction:
  836. begin
  837. { walk through all operand of the instruction }
  838. for curop:=0 to taicpu(curtai).ops-1 do
  839. begin
  840. { reference? }
  841. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  842. begin
  843. { pc relative symbol? }
  844. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  845. if assigned(curdatatai) then
  846. begin
  847. { create a new copy of a data entry on arm thumb if the entry has been inserted already
  848. before because arm thumb does not allow pc relative negative offsets }
  849. if (GenerateThumbCode) and
  850. tai_label(curdatatai).inserted then
  851. begin
  852. current_asmdata.getjumplabel(l);
  853. hp:=tai_label.create(l);
  854. listtoinsert.Concat(hp);
  855. hp2:=tai(curdatatai.Next.GetCopy);
  856. hp2.Next:=nil;
  857. hp2.Previous:=nil;
  858. listtoinsert.Concat(hp2);
  859. taicpu(curtai).oper[curop]^.ref^.symboldata:=hp;
  860. taicpu(curtai).oper[curop]^.ref^.symbol:=l;
  861. curdatatai:=hp;
  862. end;
  863. { move only if we're at the first reference of a label }
  864. if not(tai_label(curdatatai).moved) then
  865. begin
  866. tai_label(curdatatai).moved:=true;
  867. { check if symbol already used. }
  868. { if yes, reuse the symbol }
  869. hp:=tai(curdatatai.next);
  870. removeref:=false;
  871. if assigned(hp) then
  872. begin
  873. case hp.typ of
  874. ait_const:
  875. begin
  876. if (tai_const(hp).consttype=aitconst_64bit) then
  877. inc(extradataoffset,multiplier);
  878. end;
  879. ait_realconst:
  880. begin
  881. inc(extradataoffset,multiplier*(((tai_realconst(hp).savesize-4)+3) div 4));
  882. end;
  883. end;
  884. { check if the same constant has been already inserted into the currently handled list,
  885. if yes, reuse it }
  886. if (hp.typ=ait_const) then
  887. begin
  888. hp2:=tai(curdata.first);
  889. while assigned(hp2) do
  890. begin
  891. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  892. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  893. then
  894. begin
  895. with taicpu(curtai).oper[curop]^.ref^ do
  896. begin
  897. symboldata:=hp2.previous;
  898. symbol:=tai_label(hp2.previous).labsym;
  899. end;
  900. removeref:=true;
  901. break;
  902. end;
  903. hp2:=tai(hp2.next);
  904. end;
  905. end;
  906. end;
  907. { move or remove symbol reference }
  908. repeat
  909. hp:=tai(curdatatai.next);
  910. listtoinsert.remove(curdatatai);
  911. if removeref then
  912. curdatatai.free
  913. else
  914. curdata.concat(curdatatai);
  915. curdatatai:=hp;
  916. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  917. if lastinspos=-1 then
  918. lastinspos:=curinspos;
  919. end;
  920. end;
  921. end;
  922. end;
  923. inc(curinspos,multiplier);
  924. end;
  925. ait_align:
  926. begin
  927. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  928. requires also incrementing curinspos by 1 }
  929. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  930. end;
  931. ait_const:
  932. begin
  933. inc(curinspos,multiplier);
  934. if (tai_const(curtai).consttype=aitconst_64bit) then
  935. inc(curinspos,multiplier);
  936. end;
  937. ait_realconst:
  938. begin
  939. inc(curinspos,multiplier*((tai_realconst(hp).savesize+3) div 4));
  940. end;
  941. end;
  942. { special case for case jump tables }
  943. penalty:=0;
  944. if SimpleGetNextInstruction(curtai,hp) and
  945. (tai(hp).typ=ait_instruction) then
  946. begin
  947. case taicpu(hp).opcode of
  948. A_MOV,
  949. A_LDR,
  950. A_ADD,
  951. A_TBH,
  952. A_TBB:
  953. { approximation if we hit a case jump table }
  954. if is_case_dispatch(taicpu(hp)) then
  955. begin
  956. penalty:=multiplier;
  957. hp:=tai(hp.next);
  958. { skip register allocations and comments inserted by the optimizer as well as a label
  959. as jump tables for thumb might have }
  960. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc,ait_label]) do
  961. hp:=tai(hp.next);
  962. while assigned(hp) and (hp.typ=ait_const) do
  963. begin
  964. inc(penalty,multiplier);
  965. hp:=tai(hp.next);
  966. end;
  967. end;
  968. A_IT:
  969. begin
  970. if GenerateThumb2Code then
  971. penalty:=multiplier;
  972. { check if the next instruction fits as well
  973. or if we splitted after the it so split before }
  974. CheckLimit(hp,1);
  975. end;
  976. A_ITE,
  977. A_ITT:
  978. begin
  979. if GenerateThumb2Code then
  980. penalty:=2*multiplier;
  981. { check if the next two instructions fit as well
  982. or if we splitted them so split before }
  983. CheckLimit(hp,2);
  984. end;
  985. A_ITEE,
  986. A_ITTE,
  987. A_ITET,
  988. A_ITTT:
  989. begin
  990. if GenerateThumb2Code then
  991. penalty:=3*multiplier;
  992. { check if the next three instructions fit as well
  993. or if we splitted them so split before }
  994. CheckLimit(hp,3);
  995. end;
  996. A_ITEEE,
  997. A_ITTEE,
  998. A_ITETE,
  999. A_ITTTE,
  1000. A_ITEET,
  1001. A_ITTET,
  1002. A_ITETT,
  1003. A_ITTTT:
  1004. begin
  1005. if GenerateThumb2Code then
  1006. penalty:=4*multiplier;
  1007. { check if the next three instructions fit as well
  1008. or if we splitted them so split before }
  1009. CheckLimit(hp,4);
  1010. end;
  1011. end;
  1012. end;
  1013. CheckLimit(curtai,1);
  1014. { don't miss an insert }
  1015. doinsert:=doinsert or
  1016. (not(curdata.empty) and
  1017. (curinspos-lastinspos+penalty+extradataoffset>limit));
  1018. { split only at real instructions else the test below fails }
  1019. if doinsert and (curtai.typ=ait_instruction) and
  1020. (
  1021. { don't split loads of pc to lr and the following move }
  1022. not(
  1023. (taicpu(curtai).opcode=A_MOV) and
  1024. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1025. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1026. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1027. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1028. )
  1029. ) and
  1030. (
  1031. { do not insert data after a B instruction due to their limited range }
  1032. not((GenerateThumbCode) and
  1033. (taicpu(curtai).opcode=A_B)
  1034. )
  1035. ) then
  1036. begin
  1037. lastinspos:=-1;
  1038. extradataoffset:=0;
  1039. if GenerateThumbCode then
  1040. limit:=502
  1041. else
  1042. limit:=1016;
  1043. { if this is an add/tbh/tbb-based jumptable, go back to the
  1044. previous instruction, because inserting data between the
  1045. dispatch instruction and the table would mess up the
  1046. addresses }
  1047. inserttai:=curtai;
  1048. if is_case_dispatch(taicpu(inserttai)) and
  1049. ((taicpu(inserttai).opcode=A_ADD) or
  1050. (taicpu(inserttai).opcode=A_TBH) or
  1051. (taicpu(inserttai).opcode=A_TBB)) then
  1052. begin
  1053. repeat
  1054. inserttai:=tai(inserttai.previous);
  1055. until inserttai.typ=ait_instruction;
  1056. { if it's an add-based jump table, then also skip the
  1057. pc-relative load }
  1058. if taicpu(curtai).opcode=A_ADD then
  1059. repeat
  1060. inserttai:=tai(inserttai.previous);
  1061. until inserttai.typ=ait_instruction;
  1062. end
  1063. else
  1064. { on arm thumb, insert the data always after all labels etc. following an instruction so it
  1065. is prevent that a bxx yyy; bl xxx; yyyy: sequence gets separated ( we never insert on arm thumb after
  1066. bxx) and the distance of bxx gets too long }
  1067. if GenerateThumbCode then
  1068. while assigned(tai(inserttai.Next)) and (tai(inserttai.Next).typ in SkipInstr+[ait_label]) do
  1069. inserttai:=tai(inserttai.next);
  1070. doinsert:=false;
  1071. current_asmdata.getjumplabel(l);
  1072. { align jump in thumb .text section to 4 bytes }
  1073. if not(curdata.empty) and (GenerateThumbCode) then
  1074. curdata.Insert(tai_align.Create(4));
  1075. curdata.insert(taicpu.op_sym(A_B,l));
  1076. curdata.concat(tai_label.create(l));
  1077. { mark all labels as inserted, arm thumb
  1078. needs this, so data referencing an already inserted label can be
  1079. duplicated because arm thumb does not allow negative pc relative offset }
  1080. hp2:=tai(curdata.first);
  1081. while assigned(hp2) do
  1082. begin
  1083. if hp2.typ=ait_label then
  1084. tai_label(hp2).inserted:=true;
  1085. hp2:=tai(hp2.next);
  1086. end;
  1087. { continue with the last inserted label because we use later
  1088. on SimpleGetNextInstruction, so if we used curtai.next (which
  1089. is then equal curdata.last.previous) we could over see one
  1090. instruction }
  1091. hp:=tai(curdata.Last);
  1092. list.insertlistafter(inserttai,curdata);
  1093. curtai:=hp;
  1094. end
  1095. else
  1096. curtai:=tai(curtai.next);
  1097. end;
  1098. { align jump in thumb .text section to 4 bytes }
  1099. if not(curdata.empty) and (GenerateThumbCode or GenerateThumb2Code) then
  1100. curdata.Insert(tai_align.Create(4));
  1101. list.concatlist(curdata);
  1102. curdata.free;
  1103. end;
  1104. procedure ensurethumb2encodings(list: TAsmList);
  1105. var
  1106. curtai: tai;
  1107. op2reg: TRegister;
  1108. begin
  1109. { Do Thumb-2 16bit -> 32bit transformations }
  1110. curtai:=tai(list.first);
  1111. while assigned(curtai) do
  1112. begin
  1113. case curtai.typ of
  1114. ait_instruction:
  1115. begin
  1116. case taicpu(curtai).opcode of
  1117. A_ADD:
  1118. begin
  1119. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  1120. if taicpu(curtai).ops = 3 then
  1121. begin
  1122. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  1123. begin
  1124. if taicpu(curtai).oper[2]^.typ = top_reg then
  1125. op2reg := taicpu(curtai).oper[2]^.reg
  1126. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  1127. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  1128. else
  1129. op2reg := NR_NO;
  1130. if op2reg <> NR_NO then
  1131. begin
  1132. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  1133. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  1134. (op2reg >= NR_R8) then
  1135. begin
  1136. taicpu(curtai).wideformat:=true;
  1137. { Handle special cases where register rules are violated by optimizer/user }
  1138. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  1139. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  1140. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  1141. begin
  1142. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  1143. taicpu(curtai).oper[1]^.reg := op2reg;
  1144. end;
  1145. end;
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. end;
  1151. end;
  1152. end;
  1153. curtai:=tai(curtai.Next);
  1154. end;
  1155. end;
  1156. procedure ensurethumbencodings(list: TAsmList);
  1157. var
  1158. curtai: tai;
  1159. op2reg: TRegister;
  1160. begin
  1161. { Do Thumb 16bit transformations to form valid instruction forms }
  1162. curtai:=tai(list.first);
  1163. while assigned(curtai) do
  1164. begin
  1165. case curtai.typ of
  1166. ait_instruction:
  1167. begin
  1168. case taicpu(curtai).opcode of
  1169. A_ADD,
  1170. A_AND,A_EOR,A_ORR,A_BIC,
  1171. A_LSL,A_LSR,A_ASR,A_ROR,
  1172. A_ADC,A_SBC:
  1173. begin
  1174. if (taicpu(curtai).ops = 3) and
  1175. (taicpu(curtai).oper[2]^.typ=top_reg) and
  1176. (taicpu(curtai).oper[0]^.reg=taicpu(curtai).oper[1]^.reg) and
  1177. (taicpu(curtai).oper[0]^.reg<>NR_STACK_POINTER_REG) then
  1178. begin
  1179. taicpu(curtai).oper[1]^.reg:=taicpu(curtai).oper[2]^.reg;
  1180. taicpu(curtai).ops:=2;
  1181. end;
  1182. end;
  1183. end;
  1184. end;
  1185. end;
  1186. curtai:=tai(curtai.Next);
  1187. end;
  1188. end;
  1189. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  1190. const
  1191. opTable: array[A_IT..A_ITTTT] of string =
  1192. ('T','TE','TT','TEE','TTE','TET','TTT',
  1193. 'TEEE','TTEE','TETE','TTTE',
  1194. 'TEET','TTET','TETT','TTTT');
  1195. invertedOpTable: array[A_IT..A_ITTTT] of string =
  1196. ('E','ET','EE','ETT','EET','ETE','EEE',
  1197. 'ETTT','EETT','ETET','EEET',
  1198. 'ETTE','EETE','ETEE','EEEE');
  1199. var
  1200. resStr : string;
  1201. i : TAsmOp;
  1202. begin
  1203. if InvertLast then
  1204. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  1205. else
  1206. resStr := opTable[FirstOp]+opTable[LastOp];
  1207. if length(resStr) > 4 then
  1208. internalerror(2012100805);
  1209. for i := low(opTable) to high(opTable) do
  1210. if opTable[i] = resStr then
  1211. exit(i);
  1212. internalerror(2012100806);
  1213. end;
  1214. procedure foldITInstructions(list: TAsmList);
  1215. var
  1216. curtai,hp1 : tai;
  1217. levels,i : LongInt;
  1218. begin
  1219. curtai:=tai(list.First);
  1220. while assigned(curtai) do
  1221. begin
  1222. case curtai.typ of
  1223. ait_instruction:
  1224. if IsIT(taicpu(curtai).opcode) then
  1225. begin
  1226. levels := GetITLevels(taicpu(curtai).opcode);
  1227. if levels < 4 then
  1228. begin
  1229. i:=levels;
  1230. hp1:=tai(curtai.Next);
  1231. while assigned(hp1) and
  1232. (i > 0) do
  1233. begin
  1234. if hp1.typ=ait_instruction then
  1235. begin
  1236. dec(i);
  1237. if (i = 0) and
  1238. mustbelast(hp1) then
  1239. begin
  1240. hp1:=nil;
  1241. break;
  1242. end;
  1243. end;
  1244. hp1:=tai(hp1.Next);
  1245. end;
  1246. if assigned(hp1) then
  1247. begin
  1248. // We are pointing at the first instruction after the IT block
  1249. while assigned(hp1) and
  1250. (hp1.typ<>ait_instruction) do
  1251. hp1:=tai(hp1.Next);
  1252. if assigned(hp1) and
  1253. (hp1.typ=ait_instruction) and
  1254. IsIT(taicpu(hp1).opcode) then
  1255. begin
  1256. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1257. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1258. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1259. begin
  1260. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1261. taicpu(hp1).opcode,
  1262. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1263. list.Remove(hp1);
  1264. hp1.Free;
  1265. end;
  1266. end;
  1267. end;
  1268. end;
  1269. end;
  1270. end;
  1271. curtai:=tai(curtai.Next);
  1272. end;
  1273. end;
  1274. procedure fix_invalid_imms(list: TAsmList);
  1275. var
  1276. curtai: tai;
  1277. sh: byte;
  1278. begin
  1279. curtai:=tai(list.First);
  1280. while assigned(curtai) do
  1281. begin
  1282. case curtai.typ of
  1283. ait_instruction:
  1284. begin
  1285. if (taicpu(curtai).opcode in [A_AND,A_BIC]) and
  1286. (taicpu(curtai).ops=3) and
  1287. (taicpu(curtai).oper[2]^.typ=top_const) and
  1288. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1289. is_shifter_const((not taicpu(curtai).oper[2]^.val) and $FFFFFFFF,sh) then
  1290. begin
  1291. case taicpu(curtai).opcode of
  1292. A_AND: taicpu(curtai).opcode:=A_BIC;
  1293. A_BIC: taicpu(curtai).opcode:=A_AND;
  1294. end;
  1295. taicpu(curtai).oper[2]^.val:=(not taicpu(curtai).oper[2]^.val) and $FFFFFFFF;
  1296. end
  1297. else if (taicpu(curtai).opcode in [A_SUB,A_ADD]) and
  1298. (taicpu(curtai).ops=3) and
  1299. (taicpu(curtai).oper[2]^.typ=top_const) and
  1300. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1301. is_shifter_const(-taicpu(curtai).oper[2]^.val,sh) then
  1302. begin
  1303. case taicpu(curtai).opcode of
  1304. A_ADD: taicpu(curtai).opcode:=A_SUB;
  1305. A_SUB: taicpu(curtai).opcode:=A_ADD;
  1306. end;
  1307. taicpu(curtai).oper[2]^.val:=-taicpu(curtai).oper[2]^.val;
  1308. end;
  1309. end;
  1310. end;
  1311. curtai:=tai(curtai.Next);
  1312. end;
  1313. end;
  1314. procedure gather_it_info(list: TAsmList);
  1315. var
  1316. curtai: tai;
  1317. in_it: boolean;
  1318. it_count: longint;
  1319. begin
  1320. in_it:=false;
  1321. it_count:=0;
  1322. curtai:=tai(list.First);
  1323. while assigned(curtai) do
  1324. begin
  1325. case curtai.typ of
  1326. ait_instruction:
  1327. begin
  1328. case taicpu(curtai).opcode of
  1329. A_IT..A_ITTTT:
  1330. begin
  1331. if in_it then
  1332. Message1(asmw_e_invalid_opcode_and_operands, 'ITxx instruction is inside another ITxx instruction')
  1333. else
  1334. begin
  1335. in_it:=true;
  1336. it_count:=GetITLevels(taicpu(curtai).opcode);
  1337. end;
  1338. end;
  1339. else
  1340. begin
  1341. taicpu(curtai).inIT:=in_it;
  1342. taicpu(curtai).lastinIT:=in_it and (it_count=1);
  1343. if in_it then
  1344. begin
  1345. dec(it_count);
  1346. if it_count <= 0 then
  1347. in_it:=false;
  1348. end;
  1349. end;
  1350. end;
  1351. end;
  1352. end;
  1353. curtai:=tai(curtai.Next);
  1354. end;
  1355. end;
  1356. { Expands pseudo instructions ( mov r1,r2,lsl #4 -> lsl r1,r2,#4) }
  1357. procedure expand_instructions(list: TAsmList);
  1358. var
  1359. curtai: tai;
  1360. begin
  1361. curtai:=tai(list.First);
  1362. while assigned(curtai) do
  1363. begin
  1364. case curtai.typ of
  1365. ait_instruction:
  1366. begin
  1367. case taicpu(curtai).opcode of
  1368. A_MOV:
  1369. begin
  1370. if (taicpu(curtai).ops=3) and
  1371. (taicpu(curtai).oper[2]^.typ=top_shifterop) then
  1372. begin
  1373. case taicpu(curtai).oper[2]^.shifterop^.shiftmode of
  1374. SM_LSL: taicpu(curtai).opcode:=A_LSL;
  1375. SM_LSR: taicpu(curtai).opcode:=A_LSR;
  1376. SM_ASR: taicpu(curtai).opcode:=A_ASR;
  1377. SM_ROR: taicpu(curtai).opcode:=A_ROR;
  1378. SM_RRX: taicpu(curtai).opcode:=A_RRX;
  1379. end;
  1380. if taicpu(curtai).oper[2]^.shifterop^.shiftmode=SM_RRX then
  1381. taicpu(curtai).ops:=2;
  1382. if taicpu(curtai).oper[2]^.shifterop^.rs=NR_NO then
  1383. taicpu(curtai).loadconst(2, taicpu(curtai).oper[2]^.shifterop^.shiftimm)
  1384. else
  1385. taicpu(curtai).loadreg(2, taicpu(curtai).oper[2]^.shifterop^.rs);
  1386. end;
  1387. end;
  1388. A_NEG:
  1389. begin
  1390. taicpu(curtai).opcode:=A_RSB;
  1391. taicpu(curtai).oppostfix:=PF_S; // NEG should always set flags (according to documentation NEG<c> = RSBS<c>)
  1392. if taicpu(curtai).ops=2 then
  1393. begin
  1394. taicpu(curtai).loadconst(2,0);
  1395. taicpu(curtai).ops:=3;
  1396. end
  1397. else
  1398. begin
  1399. taicpu(curtai).loadconst(1,0);
  1400. taicpu(curtai).ops:=2;
  1401. end;
  1402. end;
  1403. A_SWI:
  1404. begin
  1405. taicpu(curtai).opcode:=A_SVC;
  1406. end;
  1407. end;
  1408. end;
  1409. end;
  1410. curtai:=tai(curtai.Next);
  1411. end;
  1412. end;
  1413. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1414. begin
  1415. { Don't expand pseudo instructions when using GAS, it breaks on some thumb instructions }
  1416. if target_asm.id<>as_gas then
  1417. expand_instructions(list);
  1418. { Do Thumb-2 16bit -> 32bit transformations }
  1419. if GenerateThumb2Code then
  1420. begin
  1421. ensurethumbencodings(list);
  1422. ensurethumb2encodings(list);
  1423. foldITInstructions(list);
  1424. end
  1425. else if GenerateThumbCode then
  1426. ensurethumbencodings(list);
  1427. gather_it_info(list);
  1428. fix_invalid_imms(list);
  1429. insertpcrelativedata(list, listtoinsert);
  1430. end;
  1431. procedure InsertPData;
  1432. var
  1433. prolog: TAsmList;
  1434. begin
  1435. prolog:=TAsmList.create;
  1436. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1437. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1438. prolog.concat(Tai_const.Create_32bit(0));
  1439. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1440. { dummy function }
  1441. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1442. current_asmdata.asmlists[al_start].insertList(prolog);
  1443. prolog.Free;
  1444. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1445. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1446. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1447. end;
  1448. (*
  1449. Floating point instruction format information, taken from the linux kernel
  1450. ARM Floating Point Instruction Classes
  1451. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1452. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  1453. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  1454. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1455. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  1456. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  1457. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  1458. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1459. CPDT data transfer instructions
  1460. LDF, STF, LFM (copro 2), SFM (copro 2)
  1461. CPDO dyadic arithmetic instructions
  1462. ADF, MUF, SUF, RSF, DVF, RDF,
  1463. POW, RPW, RMF, FML, FDV, FRD, POL
  1464. CPDO monadic arithmetic instructions
  1465. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1466. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1467. CPRT joint arithmetic/data transfer instructions
  1468. FIX (arithmetic followed by load/store)
  1469. FLT (load/store followed by arithmetic)
  1470. CMF, CNF CMFE, CNFE (comparisons)
  1471. WFS, RFS (write/read floating point status register)
  1472. WFC, RFC (write/read floating point control register)
  1473. cond condition codes
  1474. P pre/post index bit: 0 = postindex, 1 = preindex
  1475. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1476. W write back bit: 1 = update base register (Rn)
  1477. L load/store bit: 0 = store, 1 = load
  1478. Rn base register
  1479. Rd destination/source register
  1480. Fd floating point destination register
  1481. Fn floating point source register
  1482. Fm floating point source register or floating point constant
  1483. uv transfer length (TABLE 1)
  1484. wx register count (TABLE 2)
  1485. abcd arithmetic opcode (TABLES 3 & 4)
  1486. ef destination size (rounding precision) (TABLE 5)
  1487. gh rounding mode (TABLE 6)
  1488. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1489. i constant bit: 1 = constant (TABLE 6)
  1490. */
  1491. /*
  1492. TABLE 1
  1493. +-------------------------+---+---+---------+---------+
  1494. | Precision | u | v | FPSR.EP | length |
  1495. +-------------------------+---+---+---------+---------+
  1496. | Single | 0 | 0 | x | 1 words |
  1497. | Double | 1 | 1 | x | 2 words |
  1498. | Extended | 1 | 1 | x | 3 words |
  1499. | Packed decimal | 1 | 1 | 0 | 3 words |
  1500. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1501. +-------------------------+---+---+---------+---------+
  1502. Note: x = don't care
  1503. */
  1504. /*
  1505. TABLE 2
  1506. +---+---+---------------------------------+
  1507. | w | x | Number of registers to transfer |
  1508. +---+---+---------------------------------+
  1509. | 0 | 1 | 1 |
  1510. | 1 | 0 | 2 |
  1511. | 1 | 1 | 3 |
  1512. | 0 | 0 | 4 |
  1513. +---+---+---------------------------------+
  1514. */
  1515. /*
  1516. TABLE 3: Dyadic Floating Point Opcodes
  1517. +---+---+---+---+----------+-----------------------+-----------------------+
  1518. | a | b | c | d | Mnemonic | Description | Operation |
  1519. +---+---+---+---+----------+-----------------------+-----------------------+
  1520. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1521. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1522. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1523. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1524. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1525. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1526. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1527. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1528. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1529. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1530. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1531. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1532. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1533. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1534. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1535. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1536. +---+---+---+---+----------+-----------------------+-----------------------+
  1537. Note: POW, RPW, POL are deprecated, and are available for backwards
  1538. compatibility only.
  1539. */
  1540. /*
  1541. TABLE 4: Monadic Floating Point Opcodes
  1542. +---+---+---+---+----------+-----------------------+-----------------------+
  1543. | a | b | c | d | Mnemonic | Description | Operation |
  1544. +---+---+---+---+----------+-----------------------+-----------------------+
  1545. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1546. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1547. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1548. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1549. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1550. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1551. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1552. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1553. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1554. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1555. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1556. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1557. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1558. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1559. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1560. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1561. +---+---+---+---+----------+-----------------------+-----------------------+
  1562. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1563. available for backwards compatibility only.
  1564. */
  1565. /*
  1566. TABLE 5
  1567. +-------------------------+---+---+
  1568. | Rounding Precision | e | f |
  1569. +-------------------------+---+---+
  1570. | IEEE Single precision | 0 | 0 |
  1571. | IEEE Double precision | 0 | 1 |
  1572. | IEEE Extended precision | 1 | 0 |
  1573. | undefined (trap) | 1 | 1 |
  1574. +-------------------------+---+---+
  1575. */
  1576. /*
  1577. TABLE 5
  1578. +---------------------------------+---+---+
  1579. | Rounding Mode | g | h |
  1580. +---------------------------------+---+---+
  1581. | Round to nearest (default) | 0 | 0 |
  1582. | Round toward plus infinity | 0 | 1 |
  1583. | Round toward negative infinity | 1 | 0 |
  1584. | Round toward zero | 1 | 1 |
  1585. +---------------------------------+---+---+
  1586. *)
  1587. function taicpu.GetString:string;
  1588. var
  1589. i : longint;
  1590. s : string;
  1591. addsize : boolean;
  1592. begin
  1593. s:='['+gas_op2str[opcode];
  1594. for i:=0 to ops-1 do
  1595. begin
  1596. with oper[i]^ do
  1597. begin
  1598. if i=0 then
  1599. s:=s+' '
  1600. else
  1601. s:=s+',';
  1602. { type }
  1603. addsize:=false;
  1604. if (ot and OT_VREG)=OT_VREG then
  1605. s:=s+'vreg'
  1606. else
  1607. if (ot and OT_FPUREG)=OT_FPUREG then
  1608. s:=s+'fpureg'
  1609. else
  1610. if (ot and OT_REGS)=OT_REGS then
  1611. s:=s+'sreg'
  1612. else
  1613. if (ot and OT_REGF)=OT_REGF then
  1614. s:=s+'creg'
  1615. else
  1616. if (ot and OT_REGISTER)=OT_REGISTER then
  1617. begin
  1618. s:=s+'reg';
  1619. addsize:=true;
  1620. end
  1621. else
  1622. if (ot and OT_REGLIST)=OT_REGLIST then
  1623. begin
  1624. s:=s+'reglist';
  1625. addsize:=false;
  1626. end
  1627. else
  1628. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1629. begin
  1630. s:=s+'imm';
  1631. addsize:=true;
  1632. end
  1633. else
  1634. if (ot and OT_MEMORY)=OT_MEMORY then
  1635. begin
  1636. s:=s+'mem';
  1637. addsize:=true;
  1638. if (ot and OT_AM2)<>0 then
  1639. s:=s+' am2 '
  1640. else if (ot and OT_AM6)<>0 then
  1641. s:=s+' am2 ';
  1642. end
  1643. else
  1644. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1645. begin
  1646. s:=s+'shifterop';
  1647. addsize:=false;
  1648. end
  1649. else
  1650. s:=s+'???';
  1651. { size }
  1652. if addsize then
  1653. begin
  1654. if (ot and OT_BITS8)<>0 then
  1655. s:=s+'8'
  1656. else
  1657. if (ot and OT_BITS16)<>0 then
  1658. s:=s+'24'
  1659. else
  1660. if (ot and OT_BITS32)<>0 then
  1661. s:=s+'32'
  1662. else
  1663. if (ot and OT_BITSSHIFTER)<>0 then
  1664. s:=s+'shifter'
  1665. else
  1666. s:=s+'??';
  1667. { signed }
  1668. if (ot and OT_SIGNED)<>0 then
  1669. s:=s+'s';
  1670. end;
  1671. end;
  1672. end;
  1673. GetString:=s+']';
  1674. end;
  1675. procedure taicpu.ResetPass1;
  1676. begin
  1677. { we need to reset everything here, because the choosen insentry
  1678. can be invalid for a new situation where the previously optimized
  1679. insentry is not correct }
  1680. InsEntry:=nil;
  1681. InsSize:=0;
  1682. LastInsOffset:=-1;
  1683. end;
  1684. procedure taicpu.ResetPass2;
  1685. begin
  1686. { we are here in a second pass, check if the instruction can be optimized }
  1687. if assigned(InsEntry) and
  1688. ((InsEntry^.flags and IF_PASS2)<>0) then
  1689. begin
  1690. InsEntry:=nil;
  1691. InsSize:=0;
  1692. end;
  1693. LastInsOffset:=-1;
  1694. end;
  1695. function taicpu.CheckIfValid:boolean;
  1696. begin
  1697. Result:=False; { unimplemented }
  1698. end;
  1699. function taicpu.Pass1(objdata:TObjData):longint;
  1700. var
  1701. ldr2op : array[PF_B..PF_T] of tasmop = (
  1702. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1703. str2op : array[PF_B..PF_T] of tasmop = (
  1704. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1705. begin
  1706. Pass1:=0;
  1707. { Save the old offset and set the new offset }
  1708. InsOffset:=ObjData.CurrObjSec.Size;
  1709. { Error? }
  1710. if (Insentry=nil) and (InsSize=-1) then
  1711. exit;
  1712. { set the file postion }
  1713. current_filepos:=fileinfo;
  1714. { tranlate LDR+postfix to complete opcode }
  1715. if (opcode=A_LDR) and (oppostfix=PF_D) then
  1716. begin
  1717. opcode:=A_LDRD;
  1718. oppostfix:=PF_None;
  1719. end
  1720. else if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1721. begin
  1722. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1723. opcode:=ldr2op[oppostfix]
  1724. else
  1725. internalerror(2005091001);
  1726. if opcode=A_None then
  1727. internalerror(2005091004);
  1728. { postfix has been added to opcode }
  1729. oppostfix:=PF_None;
  1730. end
  1731. else if (opcode=A_STR) and (oppostfix=PF_D) then
  1732. begin
  1733. opcode:=A_STRD;
  1734. oppostfix:=PF_None;
  1735. end
  1736. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1737. begin
  1738. if (oppostfix in [low(str2op)..high(str2op)]) then
  1739. opcode:=str2op[oppostfix]
  1740. else
  1741. internalerror(2005091002);
  1742. if opcode=A_None then
  1743. internalerror(2005091003);
  1744. { postfix has been added to opcode }
  1745. oppostfix:=PF_None;
  1746. end;
  1747. { Get InsEntry }
  1748. if FindInsEntry(objdata) then
  1749. begin
  1750. InsSize:=4;
  1751. if insentry^.code[0] in [#$60..#$6C] then
  1752. InsSize:=2;
  1753. LastInsOffset:=InsOffset;
  1754. Pass1:=InsSize;
  1755. exit;
  1756. end;
  1757. LastInsOffset:=-1;
  1758. end;
  1759. procedure taicpu.Pass2(objdata:TObjData);
  1760. begin
  1761. { error in pass1 ? }
  1762. if insentry=nil then
  1763. exit;
  1764. current_filepos:=fileinfo;
  1765. { Generate the instruction }
  1766. GenCode(objdata);
  1767. end;
  1768. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1769. begin
  1770. end;
  1771. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1772. begin
  1773. end;
  1774. procedure taicpu.ppubuildderefimploper(var o:toper);
  1775. begin
  1776. end;
  1777. procedure taicpu.ppuderefoper(var o:toper);
  1778. begin
  1779. end;
  1780. procedure taicpu.BuildArmMasks;
  1781. const
  1782. Masks: array[tcputype] of longint =
  1783. (
  1784. IF_NONE,
  1785. IF_ARMv4,
  1786. IF_ARMv4,
  1787. IF_ARMv4T or IF_ARMv4,
  1788. IF_ARMv4T or IF_ARMv4 or IF_ARMv5,
  1789. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T,
  1790. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE,
  1791. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ,
  1792. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6,
  1793. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K,
  1794. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2,
  1795. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z,
  1796. IF_ARMv4T or IF_ARMv5T or IF_ARMv6M,
  1797. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7,
  1798. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A,
  1799. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A or IF_ARMv7R,
  1800. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M,
  1801. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M or IF_ARMv7EM
  1802. );
  1803. FPUMasks: array[tfputype] of longword =
  1804. (
  1805. IF_NONE,
  1806. IF_NONE,
  1807. IF_NONE,
  1808. IF_FPA,
  1809. IF_FPA,
  1810. IF_FPA,
  1811. IF_VFPv2,
  1812. IF_VFPv2 or IF_VFPv3,
  1813. IF_VFPv2 or IF_VFPv3,
  1814. IF_NONE,
  1815. IF_VFPv2 or IF_VFPv3 or IF_VFPv4
  1816. );
  1817. begin
  1818. fArmVMask:=Masks[current_settings.cputype] or FPUMasks[current_settings.fputype];
  1819. if current_settings.instructionset=is_thumb then
  1820. begin
  1821. fArmMask:=IF_THUMB;
  1822. if CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype] then
  1823. fArmMask:=fArmMask or IF_THUMB32;
  1824. end
  1825. else
  1826. fArmMask:=IF_ARM32;
  1827. end;
  1828. function taicpu.InsEnd:longint;
  1829. begin
  1830. Result:=0; { unimplemented }
  1831. end;
  1832. procedure taicpu.create_ot(objdata:TObjData);
  1833. var
  1834. i,l,relsize : longint;
  1835. dummy : byte;
  1836. currsym : TObjSymbol;
  1837. begin
  1838. if ops=0 then
  1839. exit;
  1840. { update oper[].ot field }
  1841. for i:=0 to ops-1 do
  1842. with oper[i]^ do
  1843. begin
  1844. case typ of
  1845. top_regset:
  1846. begin
  1847. ot:=OT_REGLIST;
  1848. end;
  1849. top_reg :
  1850. begin
  1851. case getregtype(reg) of
  1852. R_INTREGISTER:
  1853. begin
  1854. ot:=OT_REG32 or OT_SHIFTEROP;
  1855. if getsupreg(reg)<8 then
  1856. ot:=ot or OT_REGLO
  1857. else if reg=NR_STACK_POINTER_REG then
  1858. ot:=ot or OT_REGSP;
  1859. end;
  1860. R_FPUREGISTER:
  1861. ot:=OT_FPUREG;
  1862. R_MMREGISTER:
  1863. ot:=OT_VREG;
  1864. R_SPECIALREGISTER:
  1865. ot:=OT_REGF;
  1866. else
  1867. internalerror(2005090901);
  1868. end;
  1869. end;
  1870. top_ref :
  1871. begin
  1872. if ref^.refaddr=addr_no then
  1873. begin
  1874. { create ot field }
  1875. { we should get the size here dependend on the
  1876. instruction }
  1877. if (ot and OT_SIZE_MASK)=0 then
  1878. ot:=OT_MEMORY or OT_BITS32
  1879. else
  1880. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1881. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1882. ot:=ot or OT_MEM_OFFS;
  1883. { if we need to fix a reference, we do it here }
  1884. { pc relative addressing }
  1885. if (ref^.base=NR_NO) and
  1886. (ref^.index=NR_NO) and
  1887. (ref^.shiftmode=SM_None)
  1888. { at least we should check if the destination symbol
  1889. is in a text section }
  1890. { and
  1891. (ref^.symbol^.owner="text") } then
  1892. ref^.base:=NR_PC;
  1893. { determine possible address modes }
  1894. if GenerateThumbCode or
  1895. GenerateThumb2Code then
  1896. begin
  1897. if (ref^.addressmode<>AM_OFFSET) then
  1898. ot:=ot or OT_AM2
  1899. else if (ref^.base=NR_PC) then
  1900. ot:=ot or OT_AM6
  1901. else if (ref^.base=NR_STACK_POINTER_REG) then
  1902. ot:=ot or OT_AM5
  1903. else if ref^.index=NR_NO then
  1904. ot:=ot or OT_AM4
  1905. else
  1906. ot:=ot or OT_AM3;
  1907. end;
  1908. if (ref^.base<>NR_NO) and
  1909. (opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
  1910. A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
  1911. (
  1912. (ref^.addressmode=AM_OFFSET) and
  1913. (ref^.index=NR_NO) and
  1914. (ref^.shiftmode=SM_None) and
  1915. (ref^.offset=0)
  1916. ) then
  1917. ot:=ot or OT_AM6
  1918. else if (ref^.base<>NR_NO) and
  1919. (
  1920. (
  1921. (ref^.index=NR_NO) and
  1922. (ref^.shiftmode=SM_None) and
  1923. (ref^.offset>=-4097) and
  1924. (ref^.offset<=4097)
  1925. ) or
  1926. (
  1927. (ref^.shiftmode=SM_None) and
  1928. (ref^.offset=0)
  1929. ) or
  1930. (
  1931. (ref^.index<>NR_NO) and
  1932. (ref^.shiftmode<>SM_None) and
  1933. (ref^.shiftimm<=32) and
  1934. (ref^.offset=0)
  1935. )
  1936. ) then
  1937. ot:=ot or OT_AM2;
  1938. if (ref^.index<>NR_NO) and
  1939. (oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  1940. PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  1941. PF_IAS,PF_DBS,PF_FDS,PF_EAS,
  1942. PF_IAX,PF_DBX,PF_FDX,PF_EAX]) and
  1943. (
  1944. (ref^.base=NR_NO) and
  1945. (ref^.shiftmode=SM_None) and
  1946. (ref^.offset=0)
  1947. ) then
  1948. ot:=ot or OT_AM4;
  1949. end
  1950. else
  1951. begin
  1952. l:=ref^.offset;
  1953. currsym:=ObjData.symbolref(ref^.symbol);
  1954. if assigned(currsym) then
  1955. inc(l,currsym.address);
  1956. relsize:=(InsOffset+2)-l;
  1957. if (relsize<-33554428) or (relsize>33554428) then
  1958. ot:=OT_IMM32
  1959. else
  1960. ot:=OT_IMM24;
  1961. end;
  1962. end;
  1963. top_local :
  1964. begin
  1965. { we should get the size here dependend on the
  1966. instruction }
  1967. if (ot and OT_SIZE_MASK)=0 then
  1968. ot:=OT_MEMORY or OT_BITS32
  1969. else
  1970. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1971. end;
  1972. top_const :
  1973. begin
  1974. ot:=OT_IMMEDIATE;
  1975. if (val=0) then
  1976. ot:=ot_immediatezero
  1977. else if is_shifter_const(val,dummy) then
  1978. ot:=OT_IMMSHIFTER
  1979. else if GenerateThumb2Code and is_thumb32_imm(val) then
  1980. ot:=OT_IMMSHIFTER
  1981. else
  1982. ot:=OT_IMM32
  1983. end;
  1984. top_none :
  1985. begin
  1986. { generated when there was an error in the
  1987. assembler reader. It never happends when generating
  1988. assembler }
  1989. end;
  1990. top_shifterop:
  1991. begin
  1992. ot:=OT_SHIFTEROP;
  1993. end;
  1994. top_conditioncode:
  1995. begin
  1996. ot:=OT_CONDITION;
  1997. end;
  1998. top_specialreg:
  1999. begin
  2000. ot:=OT_REGS;
  2001. end;
  2002. top_modeflags:
  2003. begin
  2004. ot:=OT_MODEFLAGS;
  2005. end;
  2006. else
  2007. internalerror(2004022623);
  2008. end;
  2009. end;
  2010. end;
  2011. function taicpu.Matches(p:PInsEntry):longint;
  2012. { * IF_SM stands for Size Match: any operand whose size is not
  2013. * explicitly specified by the template is `really' intended to be
  2014. * the same size as the first size-specified operand.
  2015. * Non-specification is tolerated in the input instruction, but
  2016. * _wrong_ specification is not.
  2017. *
  2018. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  2019. * three-operand instructions such as SHLD: it implies that the
  2020. * first two operands must match in size, but that the third is
  2021. * required to be _unspecified_.
  2022. *
  2023. * IF_SB invokes Size Byte: operands with unspecified size in the
  2024. * template are really bytes, and so no non-byte specification in
  2025. * the input instruction will be tolerated. IF_SW similarly invokes
  2026. * Size Word, and IF_SD invokes Size Doubleword.
  2027. *
  2028. * (The default state if neither IF_SM nor IF_SM2 is specified is
  2029. * that any operand with unspecified size in the template is
  2030. * required to have unspecified size in the instruction too...)
  2031. }
  2032. var
  2033. i{,j,asize,oprs} : longint;
  2034. {siz : array[0..3] of longint;}
  2035. begin
  2036. Matches:=100;
  2037. { Check the opcode and operands }
  2038. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  2039. begin
  2040. Matches:=0;
  2041. exit;
  2042. end;
  2043. { check ARM instruction version }
  2044. if (p^.flags and fArmVMask)=0 then
  2045. begin
  2046. Matches:=0;
  2047. exit;
  2048. end;
  2049. { check ARM instruction type }
  2050. if (p^.flags and fArmMask)=0 then
  2051. begin
  2052. Matches:=0;
  2053. exit;
  2054. end;
  2055. { Check wideformat flag }
  2056. if wideformat and ((p^.flags and IF_WIDE)=0) then
  2057. begin
  2058. matches:=0;
  2059. exit;
  2060. end;
  2061. { Check that no spurious colons or TOs are present }
  2062. for i:=0 to p^.ops-1 do
  2063. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  2064. begin
  2065. Matches:=0;
  2066. exit;
  2067. end;
  2068. { Check that the operand flags all match up }
  2069. for i:=0 to p^.ops-1 do
  2070. begin
  2071. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  2072. ((p^.optypes[i] and OT_SIZE_MASK) and
  2073. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  2074. begin
  2075. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  2076. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  2077. begin
  2078. Matches:=0;
  2079. exit;
  2080. end
  2081. else if ((p^.optypes[i] and OT_OPT_SIZE)<>0) and
  2082. ((p^.optypes[i] and OT_OPT_SIZE)<>(oper[i]^.ot and OT_OPT_SIZE)) then
  2083. begin
  2084. Matches:=0;
  2085. exit;
  2086. end
  2087. else
  2088. Matches:=1;
  2089. end;
  2090. end;
  2091. { check postfixes:
  2092. the existance of a certain postfix requires a
  2093. particular code }
  2094. { update condition flags
  2095. or floating point single }
  2096. if (oppostfix=PF_S) and
  2097. not(p^.code[0] in [#$04..#$0F,#$14..#$16,#$29,#$30,#$60..#$6B,#$80..#$82,#$A0..#$A2,#$44,#$94,#$42,#$92]) then
  2098. begin
  2099. Matches:=0;
  2100. exit;
  2101. end;
  2102. { floating point size }
  2103. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  2104. not(p^.code[0] in [
  2105. // FPA
  2106. #$A0..#$A2,
  2107. // old-school VFP
  2108. #$42,#$92,
  2109. // vldm/vstm
  2110. #$44,#$94]) then
  2111. begin
  2112. Matches:=0;
  2113. exit;
  2114. end;
  2115. { multiple load/store address modes }
  2116. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  2117. not(p^.code[0] in [
  2118. // ldr,str,ldrb,strb
  2119. #$17,
  2120. // stm,ldm
  2121. #$26,#$69,#$8C,
  2122. // vldm/vstm
  2123. #$44,#$94
  2124. ]) then
  2125. begin
  2126. Matches:=0;
  2127. exit;
  2128. end;
  2129. { we shouldn't see any opsize prefixes here }
  2130. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  2131. begin
  2132. Matches:=0;
  2133. exit;
  2134. end;
  2135. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  2136. begin
  2137. Matches:=0;
  2138. exit;
  2139. end;
  2140. { Check thumb flags }
  2141. if p^.code[0] in [#$60..#$61] then
  2142. begin
  2143. if (p^.code[0]=#$60) and
  2144. (GenerateThumb2Code and
  2145. ((not inIT) and (oppostfix<>PF_S)) or
  2146. (inIT and (condition=C_None))) then
  2147. begin
  2148. Matches:=0;
  2149. exit;
  2150. end
  2151. else if (p^.code[0]=#$61) and
  2152. (oppostfix=PF_S) then
  2153. begin
  2154. Matches:=0;
  2155. exit;
  2156. end;
  2157. end
  2158. else if p^.code[0]=#$62 then
  2159. begin
  2160. if (GenerateThumb2Code and
  2161. (condition<>C_None) and
  2162. (not inIT) and
  2163. (not lastinIT)) then
  2164. begin
  2165. Matches:=0;
  2166. exit;
  2167. end;
  2168. end
  2169. else if p^.code[0]=#$63 then
  2170. begin
  2171. if inIT then
  2172. begin
  2173. Matches:=0;
  2174. exit;
  2175. end;
  2176. end
  2177. else if p^.code[0]=#$64 then
  2178. begin
  2179. if (opcode=A_MUL) then
  2180. begin
  2181. if (ops=3) and
  2182. ((oper[2]^.typ<>top_reg) or
  2183. (oper[0]^.reg<>oper[2]^.reg)) then
  2184. begin
  2185. matches:=0;
  2186. exit;
  2187. end;
  2188. end;
  2189. end
  2190. else if p^.code[0]=#$6B then
  2191. begin
  2192. if inIT or
  2193. (oppostfix<>PF_S) then
  2194. begin
  2195. Matches:=0;
  2196. exit;
  2197. end;
  2198. end;
  2199. { Check operand sizes }
  2200. { as default an untyped size can get all the sizes, this is different
  2201. from nasm, but else we need to do a lot checking which opcodes want
  2202. size or not with the automatic size generation }
  2203. (*
  2204. asize:=longint($ffffffff);
  2205. if (p^.flags and IF_SB)<>0 then
  2206. asize:=OT_BITS8
  2207. else if (p^.flags and IF_SW)<>0 then
  2208. asize:=OT_BITS16
  2209. else if (p^.flags and IF_SD)<>0 then
  2210. asize:=OT_BITS32;
  2211. if (p^.flags and IF_ARMASK)<>0 then
  2212. begin
  2213. siz[0]:=0;
  2214. siz[1]:=0;
  2215. siz[2]:=0;
  2216. if (p^.flags and IF_AR0)<>0 then
  2217. siz[0]:=asize
  2218. else if (p^.flags and IF_AR1)<>0 then
  2219. siz[1]:=asize
  2220. else if (p^.flags and IF_AR2)<>0 then
  2221. siz[2]:=asize;
  2222. end
  2223. else
  2224. begin
  2225. { we can leave because the size for all operands is forced to be
  2226. the same
  2227. but not if IF_SB IF_SW or IF_SD is set PM }
  2228. if asize=-1 then
  2229. exit;
  2230. siz[0]:=asize;
  2231. siz[1]:=asize;
  2232. siz[2]:=asize;
  2233. end;
  2234. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  2235. begin
  2236. if (p^.flags and IF_SM2)<>0 then
  2237. oprs:=2
  2238. else
  2239. oprs:=p^.ops;
  2240. for i:=0 to oprs-1 do
  2241. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  2242. begin
  2243. for j:=0 to oprs-1 do
  2244. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  2245. break;
  2246. end;
  2247. end
  2248. else
  2249. oprs:=2;
  2250. { Check operand sizes }
  2251. for i:=0 to p^.ops-1 do
  2252. begin
  2253. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  2254. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  2255. { Immediates can always include smaller size }
  2256. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  2257. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  2258. Matches:=2;
  2259. end;
  2260. *)
  2261. end;
  2262. function taicpu.calcsize(p:PInsEntry):shortint;
  2263. begin
  2264. result:=4;
  2265. end;
  2266. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  2267. begin
  2268. Result:=False; { unimplemented }
  2269. end;
  2270. procedure taicpu.Swapoperands;
  2271. begin
  2272. end;
  2273. function taicpu.FindInsentry(objdata:TObjData):boolean;
  2274. var
  2275. i : longint;
  2276. begin
  2277. result:=false;
  2278. { Things which may only be done once, not when a second pass is done to
  2279. optimize }
  2280. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  2281. begin
  2282. { create the .ot fields }
  2283. create_ot(objdata);
  2284. BuildArmMasks;
  2285. { set the file postion }
  2286. current_filepos:=fileinfo;
  2287. end
  2288. else
  2289. begin
  2290. { we've already an insentry so it's valid }
  2291. result:=true;
  2292. exit;
  2293. end;
  2294. { Lookup opcode in the table }
  2295. InsSize:=-1;
  2296. i:=instabcache^[opcode];
  2297. if i=-1 then
  2298. begin
  2299. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  2300. exit;
  2301. end;
  2302. insentry:=@instab[i];
  2303. while (insentry^.opcode=opcode) do
  2304. begin
  2305. if matches(insentry)=100 then
  2306. begin
  2307. result:=true;
  2308. exit;
  2309. end;
  2310. inc(i);
  2311. insentry:=@instab[i];
  2312. end;
  2313. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2314. { No instruction found, set insentry to nil and inssize to -1 }
  2315. insentry:=nil;
  2316. inssize:=-1;
  2317. end;
  2318. procedure taicpu.gencode(objdata:TObjData);
  2319. const
  2320. CondVal : array[TAsmCond] of byte=(
  2321. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  2322. $B, $C, $D, $E, 0);
  2323. var
  2324. bytes, rd, rm, rn, d, m, n : dword;
  2325. bytelen : longint;
  2326. dp_operation : boolean;
  2327. i_field : byte;
  2328. currsym : TObjSymbol;
  2329. offset : longint;
  2330. refoper : poper;
  2331. msb : longint;
  2332. r: byte;
  2333. procedure setshifterop(op : byte);
  2334. var
  2335. r : byte;
  2336. imm : dword;
  2337. count : integer;
  2338. begin
  2339. case oper[op]^.typ of
  2340. top_const:
  2341. begin
  2342. i_field:=1;
  2343. if oper[op]^.val and $ff=oper[op]^.val then
  2344. bytes:=bytes or dword(oper[op]^.val)
  2345. else
  2346. begin
  2347. { calc rotate and adjust imm }
  2348. count:=0;
  2349. r:=0;
  2350. imm:=dword(oper[op]^.val);
  2351. repeat
  2352. imm:=RolDWord(imm, 2);
  2353. inc(r);
  2354. inc(count);
  2355. if count > 32 then
  2356. begin
  2357. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
  2358. exit;
  2359. end;
  2360. until (imm and $ff)=imm;
  2361. bytes:=bytes or (r shl 8) or imm;
  2362. end;
  2363. end;
  2364. top_reg:
  2365. begin
  2366. i_field:=0;
  2367. bytes:=bytes or getsupreg(oper[op]^.reg);
  2368. { does a real shifter op follow? }
  2369. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2370. with oper[op+1]^.shifterop^ do
  2371. begin
  2372. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2373. if shiftmode<>SM_RRX then
  2374. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2375. else
  2376. bytes:=bytes or (3 shl 5);
  2377. if getregtype(rs) <> R_INVALIDREGISTER then
  2378. begin
  2379. bytes:=bytes or (1 shl 4);
  2380. bytes:=bytes or (getsupreg(rs) shl 8);
  2381. end
  2382. end;
  2383. end;
  2384. else
  2385. internalerror(2005091103);
  2386. end;
  2387. end;
  2388. function MakeRegList(reglist: tcpuregisterset): word;
  2389. var
  2390. i, w: word;
  2391. begin
  2392. result:=0;
  2393. w:=1;
  2394. for i:=RS_R0 to RS_R15 do
  2395. begin
  2396. if i in reglist then
  2397. result:=result or w;
  2398. w:=w shl 1
  2399. end;
  2400. end;
  2401. function getcoproc(reg: tregister): byte;
  2402. begin
  2403. if reg=NR_p15 then
  2404. result:=15
  2405. else
  2406. begin
  2407. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2408. result:=0;
  2409. end;
  2410. end;
  2411. function getcoprocreg(reg: tregister): byte;
  2412. begin
  2413. result:=getsupreg(reg)-getsupreg(NR_CR0);
  2414. end;
  2415. function getmmreg(reg: tregister): byte;
  2416. begin
  2417. case reg of
  2418. NR_D0: result:=0;
  2419. NR_D1: result:=1;
  2420. NR_D2: result:=2;
  2421. NR_D3: result:=3;
  2422. NR_D4: result:=4;
  2423. NR_D5: result:=5;
  2424. NR_D6: result:=6;
  2425. NR_D7: result:=7;
  2426. NR_D8: result:=8;
  2427. NR_D9: result:=9;
  2428. NR_D10: result:=10;
  2429. NR_D11: result:=11;
  2430. NR_D12: result:=12;
  2431. NR_D13: result:=13;
  2432. NR_D14: result:=14;
  2433. NR_D15: result:=15;
  2434. NR_D16: result:=16;
  2435. NR_D17: result:=17;
  2436. NR_D18: result:=18;
  2437. NR_D19: result:=19;
  2438. NR_D20: result:=20;
  2439. NR_D21: result:=21;
  2440. NR_D22: result:=22;
  2441. NR_D23: result:=23;
  2442. NR_D24: result:=24;
  2443. NR_D25: result:=25;
  2444. NR_D26: result:=26;
  2445. NR_D27: result:=27;
  2446. NR_D28: result:=28;
  2447. NR_D29: result:=29;
  2448. NR_D30: result:=30;
  2449. NR_D31: result:=31;
  2450. NR_S0: result:=0;
  2451. NR_S1: result:=1;
  2452. NR_S2: result:=2;
  2453. NR_S3: result:=3;
  2454. NR_S4: result:=4;
  2455. NR_S5: result:=5;
  2456. NR_S6: result:=6;
  2457. NR_S7: result:=7;
  2458. NR_S8: result:=8;
  2459. NR_S9: result:=9;
  2460. NR_S10: result:=10;
  2461. NR_S11: result:=11;
  2462. NR_S12: result:=12;
  2463. NR_S13: result:=13;
  2464. NR_S14: result:=14;
  2465. NR_S15: result:=15;
  2466. NR_S16: result:=16;
  2467. NR_S17: result:=17;
  2468. NR_S18: result:=18;
  2469. NR_S19: result:=19;
  2470. NR_S20: result:=20;
  2471. NR_S21: result:=21;
  2472. NR_S22: result:=22;
  2473. NR_S23: result:=23;
  2474. NR_S24: result:=24;
  2475. NR_S25: result:=25;
  2476. NR_S26: result:=26;
  2477. NR_S27: result:=27;
  2478. NR_S28: result:=28;
  2479. NR_S29: result:=29;
  2480. NR_S30: result:=30;
  2481. NR_S31: result:=31;
  2482. else
  2483. result:=0;
  2484. end;
  2485. end;
  2486. procedure encodethumbimm(imm: longword);
  2487. var
  2488. imm12, tmp: tcgint;
  2489. shift: integer;
  2490. found: boolean;
  2491. begin
  2492. found:=true;
  2493. if (imm and $FF) = imm then
  2494. imm12:=imm
  2495. else if ((imm shr 16)=(imm and $FFFF)) and
  2496. ((imm and $FF00FF00) = 0) then
  2497. imm12:=(imm and $ff) or ($1 shl 8)
  2498. else if ((imm shr 16)=(imm and $FFFF)) and
  2499. ((imm and $00FF00FF) = 0) then
  2500. imm12:=((imm shr 8) and $ff) or ($2 shl 8)
  2501. else if ((imm shr 16)=(imm and $FFFF)) and
  2502. (((imm shr 8) and $FF)=(imm and $FF)) then
  2503. imm12:=(imm and $ff) or ($3 shl 8)
  2504. else
  2505. begin
  2506. found:=false;
  2507. imm12:=0;
  2508. for shift:=1 to 31 do
  2509. begin
  2510. tmp:=RolDWord(imm,shift);
  2511. if ((tmp and $FF)=tmp) and
  2512. ((tmp and $80)=$80) then
  2513. begin
  2514. imm12:=(tmp and $7F) or (shift shl 7);
  2515. found:=true;
  2516. break;
  2517. end;
  2518. end;
  2519. end;
  2520. if found then
  2521. begin
  2522. bytes:=bytes or (imm12 and $FF);
  2523. bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
  2524. bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
  2525. end
  2526. else
  2527. Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
  2528. end;
  2529. procedure setthumbshift(op: byte; is_sat: boolean = false);
  2530. var
  2531. shift,typ: byte;
  2532. begin
  2533. shift:=0;
  2534. typ:=0;
  2535. case oper[op]^.shifterop^.shiftmode of
  2536. SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
  2537. SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2538. SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2539. SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
  2540. SM_RRX: begin typ:=3; shift:=0; end;
  2541. end;
  2542. if is_sat then
  2543. begin
  2544. bytes:=bytes or ((typ and 1) shl 5);
  2545. bytes:=bytes or ((typ shr 1) shl 21);
  2546. end
  2547. else
  2548. bytes:=bytes or (typ shl 4);
  2549. bytes:=bytes or (shift and $3) shl 6;
  2550. bytes:=bytes or ((shift and $1C) shr 2) shl 12;
  2551. end;
  2552. begin
  2553. bytes:=$0;
  2554. bytelen:=4;
  2555. i_field:=0;
  2556. { evaluate and set condition code }
  2557. bytes:=bytes or (CondVal[condition] shl 28);
  2558. { condition code allowed? }
  2559. { setup rest of the instruction }
  2560. case insentry^.code[0] of
  2561. #$01: // B/BL
  2562. begin
  2563. { set instruction code }
  2564. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2565. { set offset }
  2566. if oper[0]^.typ=top_const then
  2567. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2568. else
  2569. begin
  2570. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2571. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2572. begin
  2573. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  2574. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2575. end
  2576. else
  2577. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2578. end;
  2579. end;
  2580. #$02:
  2581. begin
  2582. { set instruction code }
  2583. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2584. { set code }
  2585. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2586. end;
  2587. #$03:
  2588. begin // BLX/BX
  2589. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2590. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2591. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2592. bytes:=bytes or ord(insentry^.code[4]);
  2593. bytes:=bytes or getsupreg(oper[0]^.reg);
  2594. end;
  2595. #$04..#$07: // SUB
  2596. begin
  2597. { set instruction code }
  2598. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2599. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2600. { set destination }
  2601. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2602. { set Rn }
  2603. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2604. { create shifter op }
  2605. setshifterop(2);
  2606. { set I field }
  2607. bytes:=bytes or (i_field shl 25);
  2608. { set S if necessary }
  2609. if oppostfix=PF_S then
  2610. bytes:=bytes or (1 shl 20);
  2611. end;
  2612. #$08,#$0A,#$0B: // MOV
  2613. begin
  2614. { set instruction code }
  2615. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2616. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2617. { set destination }
  2618. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2619. { create shifter op }
  2620. setshifterop(1);
  2621. { set I field }
  2622. bytes:=bytes or (i_field shl 25);
  2623. { set S if necessary }
  2624. if oppostfix=PF_S then
  2625. bytes:=bytes or (1 shl 20);
  2626. end;
  2627. #$0C,#$0E,#$0F: // CMP
  2628. begin
  2629. { set instruction code }
  2630. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2631. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2632. { set destination }
  2633. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2634. { create shifter op }
  2635. setshifterop(1);
  2636. { set I field }
  2637. bytes:=bytes or (i_field shl 25);
  2638. { always set S bit }
  2639. bytes:=bytes or (1 shl 20);
  2640. end;
  2641. #$10: // MRS
  2642. begin
  2643. { set instruction code }
  2644. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2645. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2646. { set destination }
  2647. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2648. case oper[1]^.reg of
  2649. NR_APSR,NR_CPSR:;
  2650. NR_SPSR:
  2651. begin
  2652. bytes:=bytes or (1 shl 22);
  2653. end;
  2654. else
  2655. Message(asmw_e_invalid_opcode_and_operands);
  2656. end;
  2657. end;
  2658. #$12,#$13: // MSR
  2659. begin
  2660. { set instruction code }
  2661. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2662. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2663. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2664. { set destination }
  2665. if oper[0]^.typ=top_specialreg then
  2666. begin
  2667. if (oper[0]^.specialreg<>NR_CPSR) and
  2668. (oper[0]^.specialreg<>NR_SPSR) then
  2669. Message1(asmw_e_invalid_opcode_and_operands, '"Invalid special reg"');
  2670. if srC in oper[0]^.specialflags then
  2671. bytes:=bytes or (1 shl 16);
  2672. if srX in oper[0]^.specialflags then
  2673. bytes:=bytes or (1 shl 17);
  2674. if srS in oper[0]^.specialflags then
  2675. bytes:=bytes or (1 shl 18);
  2676. if srF in oper[0]^.specialflags then
  2677. bytes:=bytes or (1 shl 19);
  2678. { Set R bit }
  2679. if oper[0]^.specialreg=NR_SPSR then
  2680. bytes:=bytes or (1 shl 22);
  2681. end
  2682. else
  2683. case oper[0]^.reg of
  2684. NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
  2685. NR_APSR_g: bytes:=bytes or (1 shl 18);
  2686. NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
  2687. else
  2688. Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
  2689. end;
  2690. setshifterop(1);
  2691. end;
  2692. #$14: // MUL/MLA r1,r2,r3
  2693. begin
  2694. { set instruction code }
  2695. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2696. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2697. bytes:=bytes or ord(insentry^.code[3]);
  2698. { set regs }
  2699. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2700. bytes:=bytes or getsupreg(oper[1]^.reg);
  2701. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2702. if oppostfix in [PF_S] then
  2703. bytes:=bytes or (1 shl 20);
  2704. end;
  2705. #$15: // MUL/MLA r1,r2,r3,r4
  2706. begin
  2707. { set instruction code }
  2708. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2709. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2710. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2711. { set regs }
  2712. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2713. bytes:=bytes or getsupreg(oper[1]^.reg);
  2714. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2715. if ops>3 then
  2716. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2717. else
  2718. bytes:=bytes or ord(insentry^.code[4]) shl 12;
  2719. if oppostfix in [PF_R,PF_X] then
  2720. bytes:=bytes or (1 shl 5);
  2721. if oppostfix in [PF_S] then
  2722. bytes:=bytes or (1 shl 20);
  2723. end;
  2724. #$16: // MULL r1,r2,r3,r4
  2725. begin
  2726. { set instruction code }
  2727. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2728. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2729. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2730. { set regs }
  2731. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2732. if (ops=3) and (opcode=A_PKHTB) then
  2733. begin
  2734. bytes:=bytes or getsupreg(oper[1]^.reg);
  2735. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2736. end
  2737. else
  2738. begin
  2739. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2740. bytes:=bytes or getsupreg(oper[2]^.reg);
  2741. end;
  2742. if ops=4 then
  2743. begin
  2744. if oper[3]^.typ=top_shifterop then
  2745. begin
  2746. if opcode in [A_PKHBT,A_PKHTB] then
  2747. begin
  2748. if ((opcode=A_PKHTB) and
  2749. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2750. ((opcode=A_PKHBT) and
  2751. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2752. (oper[3]^.shifterop^.rs<>NR_NO) then
  2753. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2754. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2755. end
  2756. else
  2757. begin
  2758. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2759. (oper[3]^.shifterop^.rs<>NR_NO) or
  2760. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2761. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2762. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2763. end;
  2764. end
  2765. else
  2766. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2767. end;
  2768. if PF_S=oppostfix then
  2769. bytes:=bytes or (1 shl 20);
  2770. if PF_X=oppostfix then
  2771. bytes:=bytes or (1 shl 5);
  2772. end;
  2773. #$17: // LDR/STR
  2774. begin
  2775. { set instruction code }
  2776. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2777. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2778. { set Rn and Rd }
  2779. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2780. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2781. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2782. begin
  2783. { set offset }
  2784. offset:=0;
  2785. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2786. if assigned(currsym) then
  2787. offset:=currsym.offset-insoffset-8;
  2788. offset:=offset+oper[1]^.ref^.offset;
  2789. if offset>=0 then
  2790. { set U flag }
  2791. bytes:=bytes or (1 shl 23)
  2792. else
  2793. offset:=-offset;
  2794. bytes:=bytes or (offset and $FFF);
  2795. end
  2796. else
  2797. begin
  2798. { set U flag }
  2799. if oper[1]^.ref^.signindex>=0 then
  2800. bytes:=bytes or (1 shl 23);
  2801. { set I flag }
  2802. bytes:=bytes or (1 shl 25);
  2803. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2804. { set shift }
  2805. with oper[1]^.ref^ do
  2806. if shiftmode<>SM_None then
  2807. begin
  2808. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2809. if shiftmode<>SM_RRX then
  2810. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2811. else
  2812. bytes:=bytes or (3 shl 5);
  2813. end
  2814. end;
  2815. { set W bit }
  2816. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2817. bytes:=bytes or (1 shl 21);
  2818. { set P bit if necessary }
  2819. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2820. bytes:=bytes or (1 shl 24);
  2821. end;
  2822. #$18: // LDREX/STREX
  2823. begin
  2824. { set instruction code }
  2825. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2826. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2827. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2828. bytes:=bytes or ord(insentry^.code[4]);
  2829. { set Rn and Rd }
  2830. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2831. if (ops=3) then
  2832. begin
  2833. if opcode<>A_LDREXD then
  2834. bytes:=bytes or getsupreg(oper[1]^.reg);
  2835. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2836. end
  2837. else if (ops=4) then // STREXD
  2838. begin
  2839. if opcode<>A_LDREXD then
  2840. bytes:=bytes or getsupreg(oper[1]^.reg);
  2841. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2842. end
  2843. else
  2844. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2845. end;
  2846. #$19: // LDRD/STRD
  2847. begin
  2848. { set instruction code }
  2849. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2850. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2851. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2852. bytes:=bytes or ord(insentry^.code[4]);
  2853. { set Rn and Rd }
  2854. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2855. refoper:=oper[1];
  2856. if ops=3 then
  2857. refoper:=oper[2];
  2858. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2859. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2860. begin
  2861. bytes:=bytes or (1 shl 22);
  2862. { set offset }
  2863. offset:=0;
  2864. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2865. if assigned(currsym) then
  2866. offset:=currsym.offset-insoffset-8;
  2867. offset:=offset+refoper^.ref^.offset;
  2868. if offset>=0 then
  2869. { set U flag }
  2870. bytes:=bytes or (1 shl 23)
  2871. else
  2872. offset:=-offset;
  2873. bytes:=bytes or (offset and $F);
  2874. bytes:=bytes or ((offset and $F0) shl 4);
  2875. end
  2876. else
  2877. begin
  2878. { set U flag }
  2879. if refoper^.ref^.signindex>=0 then
  2880. bytes:=bytes or (1 shl 23);
  2881. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2882. end;
  2883. { set W bit }
  2884. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2885. bytes:=bytes or (1 shl 21);
  2886. { set P bit if necessary }
  2887. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2888. bytes:=bytes or (1 shl 24);
  2889. end;
  2890. #$1A: // QADD/QSUB
  2891. begin
  2892. { set instruction code }
  2893. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2894. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2895. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2896. { set regs }
  2897. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2898. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2899. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2900. end;
  2901. #$1B:
  2902. begin
  2903. { set instruction code }
  2904. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2905. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2906. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2907. { set regs }
  2908. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2909. bytes:=bytes or getsupreg(oper[1]^.reg);
  2910. if ops=3 then
  2911. begin
  2912. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2913. (oper[2]^.shifterop^.rs<>NR_NO) or
  2914. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2915. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2916. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2917. end;
  2918. end;
  2919. #$1C: // MCR/MRC
  2920. begin
  2921. { set instruction code }
  2922. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2923. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2924. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2925. { set regs and operands }
  2926. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2927. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2928. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2929. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2930. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2931. if ops > 5 then
  2932. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2933. end;
  2934. #$1D: // MCRR/MRRC
  2935. begin
  2936. { set instruction code }
  2937. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2938. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2939. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2940. { set regs and operands }
  2941. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2942. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2943. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2944. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2945. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2946. end;
  2947. #$1E: // LDRHT/STRHT
  2948. begin
  2949. { set instruction code }
  2950. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2951. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2952. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2953. bytes:=bytes or ord(insentry^.code[4]);
  2954. { set Rn and Rd }
  2955. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2956. refoper:=oper[1];
  2957. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2958. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2959. begin
  2960. bytes:=bytes or (1 shl 22);
  2961. { set offset }
  2962. offset:=0;
  2963. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2964. if assigned(currsym) then
  2965. offset:=currsym.offset-insoffset-8;
  2966. offset:=offset+refoper^.ref^.offset;
  2967. if offset>=0 then
  2968. { set U flag }
  2969. bytes:=bytes or (1 shl 23)
  2970. else
  2971. offset:=-offset;
  2972. bytes:=bytes or (offset and $F);
  2973. bytes:=bytes or ((offset and $F0) shl 4);
  2974. end
  2975. else
  2976. begin
  2977. { set U flag }
  2978. if refoper^.ref^.signindex>=0 then
  2979. bytes:=bytes or (1 shl 23);
  2980. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2981. end;
  2982. end;
  2983. #$22: // LDRH/STRH
  2984. begin
  2985. { set instruction code }
  2986. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2987. bytes:=bytes or ord(insentry^.code[2]);
  2988. { src/dest register (Rd) }
  2989. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2990. { base register (Rn) }
  2991. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2992. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2993. begin
  2994. bytes:=bytes or (1 shl 22); // with immediate offset
  2995. offset:=oper[1]^.ref^.offset;
  2996. if offset>=0 then
  2997. { set U flag }
  2998. bytes:=bytes or (1 shl 23)
  2999. else
  3000. offset:=-offset;
  3001. bytes:=bytes or (offset and $F);
  3002. bytes:=bytes or ((offset and $F0) shl 4);
  3003. end
  3004. else
  3005. begin
  3006. { set U flag }
  3007. if oper[1]^.ref^.signindex>=0 then
  3008. bytes:=bytes or (1 shl 23);
  3009. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  3010. end;
  3011. { set W bit }
  3012. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  3013. bytes:=bytes or (1 shl 21);
  3014. { set P bit if necessary }
  3015. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  3016. bytes:=bytes or (1 shl 24);
  3017. end;
  3018. #$25: // PLD/PLI
  3019. begin
  3020. { set instruction code }
  3021. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3022. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3023. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3024. bytes:=bytes or ord(insentry^.code[4]);
  3025. { set Rn and Rd }
  3026. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  3027. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  3028. begin
  3029. { set offset }
  3030. offset:=0;
  3031. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3032. if assigned(currsym) then
  3033. offset:=currsym.offset-insoffset-8;
  3034. offset:=offset+oper[0]^.ref^.offset;
  3035. if offset>=0 then
  3036. begin
  3037. { set U flag }
  3038. bytes:=bytes or (1 shl 23);
  3039. bytes:=bytes or offset
  3040. end
  3041. else
  3042. begin
  3043. offset:=-offset;
  3044. bytes:=bytes or offset
  3045. end;
  3046. end
  3047. else
  3048. begin
  3049. bytes:=bytes or (1 shl 25);
  3050. { set U flag }
  3051. if oper[0]^.ref^.signindex>=0 then
  3052. bytes:=bytes or (1 shl 23);
  3053. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  3054. { set shift }
  3055. with oper[0]^.ref^ do
  3056. if shiftmode<>SM_None then
  3057. begin
  3058. bytes:=bytes or ((shiftimm and $1F) shl 7);
  3059. if shiftmode<>SM_RRX then
  3060. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  3061. else
  3062. bytes:=bytes or (3 shl 5);
  3063. end
  3064. end;
  3065. end;
  3066. #$26: // LDM/STM
  3067. begin
  3068. { set instruction code }
  3069. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3070. if ops>1 then
  3071. begin
  3072. if oper[0]^.typ=top_ref then
  3073. begin
  3074. { set W bit }
  3075. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  3076. bytes:=bytes or (1 shl 21);
  3077. { set Rn }
  3078. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  3079. end
  3080. else { typ=top_reg }
  3081. begin
  3082. { set Rn }
  3083. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3084. end;
  3085. if oper[1]^.usermode then
  3086. begin
  3087. if (oper[0]^.typ=top_ref) then
  3088. begin
  3089. if (opcode=A_LDM) and
  3090. (RS_PC in oper[1]^.regset^) then
  3091. begin
  3092. // Valid exception return
  3093. end
  3094. else
  3095. Message(asmw_e_invalid_opcode_and_operands);
  3096. end;
  3097. bytes:=bytes or (1 shl 22);
  3098. end;
  3099. { reglist }
  3100. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  3101. end
  3102. else
  3103. begin
  3104. { push/pop }
  3105. { Set W and Rn to SP }
  3106. if opcode=A_PUSH then
  3107. bytes:=bytes or (1 shl 21);
  3108. bytes:=bytes or ($D shl 16);
  3109. { reglist }
  3110. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3111. end;
  3112. { set P bit }
  3113. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3114. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3115. or (opcode=A_PUSH) then
  3116. bytes:=bytes or (1 shl 24);
  3117. { set U bit }
  3118. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3119. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3120. or (opcode=A_POP) then
  3121. bytes:=bytes or (1 shl 23);
  3122. end;
  3123. #$27: // SWP/SWPB
  3124. begin
  3125. { set instruction code }
  3126. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3127. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3128. { set regs }
  3129. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3130. bytes:=bytes or getsupreg(oper[1]^.reg);
  3131. if ops=3 then
  3132. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3133. end;
  3134. #$28: // BX/BLX
  3135. begin
  3136. { set instruction code }
  3137. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3138. { set offset }
  3139. if oper[0]^.typ=top_const then
  3140. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3141. else
  3142. begin
  3143. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3144. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3145. begin
  3146. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3147. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3148. end
  3149. else
  3150. begin
  3151. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3152. { Turn BLX into BL if the destination isn't odd, could happen with recursion }
  3153. if not odd(offset shr 1) then
  3154. bytes:=(bytes and $EB000000) or $EB000000;
  3155. bytes:=bytes or ((offset shr 2) and $ffffff);
  3156. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3157. end;
  3158. end;
  3159. end;
  3160. #$29: // SUB
  3161. begin
  3162. { set instruction code }
  3163. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3164. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3165. { set regs }
  3166. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3167. { set S if necessary }
  3168. if oppostfix=PF_S then
  3169. bytes:=bytes or (1 shl 20);
  3170. end;
  3171. #$2A:
  3172. begin
  3173. { set instruction code }
  3174. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3175. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3176. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3177. bytes:=bytes or ord(insentry^.code[4]);
  3178. { set opers }
  3179. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3180. if opcode in [A_SSAT, A_SSAT16] then
  3181. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3182. else
  3183. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3184. bytes:=bytes or getsupreg(oper[2]^.reg);
  3185. if (ops>3) and
  3186. (oper[3]^.typ=top_shifterop) and
  3187. (oper[3]^.shifterop^.rs=NR_NO) then
  3188. begin
  3189. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3190. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3191. bytes:=bytes or (1 shl 6)
  3192. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3193. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3194. end;
  3195. end;
  3196. #$2B: // SETEND
  3197. begin
  3198. { set instruction code }
  3199. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3200. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3201. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3202. bytes:=bytes or ord(insentry^.code[4]);
  3203. { set endian specifier }
  3204. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3205. end;
  3206. #$2C: // MOVW
  3207. begin
  3208. { set instruction code }
  3209. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3210. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3211. { set destination }
  3212. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3213. { set imm }
  3214. bytes:=bytes or (oper[1]^.val and $FFF);
  3215. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3216. end;
  3217. #$2D: // BFX
  3218. begin
  3219. { set instruction code }
  3220. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3221. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3222. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3223. bytes:=bytes or ord(insentry^.code[4]);
  3224. if ops=3 then
  3225. begin
  3226. msb:=(oper[1]^.val+oper[2]^.val-1);
  3227. { set destination }
  3228. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3229. { set immediates }
  3230. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3231. bytes:=bytes or ((msb and $1F) shl 16);
  3232. end
  3233. else
  3234. begin
  3235. if opcode in [A_BFC,A_BFI] then
  3236. msb:=(oper[2]^.val+oper[3]^.val-1)
  3237. else
  3238. msb:=oper[3]^.val-1;
  3239. { set destination }
  3240. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3241. bytes:=bytes or getsupreg(oper[1]^.reg);
  3242. { set immediates }
  3243. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3244. bytes:=bytes or ((msb and $1F) shl 16);
  3245. end;
  3246. end;
  3247. #$2E: // Cache stuff
  3248. begin
  3249. { set instruction code }
  3250. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3251. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3252. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3253. bytes:=bytes or ord(insentry^.code[4]);
  3254. { set code }
  3255. bytes:=bytes or (oper[0]^.val and $F);
  3256. end;
  3257. #$2F: // Nop
  3258. begin
  3259. { set instruction code }
  3260. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3261. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3262. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3263. bytes:=bytes or ord(insentry^.code[4]);
  3264. end;
  3265. #$30: // Shifts
  3266. begin
  3267. { set instruction code }
  3268. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3269. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3270. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3271. bytes:=bytes or ord(insentry^.code[4]);
  3272. { set destination }
  3273. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3274. bytes:=bytes or getsupreg(oper[1]^.reg);
  3275. if ops>2 then
  3276. begin
  3277. { set shift }
  3278. if oper[2]^.typ=top_reg then
  3279. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3280. else
  3281. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3282. end;
  3283. { set S if necessary }
  3284. if oppostfix=PF_S then
  3285. bytes:=bytes or (1 shl 20);
  3286. end;
  3287. #$31: // BKPT
  3288. begin
  3289. { set instruction code }
  3290. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3291. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3292. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  3293. { set imm }
  3294. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3295. bytes:=bytes or (oper[0]^.val and $F);
  3296. end;
  3297. #$32: // CLZ/REV
  3298. begin
  3299. { set instruction code }
  3300. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3301. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3302. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3303. bytes:=bytes or ord(insentry^.code[4]);
  3304. { set regs }
  3305. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3306. bytes:=bytes or getsupreg(oper[1]^.reg);
  3307. end;
  3308. #$33:
  3309. begin
  3310. { set instruction code }
  3311. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3312. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3313. { set regs }
  3314. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3315. if oper[1]^.typ=top_ref then
  3316. begin
  3317. { set offset }
  3318. offset:=0;
  3319. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3320. if assigned(currsym) then
  3321. offset:=currsym.offset-insoffset-8;
  3322. offset:=offset+oper[1]^.ref^.offset;
  3323. if offset>=0 then
  3324. begin
  3325. { set U flag }
  3326. bytes:=bytes or (1 shl 23);
  3327. bytes:=bytes or offset
  3328. end
  3329. else
  3330. begin
  3331. bytes:=bytes or (1 shl 22);
  3332. offset:=-offset;
  3333. bytes:=bytes or offset
  3334. end;
  3335. end
  3336. else
  3337. begin
  3338. if is_shifter_const(oper[1]^.val,r) then
  3339. begin
  3340. setshifterop(1);
  3341. bytes:=bytes or (1 shl 23);
  3342. end
  3343. else
  3344. begin
  3345. bytes:=bytes or (1 shl 22);
  3346. oper[1]^.val:=-oper[1]^.val;
  3347. setshifterop(1);
  3348. end;
  3349. end;
  3350. end;
  3351. #$40,#$90: // VMOV
  3352. begin
  3353. { set instruction code }
  3354. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3355. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3356. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3357. bytes:=bytes or ord(insentry^.code[4]);
  3358. { set regs }
  3359. Rd:=0;
  3360. Rn:=0;
  3361. Rm:=0;
  3362. case oppostfix of
  3363. PF_None:
  3364. begin
  3365. if ops=4 then
  3366. begin
  3367. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3368. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3369. begin
  3370. Rd:=getmmreg(oper[0]^.reg);
  3371. Rm:=getsupreg(oper[2]^.reg);
  3372. Rn:=getsupreg(oper[3]^.reg);
  3373. end
  3374. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3375. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3376. begin
  3377. Rm:=getsupreg(oper[0]^.reg);
  3378. Rn:=getsupreg(oper[1]^.reg);
  3379. Rd:=getmmreg(oper[2]^.reg);
  3380. end
  3381. else
  3382. message(asmw_e_invalid_opcode_and_operands);
  3383. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3384. bytes:=bytes or ((Rd and $1) shl 5);
  3385. bytes:=bytes or (Rm shl 12);
  3386. bytes:=bytes or (Rn shl 16);
  3387. end
  3388. else if ops=3 then
  3389. begin
  3390. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3391. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3392. begin
  3393. Rd:=getmmreg(oper[0]^.reg);
  3394. Rm:=getsupreg(oper[1]^.reg);
  3395. Rn:=getsupreg(oper[2]^.reg);
  3396. end
  3397. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3398. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3399. begin
  3400. Rm:=getsupreg(oper[0]^.reg);
  3401. Rn:=getsupreg(oper[1]^.reg);
  3402. Rd:=getmmreg(oper[2]^.reg);
  3403. end
  3404. else
  3405. message(asmw_e_invalid_opcode_and_operands);
  3406. bytes:=bytes or ((Rd and $F) shl 0);
  3407. bytes:=bytes or ((Rd and $10) shl 1);
  3408. bytes:=bytes or (Rm shl 12);
  3409. bytes:=bytes or (Rn shl 16);
  3410. end
  3411. else if ops=2 then
  3412. begin
  3413. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3414. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3415. begin
  3416. Rd:=getmmreg(oper[0]^.reg);
  3417. Rm:=getsupreg(oper[1]^.reg);
  3418. end
  3419. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3420. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3421. begin
  3422. Rm:=getsupreg(oper[0]^.reg);
  3423. Rd:=getmmreg(oper[1]^.reg);
  3424. end
  3425. else
  3426. message(asmw_e_invalid_opcode_and_operands);
  3427. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3428. bytes:=bytes or ((Rd and $1) shl 7);
  3429. bytes:=bytes or (Rm shl 12);
  3430. end;
  3431. end;
  3432. PF_F32:
  3433. begin
  3434. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3435. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3436. Message(asmw_e_invalid_opcode_and_operands);
  3437. Rd:=getmmreg(oper[0]^.reg);
  3438. Rm:=getmmreg(oper[1]^.reg);
  3439. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3440. bytes:=bytes or ((Rd and $1) shl 22);
  3441. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3442. bytes:=bytes or ((Rm and $1) shl 5);
  3443. end;
  3444. PF_F64:
  3445. begin
  3446. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3447. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3448. Message(asmw_e_invalid_opcode_and_operands);
  3449. Rd:=getmmreg(oper[0]^.reg);
  3450. Rm:=getmmreg(oper[1]^.reg);
  3451. bytes:=bytes or (1 shl 8);
  3452. bytes:=bytes or ((Rd and $F) shl 12);
  3453. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3454. bytes:=bytes or (Rm and $F);
  3455. bytes:=bytes or ((Rm and $10) shl 1);
  3456. end;
  3457. end;
  3458. end;
  3459. #$41,#$91: // VMRS/VMSR
  3460. begin
  3461. { set instruction code }
  3462. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3463. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3464. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3465. bytes:=bytes or ord(insentry^.code[4]);
  3466. { set regs }
  3467. if (opcode=A_VMRS) or
  3468. (opcode=A_FMRX) then
  3469. begin
  3470. case oper[1]^.reg of
  3471. NR_FPSID: Rn:=$0;
  3472. NR_FPSCR: Rn:=$1;
  3473. NR_MVFR1: Rn:=$6;
  3474. NR_MVFR0: Rn:=$7;
  3475. NR_FPEXC: Rn:=$8;
  3476. else
  3477. Rn:=0;
  3478. message(asmw_e_invalid_opcode_and_operands);
  3479. end;
  3480. bytes:=bytes or (Rn shl 16);
  3481. if oper[0]^.reg=NR_APSR_nzcv then
  3482. bytes:=bytes or ($F shl 12)
  3483. else
  3484. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3485. end
  3486. else
  3487. begin
  3488. case oper[0]^.reg of
  3489. NR_FPSID: Rn:=$0;
  3490. NR_FPSCR: Rn:=$1;
  3491. NR_FPEXC: Rn:=$8;
  3492. else
  3493. Rn:=0;
  3494. message(asmw_e_invalid_opcode_and_operands);
  3495. end;
  3496. bytes:=bytes or (Rn shl 16);
  3497. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3498. end;
  3499. end;
  3500. #$42,#$92: // VMUL
  3501. begin
  3502. { set instruction code }
  3503. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3504. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3505. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3506. bytes:=bytes or ord(insentry^.code[4]);
  3507. { set regs }
  3508. if ops=3 then
  3509. begin
  3510. Rd:=getmmreg(oper[0]^.reg);
  3511. Rn:=getmmreg(oper[1]^.reg);
  3512. Rm:=getmmreg(oper[2]^.reg);
  3513. end
  3514. else if ops=1 then
  3515. begin
  3516. Rd:=getmmreg(oper[0]^.reg);
  3517. Rn:=0;
  3518. Rm:=0;
  3519. end
  3520. else if oper[1]^.typ=top_const then
  3521. begin
  3522. Rd:=getmmreg(oper[0]^.reg);
  3523. Rn:=0;
  3524. Rm:=0;
  3525. end
  3526. else
  3527. begin
  3528. Rd:=getmmreg(oper[0]^.reg);
  3529. Rn:=0;
  3530. Rm:=getmmreg(oper[1]^.reg);
  3531. end;
  3532. if (oppostfix=PF_F32) or (insentry^.code[5]=#1) then
  3533. begin
  3534. D:=rd and $1; Rd:=Rd shr 1;
  3535. N:=rn and $1; Rn:=Rn shr 1;
  3536. M:=rm and $1; Rm:=Rm shr 1;
  3537. end
  3538. else
  3539. begin
  3540. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3541. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3542. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3543. bytes:=bytes or (1 shl 8);
  3544. end;
  3545. bytes:=bytes or (Rd shl 12);
  3546. bytes:=bytes or (Rn shl 16);
  3547. bytes:=bytes or (Rm shl 0);
  3548. bytes:=bytes or (D shl 22);
  3549. bytes:=bytes or (N shl 7);
  3550. bytes:=bytes or (M shl 5);
  3551. end;
  3552. #$43,#$93: // VCVT
  3553. begin
  3554. { set instruction code }
  3555. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3556. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3557. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3558. bytes:=bytes or ord(insentry^.code[4]);
  3559. { set regs }
  3560. Rd:=getmmreg(oper[0]^.reg);
  3561. Rm:=getmmreg(oper[1]^.reg);
  3562. if (ops=2) and
  3563. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3564. begin
  3565. if oppostfix=PF_F32F64 then
  3566. begin
  3567. bytes:=bytes or (1 shl 8);
  3568. D:=rd and $1; Rd:=Rd shr 1;
  3569. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3570. end
  3571. else
  3572. begin
  3573. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3574. M:=rm and $1; Rm:=Rm shr 1;
  3575. end;
  3576. bytes:=bytes and $FFF0FFFF;
  3577. bytes:=bytes or ($7 shl 16);
  3578. bytes:=bytes or (Rd shl 12);
  3579. bytes:=bytes or (Rm shl 0);
  3580. bytes:=bytes or (D shl 22);
  3581. bytes:=bytes or (M shl 5);
  3582. end
  3583. else if (ops=2) and
  3584. (oppostfix=PF_None) then
  3585. begin
  3586. d:=0;
  3587. case getsubreg(oper[0]^.reg) of
  3588. R_SUBNONE:
  3589. rd:=getsupreg(oper[0]^.reg);
  3590. R_SUBFS:
  3591. begin
  3592. rd:=getmmreg(oper[0]^.reg);
  3593. d:=rd and 1;
  3594. rd:=rd shr 1;
  3595. end;
  3596. R_SUBFD:
  3597. begin
  3598. rd:=getmmreg(oper[0]^.reg);
  3599. d:=(rd shr 4) and 1;
  3600. rd:=rd and $F;
  3601. end;
  3602. end;
  3603. m:=0;
  3604. case getsubreg(oper[1]^.reg) of
  3605. R_SUBNONE:
  3606. rm:=getsupreg(oper[1]^.reg);
  3607. R_SUBFS:
  3608. begin
  3609. rm:=getmmreg(oper[1]^.reg);
  3610. m:=rm and 1;
  3611. rm:=rm shr 1;
  3612. end;
  3613. R_SUBFD:
  3614. begin
  3615. rm:=getmmreg(oper[1]^.reg);
  3616. m:=(rm shr 4) and 1;
  3617. rm:=rm and $F;
  3618. end;
  3619. end;
  3620. bytes:=bytes or (Rd shl 12);
  3621. bytes:=bytes or (Rm shl 0);
  3622. bytes:=bytes or (D shl 22);
  3623. bytes:=bytes or (M shl 5);
  3624. end
  3625. else if ops=2 then
  3626. begin
  3627. case oppostfix of
  3628. PF_S32F64,
  3629. PF_U32F64,
  3630. PF_F64S32,
  3631. PF_F64U32:
  3632. bytes:=bytes or (1 shl 8);
  3633. end;
  3634. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3635. begin
  3636. case oppostfix of
  3637. PF_S32F64,
  3638. PF_S32F32:
  3639. bytes:=bytes or (1 shl 16);
  3640. end;
  3641. bytes:=bytes or (1 shl 18);
  3642. D:=rd and $1; Rd:=Rd shr 1;
  3643. if oppostfix in [PF_S32F64,PF_U32F64] then
  3644. begin
  3645. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3646. end
  3647. else
  3648. begin
  3649. M:=rm and $1; Rm:=Rm shr 1;
  3650. end;
  3651. end
  3652. else
  3653. begin
  3654. case oppostfix of
  3655. PF_F64S32,
  3656. PF_F32S32:
  3657. bytes:=bytes or (1 shl 7);
  3658. else
  3659. bytes:=bytes and $FFFFFF7F;
  3660. end;
  3661. M:=rm and $1; Rm:=Rm shr 1;
  3662. if oppostfix in [PF_F64S32,PF_F64U32] then
  3663. begin
  3664. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3665. end
  3666. else
  3667. begin
  3668. D:=rd and $1; Rd:=Rd shr 1;
  3669. end
  3670. end;
  3671. bytes:=bytes or (Rd shl 12);
  3672. bytes:=bytes or (Rm shl 0);
  3673. bytes:=bytes or (D shl 22);
  3674. bytes:=bytes or (M shl 5);
  3675. end
  3676. else
  3677. begin
  3678. if rd<>rm then
  3679. message(asmw_e_invalid_opcode_and_operands);
  3680. case oppostfix of
  3681. PF_S32F32,PF_U32F32,
  3682. PF_F32S32,PF_F32U32,
  3683. PF_S32F64,PF_U32F64,
  3684. PF_F64S32,PF_F64U32:
  3685. begin
  3686. if not (oper[2]^.val in [1..32]) then
  3687. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3688. bytes:=bytes or (1 shl 7);
  3689. rn:=32;
  3690. end;
  3691. PF_S16F64,PF_U16F64,
  3692. PF_F64S16,PF_F64U16,
  3693. PF_S16F32,PF_U16F32,
  3694. PF_F32S16,PF_F32U16:
  3695. begin
  3696. if not (oper[2]^.val in [0..16]) then
  3697. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3698. rn:=16;
  3699. end;
  3700. else
  3701. Rn:=0;
  3702. message(asmw_e_invalid_opcode_and_operands);
  3703. end;
  3704. case oppostfix of
  3705. PF_S16F64,PF_U16F64,
  3706. PF_S32F64,PF_U32F64,
  3707. PF_F64S16,PF_F64U16,
  3708. PF_F64S32,PF_F64U32:
  3709. begin
  3710. bytes:=bytes or (1 shl 8);
  3711. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3712. end;
  3713. else
  3714. begin
  3715. D:=rd and $1; Rd:=Rd shr 1;
  3716. end;
  3717. end;
  3718. case oppostfix of
  3719. PF_U16F64,PF_U16F32,
  3720. PF_U32F32,PF_U32F64,
  3721. PF_F64U16,PF_F32U16,
  3722. PF_F32U32,PF_F64U32:
  3723. bytes:=bytes or (1 shl 16);
  3724. end;
  3725. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3726. bytes:=bytes or (1 shl 18);
  3727. bytes:=bytes or (Rd shl 12);
  3728. bytes:=bytes or (D shl 22);
  3729. rn:=rn-oper[2]^.val;
  3730. bytes:=bytes or ((rn and $1) shl 5);
  3731. bytes:=bytes or ((rn and $1E) shr 1);
  3732. end;
  3733. end;
  3734. #$44,#$94: // VLDM/VSTM/VPUSH/VPOP
  3735. begin
  3736. { set instruction code }
  3737. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3738. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3739. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3740. { set regs }
  3741. if ops=2 then
  3742. begin
  3743. if oper[0]^.typ=top_ref then
  3744. begin
  3745. Rn:=getsupreg(oper[0]^.ref^.index);
  3746. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3747. begin
  3748. { set W }
  3749. bytes:=bytes or (1 shl 21);
  3750. end
  3751. else if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
  3752. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3753. end
  3754. else
  3755. begin
  3756. Rn:=getsupreg(oper[0]^.reg);
  3757. if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
  3758. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3759. end;
  3760. bytes:=bytes or (Rn shl 16);
  3761. { Set PU bits }
  3762. case oppostfix of
  3763. PF_None,
  3764. PF_IA,PF_IAS,PF_IAD,PF_IAX:
  3765. bytes:=bytes or (1 shl 23);
  3766. PF_DB,PF_DBS,PF_DBD,PF_DBX:
  3767. bytes:=bytes or (2 shl 23);
  3768. end;
  3769. case oppostfix of
  3770. PF_IAX,PF_DBX,PF_FDX,PF_EAX:
  3771. begin
  3772. bytes:=bytes or (1 shl 8);
  3773. bytes:=bytes or (1 shl 0); // Offset is odd
  3774. end;
  3775. end;
  3776. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3777. if oper[1]^.regset^=[] then
  3778. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3779. rd:=0;
  3780. for r:=0 to 31 do
  3781. if r in oper[1]^.regset^ then
  3782. begin
  3783. rd:=r;
  3784. break;
  3785. end;
  3786. rn:=32-rd;
  3787. for r:=rd+1 to 31 do
  3788. if not(r in oper[1]^.regset^) then
  3789. begin
  3790. rn:=r-rd;
  3791. break;
  3792. end;
  3793. if dp_operation then
  3794. begin
  3795. bytes:=bytes or (1 shl 8);
  3796. bytes:=bytes or (rn*2);
  3797. bytes:=bytes or ((rd and $F) shl 12);
  3798. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3799. end
  3800. else
  3801. begin
  3802. bytes:=bytes or rn;
  3803. bytes:=bytes or ((rd and $1) shl 22);
  3804. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3805. end;
  3806. end
  3807. else { VPUSH/VPOP }
  3808. begin
  3809. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3810. if oper[0]^.regset^=[] then
  3811. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3812. rd:=0;
  3813. for r:=0 to 31 do
  3814. if r in oper[0]^.regset^ then
  3815. begin
  3816. rd:=r;
  3817. break;
  3818. end;
  3819. rn:=32-rd;
  3820. for r:=rd+1 to 31 do
  3821. if not(r in oper[0]^.regset^) then
  3822. begin
  3823. rn:=r-rd;
  3824. break;
  3825. end;
  3826. if dp_operation then
  3827. begin
  3828. bytes:=bytes or (1 shl 8);
  3829. bytes:=bytes or (rn*2);
  3830. bytes:=bytes or ((rd and $F) shl 12);
  3831. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3832. end
  3833. else
  3834. begin
  3835. bytes:=bytes or rn;
  3836. bytes:=bytes or ((rd and $1) shl 22);
  3837. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3838. end;
  3839. end;
  3840. end;
  3841. #$45,#$95: // VLDR/VSTR
  3842. begin
  3843. { set instruction code }
  3844. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3845. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3846. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3847. { set regs }
  3848. rd:=getmmreg(oper[0]^.reg);
  3849. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3850. begin
  3851. bytes:=bytes or (1 shl 8);
  3852. bytes:=bytes or ((rd and $F) shl 12);
  3853. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3854. end
  3855. else
  3856. begin
  3857. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3858. bytes:=bytes or ((rd and $1) shl 22);
  3859. end;
  3860. { set ref }
  3861. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3862. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3863. begin
  3864. { set offset }
  3865. offset:=0;
  3866. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3867. if assigned(currsym) then
  3868. offset:=currsym.offset-insoffset-8;
  3869. offset:=offset+oper[1]^.ref^.offset;
  3870. offset:=offset div 4;
  3871. if offset>=0 then
  3872. begin
  3873. { set U flag }
  3874. bytes:=bytes or (1 shl 23);
  3875. bytes:=bytes or offset
  3876. end
  3877. else
  3878. begin
  3879. offset:=-offset;
  3880. bytes:=bytes or offset
  3881. end;
  3882. end
  3883. else
  3884. message(asmw_e_invalid_opcode_and_operands);
  3885. end;
  3886. #$46: { System instructions }
  3887. begin
  3888. { set instruction code }
  3889. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3890. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3891. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3892. { set regs }
  3893. if (oper[0]^.typ=top_modeflags) then
  3894. begin
  3895. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 8);
  3896. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  3897. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  3898. end;
  3899. if (ops=2) then
  3900. bytes:=bytes or (oper[1]^.val and $1F)
  3901. else if (ops=1) and
  3902. (oper[0]^.typ=top_const) then
  3903. bytes:=bytes or (oper[0]^.val and $1F);
  3904. end;
  3905. #$60: { Thumb }
  3906. begin
  3907. bytelen:=2;
  3908. bytes:=0;
  3909. { set opcode }
  3910. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3911. bytes:=bytes or ord(insentry^.code[2]);
  3912. { set regs }
  3913. if ops=2 then
  3914. begin
  3915. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3916. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3917. if (oper[1]^.typ=top_reg) then
  3918. bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
  3919. else
  3920. bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
  3921. end
  3922. else if ops=3 then
  3923. begin
  3924. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3925. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3926. if (oper[2]^.typ=top_reg) then
  3927. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
  3928. else
  3929. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  3930. end
  3931. else if ops=1 then
  3932. begin
  3933. if oper[0]^.typ=top_const then
  3934. bytes:=bytes or (oper[0]^.val and $FF);
  3935. end;
  3936. end;
  3937. #$61: { Thumb }
  3938. begin
  3939. bytelen:=2;
  3940. bytes:=0;
  3941. { set opcode }
  3942. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3943. bytes:=bytes or ord(insentry^.code[2]);
  3944. { set regs }
  3945. if ops=2 then
  3946. begin
  3947. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3948. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3949. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3950. end
  3951. else if ops=1 then
  3952. begin
  3953. if oper[0]^.typ=top_const then
  3954. bytes:=bytes or (oper[0]^.val and $FF);
  3955. end;
  3956. end;
  3957. #$62..#$63: { Thumb branches }
  3958. begin
  3959. bytelen:=2;
  3960. bytes:=0;
  3961. { set opcode }
  3962. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3963. bytes:=bytes or ord(insentry^.code[2]);
  3964. if insentry^.code[0]=#$63 then
  3965. bytes:=bytes or (CondVal[condition] shl 8);
  3966. if oper[0]^.typ=top_const then
  3967. begin
  3968. if insentry^.code[0]=#$63 then
  3969. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  3970. else
  3971. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  3972. end
  3973. else if oper[0]^.typ=top_reg then
  3974. begin
  3975. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3976. end
  3977. else if oper[0]^.typ=top_ref then
  3978. begin
  3979. offset:=0;
  3980. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3981. if assigned(currsym) then
  3982. offset:=currsym.offset-insoffset-8;
  3983. offset:=offset+oper[0]^.ref^.offset;
  3984. if insentry^.code[0]=#$63 then
  3985. bytes:=bytes or (((offset+4) shr 1) and $FF)
  3986. else
  3987. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  3988. end
  3989. end;
  3990. #$64: { Thumb: Special encodings }
  3991. begin
  3992. bytelen:=2;
  3993. bytes:=0;
  3994. { set opcode }
  3995. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3996. bytes:=bytes or ord(insentry^.code[2]);
  3997. case opcode of
  3998. A_SUB:
  3999. begin
  4000. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4001. if (ops=3) and
  4002. (oper[2]^.typ=top_const) then
  4003. bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
  4004. else if (ops=2) and
  4005. (oper[1]^.typ=top_const) then
  4006. bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
  4007. end;
  4008. A_MUL:
  4009. if (ops in [2,3]) then
  4010. begin
  4011. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4012. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4013. end;
  4014. A_ADD:
  4015. begin
  4016. if ops=2 then
  4017. begin
  4018. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4019. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  4020. end
  4021. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  4022. (oper[2]^.typ=top_const) then
  4023. begin
  4024. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  4025. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  4026. end
  4027. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  4028. (oper[2]^.typ=top_reg) then
  4029. begin
  4030. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4031. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  4032. end
  4033. else
  4034. begin
  4035. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4036. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  4037. end;
  4038. end;
  4039. end;
  4040. end;
  4041. #$65: { Thumb load/store }
  4042. begin
  4043. bytelen:=2;
  4044. bytes:=0;
  4045. { set opcode }
  4046. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4047. bytes:=bytes or ord(insentry^.code[2]);
  4048. { set regs }
  4049. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4050. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  4051. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  4052. end;
  4053. #$66: { Thumb load/store }
  4054. begin
  4055. bytelen:=2;
  4056. bytes:=0;
  4057. { set opcode }
  4058. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4059. bytes:=bytes or ord(insentry^.code[2]);
  4060. { set regs }
  4061. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4062. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  4063. { set offset }
  4064. offset:=0;
  4065. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4066. if assigned(currsym) then
  4067. offset:=currsym.offset-(insoffset+4) and (not longword(3));
  4068. offset:=(offset+oper[1]^.ref^.offset);
  4069. bytes:=bytes or (((offset shr ord(insentry^.code[3])) and $1F) shl 6);
  4070. end;
  4071. #$67: { Thumb load/store }
  4072. begin
  4073. bytelen:=2;
  4074. bytes:=0;
  4075. { set opcode }
  4076. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4077. bytes:=bytes or ord(insentry^.code[2]);
  4078. { set regs }
  4079. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4080. if oper[1]^.typ=top_ref then
  4081. begin
  4082. { set offset }
  4083. offset:=0;
  4084. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4085. if assigned(currsym) then
  4086. offset:=currsym.offset-(insoffset+4) and (not longword(3));
  4087. offset:=(offset+oper[1]^.ref^.offset);
  4088. bytes:=bytes or ((offset shr ord(insentry^.code[3])) and $FF);
  4089. end
  4090. else
  4091. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  4092. end;
  4093. #$68: { Thumb CB[N]Z }
  4094. begin
  4095. bytelen:=2;
  4096. bytes:=0;
  4097. { set opcode }
  4098. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4099. { set opers }
  4100. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4101. if oper[1]^.typ=top_ref then
  4102. begin
  4103. offset:=0;
  4104. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4105. if assigned(currsym) then
  4106. offset:=currsym.offset-insoffset-8;
  4107. offset:=offset+oper[1]^.ref^.offset;
  4108. offset:=offset div 2;
  4109. end
  4110. else
  4111. offset:=oper[1]^.val div 2;
  4112. bytes:=bytes or ((offset) and $1F) shl 3;
  4113. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  4114. end;
  4115. #$69: { Thumb: Push/Pop/Stm/Ldm }
  4116. begin
  4117. bytelen:=2;
  4118. bytes:=0;
  4119. { set opcode }
  4120. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4121. case opcode of
  4122. A_PUSH:
  4123. begin
  4124. for r:=0 to 7 do
  4125. if r in oper[0]^.regset^ then
  4126. bytes:=bytes or (1 shl r);
  4127. if RS_R14 in oper[0]^.regset^ then
  4128. bytes:=bytes or (1 shl 8);
  4129. end;
  4130. A_POP:
  4131. begin
  4132. for r:=0 to 7 do
  4133. if r in oper[0]^.regset^ then
  4134. bytes:=bytes or (1 shl r);
  4135. if RS_R15 in oper[0]^.regset^ then
  4136. bytes:=bytes or (1 shl 8);
  4137. end;
  4138. A_STM:
  4139. begin
  4140. for r:=0 to 7 do
  4141. if r in oper[1]^.regset^ then
  4142. bytes:=bytes or (1 shl r);
  4143. if oper[0]^.typ=top_ref then
  4144. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4145. else
  4146. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4147. end;
  4148. A_LDM:
  4149. begin
  4150. for r:=0 to 7 do
  4151. if r in oper[1]^.regset^ then
  4152. bytes:=bytes or (1 shl r);
  4153. if oper[0]^.typ=top_ref then
  4154. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4155. else
  4156. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4157. end;
  4158. end;
  4159. end;
  4160. #$6A: { Thumb: IT }
  4161. begin
  4162. bytelen:=2;
  4163. bytes:=0;
  4164. { set opcode }
  4165. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4166. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  4167. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  4168. i_field:=(bytes shr 4) and 1;
  4169. i_field:=(i_field shl 1) or i_field;
  4170. i_field:=(i_field shl 2) or i_field;
  4171. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  4172. end;
  4173. #$6B: { Thumb: Data processing (misc) }
  4174. begin
  4175. bytelen:=2;
  4176. bytes:=0;
  4177. { set opcode }
  4178. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4179. bytes:=bytes or ord(insentry^.code[2]);
  4180. { set regs }
  4181. if ops>=2 then
  4182. begin
  4183. if oper[1]^.typ=top_const then
  4184. begin
  4185. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  4186. bytes:=bytes or (oper[1]^.val and $FF);
  4187. end
  4188. else if oper[1]^.typ=top_reg then
  4189. begin
  4190. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4191. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4192. end;
  4193. end
  4194. else if ops=1 then
  4195. begin
  4196. if oper[0]^.typ=top_const then
  4197. bytes:=bytes or (oper[0]^.val and $FF);
  4198. end;
  4199. end;
  4200. #$6C: { Thumb: CPS }
  4201. begin
  4202. bytelen:=2;
  4203. bytes:=0;
  4204. { set opcode }
  4205. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4206. bytes:=bytes or ord(insentry^.code[2]);
  4207. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 2);
  4208. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 1);
  4209. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 0);
  4210. end;
  4211. #$80: { Thumb-2: Dataprocessing }
  4212. begin
  4213. bytes:=0;
  4214. { set instruction code }
  4215. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4216. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4217. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4218. bytes:=bytes or ord(insentry^.code[4]);
  4219. if ops=1 then
  4220. begin
  4221. if oper[0]^.typ=top_reg then
  4222. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4223. else if oper[0]^.typ=top_const then
  4224. bytes:=bytes or (oper[0]^.val and $F);
  4225. end
  4226. else if (ops=2) and
  4227. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4228. begin
  4229. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4230. if oper[1]^.typ=top_const then
  4231. encodethumbimm(oper[1]^.val)
  4232. else if oper[1]^.typ=top_reg then
  4233. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4234. end
  4235. else if (ops=3) and
  4236. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4237. begin
  4238. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4239. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4240. if oper[2]^.typ=top_shifterop then
  4241. setthumbshift(2)
  4242. else if oper[2]^.typ=top_reg then
  4243. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  4244. end
  4245. else if (ops=2) and
  4246. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  4247. begin
  4248. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4249. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4250. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4251. end
  4252. else if ops=2 then
  4253. begin
  4254. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4255. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4256. if oper[1]^.typ=top_const then
  4257. encodethumbimm(oper[1]^.val)
  4258. else if oper[1]^.typ=top_reg then
  4259. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4260. end
  4261. else if ops=3 then
  4262. begin
  4263. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4264. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4265. if oper[2]^.typ=top_const then
  4266. encodethumbimm(oper[2]^.val)
  4267. else if oper[2]^.typ=top_reg then
  4268. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4269. end
  4270. else if ops=4 then
  4271. begin
  4272. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4273. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4274. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4275. if oper[3]^.typ=top_shifterop then
  4276. setthumbshift(3)
  4277. else if oper[3]^.typ=top_reg then
  4278. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4279. end;
  4280. if oppostfix=PF_S then
  4281. bytes:=bytes or (1 shl 20)
  4282. else if oppostfix=PF_X then
  4283. bytes:=bytes or (1 shl 4)
  4284. else if oppostfix=PF_R then
  4285. bytes:=bytes or (1 shl 4);
  4286. end;
  4287. #$81: { Thumb-2: Dataprocessing misc }
  4288. begin
  4289. bytes:=0;
  4290. { set instruction code }
  4291. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4292. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4293. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4294. bytes:=bytes or ord(insentry^.code[4]);
  4295. if ops=3 then
  4296. begin
  4297. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4298. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4299. if oper[2]^.typ=top_const then
  4300. begin
  4301. bytes:=bytes or (oper[2]^.val and $FF);
  4302. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4303. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4304. end;
  4305. end
  4306. else if ops=2 then
  4307. begin
  4308. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4309. offset:=0;
  4310. if oper[1]^.typ=top_const then
  4311. begin
  4312. offset:=oper[1]^.val;
  4313. end
  4314. else if oper[1]^.typ=top_ref then
  4315. begin
  4316. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4317. if assigned(currsym) then
  4318. offset:=currsym.offset-insoffset-8;
  4319. offset:=offset+oper[1]^.ref^.offset;
  4320. offset:=offset;
  4321. end;
  4322. bytes:=bytes or (offset and $FF);
  4323. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4324. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4325. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4326. end;
  4327. if oppostfix=PF_S then
  4328. bytes:=bytes or (1 shl 20);
  4329. end;
  4330. #$82: { Thumb-2: Shifts }
  4331. begin
  4332. bytes:=0;
  4333. { set instruction code }
  4334. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4335. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4336. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4337. bytes:=bytes or ord(insentry^.code[4]);
  4338. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4339. if oper[1]^.typ=top_reg then
  4340. begin
  4341. offset:=2;
  4342. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4343. end
  4344. else
  4345. begin
  4346. offset:=1;
  4347. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4348. end;
  4349. if oper[offset]^.typ=top_const then
  4350. begin
  4351. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4352. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4353. end
  4354. else if oper[offset]^.typ=top_reg then
  4355. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4356. if (ops>=(offset+2)) and
  4357. (oper[offset+1]^.typ=top_const) then
  4358. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4359. if oppostfix=PF_S then
  4360. bytes:=bytes or (1 shl 20);
  4361. end;
  4362. #$84: { Thumb-2: Shifts(width-1) }
  4363. begin
  4364. bytes:=0;
  4365. { set instruction code }
  4366. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4367. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4368. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4369. bytes:=bytes or ord(insentry^.code[4]);
  4370. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4371. if oper[1]^.typ=top_reg then
  4372. begin
  4373. offset:=2;
  4374. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4375. end
  4376. else
  4377. offset:=1;
  4378. if oper[offset]^.typ=top_const then
  4379. begin
  4380. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4381. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4382. end;
  4383. if (ops>=(offset+2)) and
  4384. (oper[offset+1]^.typ=top_const) then
  4385. begin
  4386. if opcode in [A_BFI,A_BFC] then
  4387. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4388. else
  4389. i_field:=oper[offset+1]^.val-1;
  4390. bytes:=bytes or (i_field and $1F);
  4391. end;
  4392. if oppostfix=PF_S then
  4393. bytes:=bytes or (1 shl 20);
  4394. end;
  4395. #$83: { Thumb-2: Saturation }
  4396. begin
  4397. bytes:=0;
  4398. { set instruction code }
  4399. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4400. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4401. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4402. bytes:=bytes or ord(insentry^.code[4]);
  4403. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4404. bytes:=bytes or (oper[1]^.val and $1F);
  4405. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4406. if ops=4 then
  4407. setthumbshift(3,true);
  4408. end;
  4409. #$85: { Thumb-2: Long multiplications }
  4410. begin
  4411. bytes:=0;
  4412. { set instruction code }
  4413. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4414. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4415. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4416. bytes:=bytes or ord(insentry^.code[4]);
  4417. if ops=4 then
  4418. begin
  4419. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4420. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4421. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4422. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4423. end;
  4424. if oppostfix=PF_S then
  4425. bytes:=bytes or (1 shl 20)
  4426. else if oppostfix=PF_X then
  4427. bytes:=bytes or (1 shl 4);
  4428. end;
  4429. #$86: { Thumb-2: Extension ops }
  4430. begin
  4431. bytes:=0;
  4432. { set instruction code }
  4433. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4434. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4435. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4436. bytes:=bytes or ord(insentry^.code[4]);
  4437. if ops=2 then
  4438. begin
  4439. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4440. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4441. end
  4442. else if ops=3 then
  4443. begin
  4444. if oper[2]^.typ=top_shifterop then
  4445. begin
  4446. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4447. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4448. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4449. end
  4450. else
  4451. begin
  4452. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4453. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4454. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4455. end;
  4456. end
  4457. else if ops=4 then
  4458. begin
  4459. if oper[3]^.typ=top_shifterop then
  4460. begin
  4461. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4462. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4463. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4464. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4465. end;
  4466. end;
  4467. end;
  4468. #$87: { Thumb-2: PLD/PLI }
  4469. begin
  4470. { set instruction code }
  4471. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4472. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4473. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4474. bytes:=bytes or ord(insentry^.code[4]);
  4475. { set Rn and Rd }
  4476. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4477. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4478. begin
  4479. { set offset }
  4480. offset:=0;
  4481. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4482. if assigned(currsym) then
  4483. offset:=currsym.offset-insoffset-8;
  4484. offset:=offset+oper[0]^.ref^.offset;
  4485. if offset>=0 then
  4486. begin
  4487. { set U flag }
  4488. bytes:=bytes or (1 shl 23);
  4489. bytes:=bytes or (offset and $FFF);
  4490. end
  4491. else
  4492. begin
  4493. bytes:=bytes or ($3 shl 10);
  4494. offset:=-offset;
  4495. bytes:=bytes or (offset and $FF);
  4496. end;
  4497. end
  4498. else
  4499. begin
  4500. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4501. { set shift }
  4502. with oper[0]^.ref^ do
  4503. if shiftmode=SM_LSL then
  4504. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4505. end;
  4506. end;
  4507. #$88: { Thumb-2: LDR/STR }
  4508. begin
  4509. { set instruction code }
  4510. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4511. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4512. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4513. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4514. { set Rn and Rd }
  4515. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4516. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4517. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4518. begin
  4519. { set offset }
  4520. offset:=0;
  4521. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4522. if assigned(currsym) then
  4523. offset:=currsym.offset-insoffset-8;
  4524. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4525. if offset>=0 then
  4526. begin
  4527. if (offset>255) and
  4528. (not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT])) then
  4529. bytes:=bytes or (1 shl 23);
  4530. { set U flag }
  4531. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4532. begin
  4533. bytes:=bytes or (1 shl 9);
  4534. bytes:=bytes or (1 shl 11);
  4535. end;
  4536. bytes:=bytes or offset
  4537. end
  4538. else
  4539. begin
  4540. bytes:=bytes or (1 shl 11);
  4541. offset:=-offset;
  4542. bytes:=bytes or offset
  4543. end;
  4544. end
  4545. else
  4546. begin
  4547. { set I flag }
  4548. bytes:=bytes or (1 shl 25);
  4549. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4550. { set shift }
  4551. with oper[1]^.ref^ do
  4552. if shiftmode<>SM_None then
  4553. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4554. end;
  4555. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4556. begin
  4557. { set W bit }
  4558. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4559. bytes:=bytes or (1 shl 8);
  4560. { set P bit if necessary }
  4561. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4562. bytes:=bytes or (1 shl 10);
  4563. end;
  4564. end;
  4565. #$89: { Thumb-2: LDRD/STRD }
  4566. begin
  4567. { set instruction code }
  4568. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4569. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4570. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4571. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4572. { set Rn and Rd }
  4573. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4574. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4575. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4576. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4577. begin
  4578. { set offset }
  4579. offset:=0;
  4580. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4581. if assigned(currsym) then
  4582. offset:=currsym.offset-insoffset-8;
  4583. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4584. if offset>=0 then
  4585. begin
  4586. { set U flag }
  4587. bytes:=bytes or (1 shl 23);
  4588. bytes:=bytes or offset
  4589. end
  4590. else
  4591. begin
  4592. offset:=-offset;
  4593. bytes:=bytes or offset
  4594. end;
  4595. end
  4596. else
  4597. begin
  4598. message(asmw_e_invalid_opcode_and_operands);
  4599. end;
  4600. { set W bit }
  4601. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4602. bytes:=bytes or (1 shl 21);
  4603. { set P bit if necessary }
  4604. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4605. bytes:=bytes or (1 shl 24);
  4606. end;
  4607. #$8A: { Thumb-2: LDREX }
  4608. begin
  4609. { set instruction code }
  4610. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4611. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4612. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4613. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4614. { set Rn and Rd }
  4615. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4616. if (ops=2) and (opcode in [A_LDREX]) then
  4617. begin
  4618. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4619. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4620. begin
  4621. { set offset }
  4622. offset:=0;
  4623. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4624. if assigned(currsym) then
  4625. offset:=currsym.offset-insoffset-8;
  4626. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4627. if offset>=0 then
  4628. begin
  4629. bytes:=bytes or offset
  4630. end
  4631. else
  4632. begin
  4633. message(asmw_e_invalid_opcode_and_operands);
  4634. end;
  4635. end
  4636. else
  4637. begin
  4638. message(asmw_e_invalid_opcode_and_operands);
  4639. end;
  4640. end
  4641. else if (ops=2) then
  4642. begin
  4643. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4644. end
  4645. else
  4646. begin
  4647. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4648. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4649. end;
  4650. end;
  4651. #$8B: { Thumb-2: STREX }
  4652. begin
  4653. { set instruction code }
  4654. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4655. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4656. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4657. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4658. { set Rn and Rd }
  4659. if (ops=3) and (opcode in [A_STREX]) then
  4660. begin
  4661. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4662. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4663. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4664. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4665. begin
  4666. { set offset }
  4667. offset:=0;
  4668. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4669. if assigned(currsym) then
  4670. offset:=currsym.offset-insoffset-8;
  4671. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4672. if offset>=0 then
  4673. begin
  4674. bytes:=bytes or offset
  4675. end
  4676. else
  4677. begin
  4678. message(asmw_e_invalid_opcode_and_operands);
  4679. end;
  4680. end
  4681. else
  4682. begin
  4683. message(asmw_e_invalid_opcode_and_operands);
  4684. end;
  4685. end
  4686. else if (ops=3) then
  4687. begin
  4688. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4689. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4690. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4691. end
  4692. else
  4693. begin
  4694. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4695. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4696. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  4697. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  4698. end;
  4699. end;
  4700. #$8C: { Thumb-2: LDM/STM }
  4701. begin
  4702. { set instruction code }
  4703. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4704. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4705. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4706. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4707. if oper[0]^.typ=top_reg then
  4708. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4709. else
  4710. begin
  4711. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  4712. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4713. bytes:=bytes or (1 shl 21);
  4714. end;
  4715. for r:=0 to 15 do
  4716. if r in oper[1]^.regset^ then
  4717. bytes:=bytes or (1 shl r);
  4718. case oppostfix of
  4719. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  4720. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  4721. end;
  4722. end;
  4723. #$8D: { Thumb-2: BL/BLX }
  4724. begin
  4725. { set instruction code }
  4726. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4727. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  4728. { set offset }
  4729. if oper[0]^.typ=top_const then
  4730. offset:=(oper[0]^.val shr 1) and $FFFFFF
  4731. else
  4732. begin
  4733. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4734. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  4735. begin
  4736. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  4737. offset:=$FFFFFE
  4738. end
  4739. else
  4740. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  4741. end;
  4742. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  4743. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  4744. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  4745. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  4746. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  4747. end;
  4748. #$8E: { Thumb-2: TBB/TBH }
  4749. begin
  4750. { set instruction code }
  4751. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4752. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4753. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4754. bytes:=bytes or ord(insentry^.code[4]);
  4755. { set Rn and Rm }
  4756. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4757. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4758. message(asmw_e_invalid_effective_address)
  4759. else
  4760. begin
  4761. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4762. if (opcode=A_TBH) and
  4763. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  4764. (oper[0]^.ref^.shiftimm<>1) then
  4765. message(asmw_e_invalid_effective_address);
  4766. end;
  4767. end;
  4768. #$8F: { Thumb-2: CPSxx }
  4769. begin
  4770. { set opcode }
  4771. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4772. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4773. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4774. bytes:=bytes or ord(insentry^.code[4]);
  4775. if (oper[0]^.typ=top_modeflags) then
  4776. begin
  4777. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  4778. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  4779. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 5);
  4780. end;
  4781. if (ops=2) then
  4782. bytes:=bytes or (oper[1]^.val and $1F)
  4783. else if (ops=1) and
  4784. (oper[0]^.typ=top_const) then
  4785. bytes:=bytes or (oper[0]^.val and $1F);
  4786. end;
  4787. #$96: { Thumb-2: MSR/MRS }
  4788. begin
  4789. { set instruction code }
  4790. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4791. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4792. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4793. bytes:=bytes or ord(insentry^.code[4]);
  4794. if opcode=A_MRS then
  4795. begin
  4796. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4797. case oper[1]^.reg of
  4798. NR_MSP: bytes:=bytes or $08;
  4799. NR_PSP: bytes:=bytes or $09;
  4800. NR_IPSR: bytes:=bytes or $05;
  4801. NR_EPSR: bytes:=bytes or $06;
  4802. NR_APSR: bytes:=bytes or $00;
  4803. NR_PRIMASK: bytes:=bytes or $10;
  4804. NR_BASEPRI: bytes:=bytes or $11;
  4805. NR_BASEPRI_MAX: bytes:=bytes or $12;
  4806. NR_FAULTMASK: bytes:=bytes or $13;
  4807. NR_CONTROL: bytes:=bytes or $14;
  4808. else
  4809. Message(asmw_e_invalid_opcode_and_operands);
  4810. end;
  4811. end
  4812. else
  4813. begin
  4814. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4815. case oper[0]^.reg of
  4816. NR_APSR,
  4817. NR_APSR_nzcvqg: bytes:=bytes or $C00;
  4818. NR_APSR_g: bytes:=bytes or $400;
  4819. NR_APSR_nzcvq: bytes:=bytes or $800;
  4820. NR_MSP: bytes:=bytes or $08;
  4821. NR_PSP: bytes:=bytes or $09;
  4822. NR_PRIMASK: bytes:=bytes or $10;
  4823. NR_BASEPRI: bytes:=bytes or $11;
  4824. NR_BASEPRI_MAX: bytes:=bytes or $12;
  4825. NR_FAULTMASK: bytes:=bytes or $13;
  4826. NR_CONTROL: bytes:=bytes or $14;
  4827. else
  4828. Message(asmw_e_invalid_opcode_and_operands);
  4829. end;
  4830. end;
  4831. end;
  4832. #$A0: { FPA: CPDT(LDF/STF) }
  4833. begin
  4834. { set instruction code }
  4835. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4836. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4837. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4838. bytes:=bytes or ord(insentry^.code[4]);
  4839. if ops=2 then
  4840. begin
  4841. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4842. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4843. bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
  4844. if oper[1]^.ref^.offset>=0 then
  4845. bytes:=bytes or (1 shl 23);
  4846. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4847. bytes:=bytes or (1 shl 21);
  4848. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  4849. bytes:=bytes or (1 shl 24);
  4850. case oppostfix of
  4851. PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
  4852. PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
  4853. PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4854. end;
  4855. end
  4856. else
  4857. begin
  4858. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4859. case oper[1]^.val of
  4860. 1: bytes:=bytes or (1 shl 15);
  4861. 2: bytes:=bytes or (1 shl 22);
  4862. 3: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4863. 4: ;
  4864. else
  4865. message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
  4866. end;
  4867. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4868. bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
  4869. if oper[2]^.ref^.offset>=0 then
  4870. bytes:=bytes or (1 shl 23);
  4871. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4872. bytes:=bytes or (1 shl 21);
  4873. if oper[2]^.ref^.addressmode=AM_PREINDEXED then
  4874. bytes:=bytes or (1 shl 24);
  4875. end;
  4876. end;
  4877. #$A1: { FPA: CPDO }
  4878. begin
  4879. { set instruction code }
  4880. bytes:=bytes or ($E shl 24);
  4881. bytes:=bytes or (ord(insentry^.code[1]) shl 15);
  4882. bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
  4883. bytes:=bytes or (1 shl 8);
  4884. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4885. if ops=2 then
  4886. begin
  4887. if oper[1]^.typ=top_reg then
  4888. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4889. else
  4890. case oper[1]^.val of
  4891. 0: bytes:=bytes or $8;
  4892. 1: bytes:=bytes or $9;
  4893. 2: bytes:=bytes or $A;
  4894. 3: bytes:=bytes or $B;
  4895. 4: bytes:=bytes or $C;
  4896. 5: bytes:=bytes or $D;
  4897. //0.5: bytes:=bytes or $E;
  4898. 10: bytes:=bytes or $F;
  4899. else
  4900. Message(asmw_e_invalid_opcode_and_operands);
  4901. end;
  4902. end
  4903. else
  4904. begin
  4905. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  4906. if oper[2]^.typ=top_reg then
  4907. bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
  4908. else
  4909. case oper[2]^.val of
  4910. 0: bytes:=bytes or $8;
  4911. 1: bytes:=bytes or $9;
  4912. 2: bytes:=bytes or $A;
  4913. 3: bytes:=bytes or $B;
  4914. 4: bytes:=bytes or $C;
  4915. 5: bytes:=bytes or $D;
  4916. //0.5: bytes:=bytes or $E;
  4917. 10: bytes:=bytes or $F;
  4918. else
  4919. Message(asmw_e_invalid_opcode_and_operands);
  4920. end;
  4921. end;
  4922. case roundingmode of
  4923. RM_P: bytes:=bytes or (1 shl 5);
  4924. RM_M: bytes:=bytes or (2 shl 5);
  4925. RM_Z: bytes:=bytes or (3 shl 5);
  4926. end;
  4927. case oppostfix of
  4928. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4929. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4930. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4931. else
  4932. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4933. end;
  4934. end;
  4935. #$A2: { FPA: CPDO }
  4936. begin
  4937. { set instruction code }
  4938. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4939. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4940. bytes:=bytes or ($11 shl 4);
  4941. case opcode of
  4942. A_FLT:
  4943. begin
  4944. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4945. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  4946. case roundingmode of
  4947. RM_P: bytes:=bytes or (1 shl 5);
  4948. RM_M: bytes:=bytes or (2 shl 5);
  4949. RM_Z: bytes:=bytes or (3 shl 5);
  4950. end;
  4951. case oppostfix of
  4952. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4953. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4954. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4955. else
  4956. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4957. end;
  4958. end;
  4959. A_FIX:
  4960. begin
  4961. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4962. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4963. case roundingmode of
  4964. RM_P: bytes:=bytes or (1 shl 5);
  4965. RM_M: bytes:=bytes or (2 shl 5);
  4966. RM_Z: bytes:=bytes or (3 shl 5);
  4967. end;
  4968. end;
  4969. A_WFS,A_RFS,A_WFC,A_RFC:
  4970. begin
  4971. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4972. end;
  4973. A_CMF,A_CNF,A_CMFE,A_CNFE:
  4974. begin
  4975. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4976. if oper[1]^.typ=top_reg then
  4977. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4978. else
  4979. case oper[1]^.val of
  4980. 0: bytes:=bytes or $8;
  4981. 1: bytes:=bytes or $9;
  4982. 2: bytes:=bytes or $A;
  4983. 3: bytes:=bytes or $B;
  4984. 4: bytes:=bytes or $C;
  4985. 5: bytes:=bytes or $D;
  4986. //0.5: bytes:=bytes or $E;
  4987. 10: bytes:=bytes or $F;
  4988. else
  4989. Message(asmw_e_invalid_opcode_and_operands);
  4990. end;
  4991. end;
  4992. end;
  4993. end;
  4994. #$fe: // No written data
  4995. begin
  4996. exit;
  4997. end;
  4998. #$ff:
  4999. internalerror(2005091101);
  5000. else
  5001. begin
  5002. writeln(ord(insentry^.code[0]), ' - ', opcode);
  5003. internalerror(2005091102);
  5004. end;
  5005. end;
  5006. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  5007. if (insentry^.code[0] in [#$80..#$96]) and (bytelen=4) then
  5008. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  5009. { we're finished, write code }
  5010. objdata.writebytes(bytes,bytelen);
  5011. end;
  5012. begin
  5013. cai_align:=tai_align;
  5014. end.