aasmcpu.pas 210 KB

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