aasmcpu.pas 207 KB

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