aasmcpu.pas 212 KB

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