aasmcpu.pas 213 KB

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