aasmcpu.pas 202 KB

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