tc-m68hc11.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497
  1. /* tc-m68hc11.c -- Assembler code for the Motorola 68HC11 & 68HC12.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Stephane Carrez (stcarrez@nerim.fr)
  4. XGATE and S12X added by James Murray (jsm@jsm-net.demon.co.uk)
  5. This file is part of GAS, the GNU Assembler.
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GAS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GAS; see the file COPYING. If not, write to
  16. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. #include "as.h"
  19. #include "safe-ctype.h"
  20. #include "subsegs.h"
  21. #include "opcode/m68hc11.h"
  22. #include "dwarf2dbg.h"
  23. #include "elf/m68hc11.h"
  24. const char comment_chars[] = ";!";
  25. const char line_comment_chars[] = "#*";
  26. const char line_separator_chars[] = "";
  27. const char EXP_CHARS[] = "eE";
  28. const char FLT_CHARS[] = "dD";
  29. #define STATE_CONDITIONAL_BRANCH (1)
  30. #define STATE_PC_RELATIVE (2)
  31. #define STATE_INDEXED_OFFSET (3)
  32. #define STATE_INDEXED_PCREL (4)
  33. #define STATE_XBCC_BRANCH (5)
  34. #define STATE_CONDITIONAL_BRANCH_6812 (6)
  35. #define STATE_BYTE (0)
  36. #define STATE_BITS5 (0)
  37. #define STATE_WORD (1)
  38. #define STATE_BITS9 (1)
  39. #define STATE_LONG (2)
  40. #define STATE_BITS16 (2)
  41. #define STATE_UNDF (3) /* Symbol undefined in pass1 */
  42. /* This macro has no side-effects. */
  43. #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
  44. #define RELAX_STATE(s) ((s) >> 2)
  45. #define RELAX_LENGTH(s) ((s) & 3)
  46. #define IS_OPCODE(C1,C2) (((C1) & 0x0FF) == ((C2) & 0x0FF))
  47. /* This table describes how you change sizes for the various types of variable
  48. size expressions. This version only supports two kinds. */
  49. /* The fields are:
  50. How far Forward this mode will reach.
  51. How far Backward this mode will reach.
  52. How many bytes this mode will add to the size of the frag.
  53. Which mode to go to if the offset won't fit in this one. */
  54. relax_typeS md_relax_table[] =
  55. {
  56. {1, 1, 0, 0}, /* First entries aren't used. */
  57. {1, 1, 0, 0}, /* For no good reason except. */
  58. {1, 1, 0, 0}, /* that the VAX doesn't either. */
  59. {1, 1, 0, 0},
  60. /* Relax for bcc <L>.
  61. These insns are translated into b!cc +3 jmp L. */
  62. {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD)},
  63. {0, 0, 3, 0},
  64. {1, 1, 0, 0},
  65. {1, 1, 0, 0},
  66. /* Relax for bsr <L> and bra <L>.
  67. These insns are translated into jsr and jmp. */
  68. {(127), (-128), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)},
  69. {0, 0, 1, 0},
  70. {1, 1, 0, 0},
  71. {1, 1, 0, 0},
  72. /* Relax for indexed offset: 5-bits, 9-bits, 16-bits. */
  73. {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9)},
  74. {(255), (-256), 1, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16)},
  75. {0, 0, 2, 0},
  76. {1, 1, 0, 0},
  77. /* Relax for PC relative offset: 5-bits, 9-bits, 16-bits.
  78. For the 9-bit case, there will be a -1 correction to take into
  79. account the new byte that's why the range is -255..256. */
  80. {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9)},
  81. {(256), (-255), 1, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16)},
  82. {0, 0, 2, 0},
  83. {1, 1, 0, 0},
  84. /* Relax for dbeq/ibeq/tbeq r,<L>:
  85. These insns are translated into db!cc +3 jmp L. */
  86. {(255), (-256), 0, ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD)},
  87. {0, 0, 3, 0},
  88. {1, 1, 0, 0},
  89. {1, 1, 0, 0},
  90. /* Relax for bcc <L> on 68HC12.
  91. These insns are translated into lbcc <L>. */
  92. {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD)},
  93. {0, 0, 2, 0},
  94. {1, 1, 0, 0},
  95. {1, 1, 0, 0},
  96. };
  97. /* 68HC11 and 68HC12 registers. They are numbered according to the 68HC12. */
  98. typedef enum register_id
  99. {
  100. REG_NONE = -1,
  101. REG_A = 0,
  102. REG_B = 1,
  103. REG_CCR = 2,
  104. REG_D = 4,
  105. REG_X = 5,
  106. REG_Y = 6,
  107. REG_SP = 7,
  108. REG_PC = 8,
  109. REG_R0 = 0,
  110. REG_R1 = 1,
  111. REG_R2 = 2,
  112. REG_R3 = 3,
  113. REG_R4 = 4,
  114. REG_R5 = 5,
  115. REG_R6 = 6,
  116. REG_R7 = 7,
  117. REG_SP_XG = 8,
  118. REG_PC_XG = 9,
  119. REG_CCR_XG = 10
  120. } register_id;
  121. typedef struct operand
  122. {
  123. expressionS exp;
  124. register_id reg1;
  125. register_id reg2;
  126. int mode;
  127. } operand;
  128. struct m68hc11_opcode_def
  129. {
  130. long format;
  131. int min_operands;
  132. int max_operands;
  133. int nb_modes;
  134. int used;
  135. struct m68hc11_opcode *opcode;
  136. };
  137. static struct m68hc11_opcode_def *m68hc11_opcode_defs = 0;
  138. static int m68hc11_nb_opcode_defs = 0;
  139. typedef struct alias
  140. {
  141. const char *name;
  142. const char *alias;
  143. } alias;
  144. static alias alias_opcodes[] =
  145. {
  146. {"cpd", "cmpd"},
  147. {"cpx", "cmpx"},
  148. {"cpy", "cmpy"},
  149. {0, 0}
  150. };
  151. struct m9s12xg_opcode_def
  152. {
  153. long format;
  154. int min_operands;
  155. int max_operands;
  156. int nb_modes;
  157. int used;
  158. struct m9s12xg_opcode *opcode;
  159. };
  160. /* Local functions. */
  161. static register_id reg_name_search (char *);
  162. static register_id register_name (void);
  163. static int cmp_opcode (struct m68hc11_opcode *, struct m68hc11_opcode *);
  164. static char *print_opcode_format (struct m68hc11_opcode *, int);
  165. static char *skip_whites (char *);
  166. static int check_range (long, int);
  167. static void print_opcode_list (void);
  168. static void get_default_target (void);
  169. static void print_insn_format (char *);
  170. static int get_operand (operand *, int, long);
  171. static void fixup8 (expressionS *, int, int);
  172. static void fixup16 (expressionS *, int, int);
  173. static void fixup24 (expressionS *, int, int);
  174. static void fixup8_xg (expressionS *, int, int);
  175. static unsigned char convert_branch (unsigned char);
  176. static char *m68hc11_new_insn (int);
  177. static void build_dbranch_insn (struct m68hc11_opcode *,
  178. operand *, int, int);
  179. static int build_indexed_byte (operand *, int, int);
  180. static int build_reg_mode (operand *, int);
  181. static struct m68hc11_opcode *find (struct m68hc11_opcode_def *,
  182. operand *, int);
  183. static struct m68hc11_opcode *find_opcode (struct m68hc11_opcode_def *,
  184. operand *, int *);
  185. static void build_jump_insn (struct m68hc11_opcode *, operand *, int, int);
  186. static void build_insn_xg (struct m68hc11_opcode *, operand *, int);
  187. static void build_insn (struct m68hc11_opcode *, operand *, int);
  188. static int relaxable_symbol (symbolS *);
  189. /* Pseudo op to indicate a relax group. */
  190. static void s_m68hc11_relax (int);
  191. /* Pseudo op to control the ELF flags. */
  192. static void s_m68hc11_mode (int);
  193. /* Process directives specified via pseudo ops. */
  194. static void s_m68hc11_parse_pseudo_instruction (int);
  195. /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
  196. are using 'rtc' for returning. It is necessary to use 'call'
  197. to invoke them. This is also used by the debugger to correctly
  198. find the stack frame. */
  199. static void s_m68hc11_mark_symbol (int);
  200. /* Controls whether relative branches can be turned into long branches.
  201. When the relative offset is too large, the insn are changed:
  202. bra -> jmp
  203. bsr -> jsr
  204. bcc -> b!cc +3
  205. jmp L
  206. dbcc -> db!cc +3
  207. jmp L
  208. Setting the flag forbids this. */
  209. static short flag_fixed_branches = 0;
  210. /* Force to use long jumps (absolute) instead of relative branches. */
  211. static short flag_force_long_jumps = 0;
  212. /* Change the direct addressing mode into an absolute addressing mode
  213. when the insn does not support direct addressing.
  214. For example, "clr *ZD0" is normally not possible and is changed
  215. into "clr ZDO". */
  216. static short flag_strict_direct_addressing = 1;
  217. /* When an opcode has invalid operand, print out the syntax of the opcode
  218. to stderr. */
  219. static short flag_print_insn_syntax = 0;
  220. /* Dumps the list of instructions with syntax and then exit:
  221. 1 -> Only dumps the list (sorted by name)
  222. 2 -> Generate an example (or test) that can be compiled. */
  223. static short flag_print_opcodes = 0;
  224. /* Opcode hash table. */
  225. static htab_t m68hc11_hash;
  226. /* Current cpu (either cpu6811 or cpu6812). This is determined automagically
  227. by 'get_default_target' by looking at default BFD vector. This is overridden
  228. with the -m<cpu> option. */
  229. static int current_architecture = 0;
  230. /* Default cpu determined by 'get_default_target'. */
  231. static const char *default_cpu;
  232. /* Number of opcodes in the sorted table (filtered by current cpu). */
  233. static int num_opcodes;
  234. /* The opcodes sorted by name and filtered by current cpu. */
  235. static struct m68hc11_opcode *m68hc11_sorted_opcodes;
  236. /* ELF flags to set in the output file header. */
  237. static int elf_flags = E_M68HC11_F64;
  238. /* These are the machine dependent pseudo-ops. These are included so
  239. the assembler can work on the output from the SUN C compiler, which
  240. generates these. */
  241. /* This table describes all the machine specific pseudo-ops the assembler
  242. has to support. The fields are:
  243. pseudo-op name without dot
  244. function to call to execute this pseudo-op
  245. Integer arg to pass to the function. */
  246. const pseudo_typeS md_pseudo_table[] =
  247. {
  248. /* The following pseudo-ops are supported for MRI compatibility. */
  249. {"fcb", cons, 1},
  250. {"fdb", cons, 2},
  251. {"fqb", cons, 4},
  252. {"fcc", stringer, 8 + 1},
  253. {"rmb", s_space, 0},
  254. /* Motorola ALIS. */
  255. {"xrefb", s_ignore, 0}, /* Same as xref */
  256. /* Gcc driven relaxation. */
  257. {"relax", s_m68hc11_relax, 0},
  258. /* .mode instruction (ala SH). */
  259. {"mode", s_m68hc11_mode, 0},
  260. /* .far instruction. */
  261. {"far", s_m68hc11_mark_symbol, STO_M68HC12_FAR},
  262. /* .interrupt instruction. */
  263. {"interrupt", s_m68hc11_mark_symbol, STO_M68HC12_INTERRUPT},
  264. /* .nobankwarning instruction. */
  265. {"nobankwarning", s_m68hc11_parse_pseudo_instruction, E_M68HC11_NO_BANK_WARNING},
  266. {0, 0, 0}
  267. };
  268. /* Options and initialization. */
  269. const char *md_shortopts = "Sm:";
  270. struct option md_longopts[] =
  271. {
  272. #define OPTION_FORCE_LONG_BRANCH (OPTION_MD_BASE)
  273. {"force-long-branches", no_argument, NULL, OPTION_FORCE_LONG_BRANCH},
  274. {"force-long-branchs", no_argument, NULL, OPTION_FORCE_LONG_BRANCH}, /* Misspelled version kept for backwards compatibility. */
  275. #define OPTION_SHORT_BRANCHES (OPTION_MD_BASE + 1)
  276. {"short-branches", no_argument, NULL, OPTION_SHORT_BRANCHES},
  277. {"short-branchs", no_argument, NULL, OPTION_SHORT_BRANCHES}, /* Misspelled version kept for backwards compatibility. */
  278. #define OPTION_STRICT_DIRECT_MODE (OPTION_MD_BASE + 2)
  279. {"strict-direct-mode", no_argument, NULL, OPTION_STRICT_DIRECT_MODE},
  280. #define OPTION_PRINT_INSN_SYNTAX (OPTION_MD_BASE + 3)
  281. {"print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX},
  282. #define OPTION_PRINT_OPCODES (OPTION_MD_BASE + 4)
  283. {"print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES},
  284. #define OPTION_GENERATE_EXAMPLE (OPTION_MD_BASE + 5)
  285. {"generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE},
  286. #define OPTION_MSHORT (OPTION_MD_BASE + 6)
  287. {"mshort", no_argument, NULL, OPTION_MSHORT},
  288. #define OPTION_MLONG (OPTION_MD_BASE + 7)
  289. {"mlong", no_argument, NULL, OPTION_MLONG},
  290. #define OPTION_MSHORT_DOUBLE (OPTION_MD_BASE + 8)
  291. {"mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE},
  292. #define OPTION_MLONG_DOUBLE (OPTION_MD_BASE + 9)
  293. {"mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE},
  294. #define OPTION_XGATE_RAMOFFSET (OPTION_MD_BASE + 10)
  295. {"xgate-ramoffset", no_argument, NULL, OPTION_XGATE_RAMOFFSET},
  296. {NULL, no_argument, NULL, 0}
  297. };
  298. size_t md_longopts_size = sizeof (md_longopts);
  299. /* Get the target cpu for the assembler. This is based on the configure
  300. options and on the -m68hc11/-m68hc12 option. If no option is specified,
  301. we must get the default. */
  302. const char *
  303. m68hc11_arch_format (void)
  304. {
  305. get_default_target ();
  306. if (current_architecture & cpu6811)
  307. return "elf32-m68hc11";
  308. else
  309. return "elf32-m68hc12";
  310. }
  311. enum bfd_architecture
  312. m68hc11_arch (void)
  313. {
  314. get_default_target ();
  315. if (current_architecture & cpu6811)
  316. return bfd_arch_m68hc11;
  317. else
  318. return bfd_arch_m68hc12;
  319. }
  320. int
  321. m68hc11_mach (void)
  322. {
  323. return 0;
  324. }
  325. /* Listing header selected according to cpu. */
  326. const char *
  327. m68hc11_listing_header (void)
  328. {
  329. if (current_architecture & cpu6811)
  330. return "M68HC11 GAS ";
  331. else if (current_architecture & cpuxgate)
  332. return "XGATE GAS ";
  333. else if (current_architecture & cpu9s12x)
  334. return "S12X GAS ";
  335. else
  336. return "M68HC12 GAS ";
  337. }
  338. void
  339. md_show_usage (FILE *stream)
  340. {
  341. get_default_target ();
  342. fprintf (stream, _("\
  343. Motorola 68HC11/68HC12/68HCS12 options:\n\
  344. -m68hc11 | -m68hc12 |\n\
  345. -m68hcs12 | -mm9s12x |\n\
  346. -mm9s12xg specify the processor [default %s]\n\
  347. -mshort use 16-bit int ABI (default)\n\
  348. -mlong use 32-bit int ABI\n\
  349. -mshort-double use 32-bit double ABI\n\
  350. -mlong-double use 64-bit double ABI (default)\n\
  351. --force-long-branches always turn relative branches into absolute ones\n\
  352. -S,--short-branches do not turn relative branches into absolute ones\n\
  353. when the offset is out of range\n\
  354. --strict-direct-mode do not turn the direct mode into extended mode\n\
  355. when the instruction does not support direct mode\n\
  356. --print-insn-syntax print the syntax of instruction in case of error\n\
  357. --print-opcodes print the list of instructions with syntax\n\
  358. --xgate-ramoffset offset ram addresses by 0xc000\n\
  359. --generate-example generate an example of each instruction\n\
  360. (used for testing)\n"), default_cpu);
  361. }
  362. /* Try to identify the default target based on the BFD library. */
  363. static void
  364. get_default_target (void)
  365. {
  366. const bfd_target *target;
  367. bfd abfd;
  368. if (current_architecture != 0)
  369. return;
  370. default_cpu = "unknown";
  371. target = bfd_find_target (0, &abfd);
  372. if (target && target->name)
  373. {
  374. if (strcmp (target->name, "elf32-m68hc12") == 0)
  375. {
  376. current_architecture = cpu6812;
  377. default_cpu = "m68hc12";
  378. }
  379. else if (strcmp (target->name, "elf32-m68hc11") == 0)
  380. {
  381. current_architecture = cpu6811;
  382. default_cpu = "m68hc11";
  383. }
  384. else
  385. {
  386. as_bad (_("Default target `%s' is not supported."), target->name);
  387. }
  388. }
  389. }
  390. void
  391. m68hc11_print_statistics (FILE *file)
  392. {
  393. int i;
  394. struct m68hc11_opcode_def *opc;
  395. htab_print_statistics (file, "opcode table", m68hc11_hash);
  396. opc = m68hc11_opcode_defs;
  397. if (opc == 0 || m68hc11_nb_opcode_defs == 0)
  398. return;
  399. /* Dump the opcode statistics table. */
  400. fprintf (file, _("Name # Modes Min ops Max ops Modes mask # Used\n"));
  401. for (i = 0; i < m68hc11_nb_opcode_defs; i++, opc++)
  402. {
  403. fprintf (file, "%-7.7s %5d %7d %7d 0x%08lx %7d\n",
  404. opc->opcode->name,
  405. opc->nb_modes,
  406. opc->min_operands, opc->max_operands, opc->format, opc->used);
  407. }
  408. }
  409. int
  410. md_parse_option (int c, const char *arg)
  411. {
  412. get_default_target ();
  413. switch (c)
  414. {
  415. /* -S means keep external to 2 bit offset rather than 16 bit one. */
  416. case OPTION_SHORT_BRANCHES:
  417. case 'S':
  418. flag_fixed_branches = 1;
  419. break;
  420. case OPTION_FORCE_LONG_BRANCH:
  421. flag_force_long_jumps = 1;
  422. break;
  423. case OPTION_PRINT_INSN_SYNTAX:
  424. flag_print_insn_syntax = 1;
  425. break;
  426. case OPTION_PRINT_OPCODES:
  427. flag_print_opcodes = 1;
  428. break;
  429. case OPTION_STRICT_DIRECT_MODE:
  430. flag_strict_direct_addressing = 0;
  431. break;
  432. case OPTION_GENERATE_EXAMPLE:
  433. flag_print_opcodes = 2;
  434. break;
  435. case OPTION_MSHORT:
  436. elf_flags &= ~E_M68HC11_I32;
  437. break;
  438. case OPTION_MLONG:
  439. elf_flags |= E_M68HC11_I32;
  440. break;
  441. case OPTION_MSHORT_DOUBLE:
  442. elf_flags &= ~E_M68HC11_F64;
  443. break;
  444. case OPTION_MLONG_DOUBLE:
  445. elf_flags |= E_M68HC11_F64;
  446. break;
  447. case OPTION_XGATE_RAMOFFSET:
  448. elf_flags |= E_M68HC11_XGATE_RAMOFFSET;
  449. break;
  450. case 'm':
  451. if ((strcasecmp (arg, "68hc11") == 0)
  452. || (strcasecmp (arg, "m68hc11") == 0))
  453. current_architecture = cpu6811;
  454. else if ((strcasecmp (arg, "68hc12") == 0)
  455. || (strcasecmp (arg, "m68hc12") == 0))
  456. current_architecture = cpu6812;
  457. else if ((strcasecmp (arg, "68hcs12") == 0)
  458. || (strcasecmp (arg, "m68hcs12") == 0))
  459. current_architecture = cpu6812 | cpu6812s;
  460. else if (strcasecmp (arg, "m9s12x") == 0)
  461. current_architecture = cpu6812 | cpu6812s | cpu9s12x;
  462. else if ((strcasecmp (arg, "m9s12xg") == 0)
  463. || (strcasecmp (arg, "xgate") == 0))
  464. /* xgate for backwards compatibility */
  465. current_architecture = cpuxgate;
  466. else
  467. as_bad (_("Option `%s' is not recognized."), arg);
  468. break;
  469. default:
  470. return 0;
  471. }
  472. return 1;
  473. }
  474. symbolS *
  475. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  476. {
  477. return 0;
  478. }
  479. const char *
  480. md_atof (int type, char *litP, int *sizeP)
  481. {
  482. return ieee_md_atof (type, litP, sizeP, true);
  483. }
  484. valueT
  485. md_section_align (asection *seg, valueT addr)
  486. {
  487. int align = bfd_section_alignment (seg);
  488. return ((addr + (1 << align) - 1) & -(1 << align));
  489. }
  490. static int
  491. cmp_opcode (struct m68hc11_opcode *op1, struct m68hc11_opcode *op2)
  492. {
  493. return strcmp (op1->name, op2->name);
  494. }
  495. #define IS_CALL_SYMBOL(MODE) \
  496. (((MODE) & (M6812_OP_PAGE|M6811_OP_IND16)) \
  497. == ((M6812_OP_PAGE|M6811_OP_IND16)))
  498. /* Initialize the assembler. Create the opcode hash table
  499. (sorted on the names) with the M6811 opcode table
  500. (from opcode library). */
  501. void
  502. md_begin (void)
  503. {
  504. const char *prev_name = "";
  505. struct m68hc11_opcode *opcodes;
  506. struct m68hc11_opcode_def *opc = 0;
  507. int i, j;
  508. get_default_target ();
  509. m68hc11_hash = str_htab_create ();
  510. /* Get a writable copy of the opcode table and sort it on the names. */
  511. opcodes = XNEWVEC (struct m68hc11_opcode, m68hc11_num_opcodes);
  512. m68hc11_sorted_opcodes = opcodes;
  513. num_opcodes = 0;
  514. for (i = 0; i < m68hc11_num_opcodes; i++)
  515. {
  516. if (m68hc11_opcodes[i].arch & current_architecture)
  517. {
  518. opcodes[num_opcodes] = m68hc11_opcodes[i];
  519. if (opcodes[num_opcodes].name[0] == 'b'
  520. && opcodes[num_opcodes].format & M6811_OP_JUMP_REL
  521. && !(opcodes[num_opcodes].format & M6811_OP_BITMASK))
  522. {
  523. num_opcodes++;
  524. opcodes[num_opcodes] = m68hc11_opcodes[i];
  525. }
  526. num_opcodes++;
  527. for (j = 0; alias_opcodes[j].name != 0; j++)
  528. if (strcmp (m68hc11_opcodes[i].name, alias_opcodes[j].name) == 0)
  529. {
  530. opcodes[num_opcodes] = m68hc11_opcodes[i];
  531. opcodes[num_opcodes].name = alias_opcodes[j].alias;
  532. num_opcodes++;
  533. break;
  534. }
  535. }
  536. }
  537. qsort (opcodes, num_opcodes, sizeof (struct m68hc11_opcode),
  538. (int (*) (const void*, const void*)) cmp_opcode);
  539. opc = XNEWVEC (struct m68hc11_opcode_def, num_opcodes);
  540. m68hc11_opcode_defs = opc--;
  541. /* Insert unique names into hash table. The M6811 instruction set
  542. has several identical opcode names that have different opcodes based
  543. on the operands. This hash table then provides a quick index to
  544. the first opcode with a particular name in the opcode table. */
  545. for (i = 0; i < num_opcodes; i++, opcodes++)
  546. {
  547. int expect;
  548. if (strcmp (prev_name, opcodes->name))
  549. {
  550. prev_name = (char *) opcodes->name;
  551. opc++;
  552. opc->format = 0;
  553. opc->min_operands = 100;
  554. opc->max_operands = 0;
  555. opc->nb_modes = 0;
  556. opc->opcode = opcodes;
  557. opc->used = 0;
  558. str_hash_insert (m68hc11_hash, opcodes->name, opc, 0);
  559. }
  560. opc->nb_modes++;
  561. opc->format |= opcodes->format;
  562. /* See how many operands this opcode needs. */
  563. expect = 0;
  564. if (opcodes->arch == cpuxgate)
  565. {
  566. if (opcodes->format & (M68XG_OP_IMM3 | M68XG_OP_R | M68XG_OP_REL9
  567. | M68XG_OP_REL10 ))
  568. expect = 1;
  569. else if (opcodes->format & (M68XG_OP_R_R | M68XG_OP_R_IMM4
  570. | M68XG_OP_R_IMM8 | M68XG_OP_R_IMM8))
  571. expect = 2;
  572. else if (opcodes->format & (M68XG_OP_R_R_R | M68XG_OP_R_R_OFFS5
  573. | M68XG_OP_RD_RB_RI | M68XG_OP_RD_RB_RIp
  574. | M68XG_OP_RD_RB_mRI))
  575. expect = 3;
  576. }
  577. else
  578. {
  579. if (opcodes->format & M6811_OP_MASK)
  580. expect++;
  581. if (opcodes->format & M6811_OP_BITMASK)
  582. expect++;
  583. if (opcodes->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  584. expect++;
  585. if (opcodes->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  586. expect++;
  587. /* Special case for call instruction. */
  588. if ((opcodes->format & M6812_OP_PAGE)
  589. && !(opcodes->format & M6811_OP_IND16))
  590. expect++;
  591. }
  592. if (expect < opc->min_operands)
  593. opc->min_operands = expect;
  594. if (IS_CALL_SYMBOL (opcodes->format))
  595. expect++;
  596. if (expect > opc->max_operands)
  597. opc->max_operands = expect;
  598. }
  599. opc++;
  600. m68hc11_nb_opcode_defs = opc - m68hc11_opcode_defs;
  601. if (flag_print_opcodes)
  602. {
  603. print_opcode_list ();
  604. exit (EXIT_SUCCESS);
  605. }
  606. }
  607. void
  608. m68hc11_init_after_args (void)
  609. {
  610. }
  611. /* Builtin help. */
  612. /* Return a string that represents the operand format for the instruction.
  613. When example is true, this generates an example of operand. This is used
  614. to give an example and also to generate a test. */
  615. static char *
  616. print_opcode_format (struct m68hc11_opcode *opcode, int example)
  617. {
  618. static char buf[128];
  619. int format = opcode->format;
  620. char *p;
  621. p = buf;
  622. buf[0] = 0;
  623. if (current_architecture == cpuxgate)
  624. {
  625. if (format & M68XG_OP_IMM3)
  626. {
  627. if (example)
  628. sprintf (p, "#%d", rand () & 0x007);
  629. else
  630. strcpy (p, _("imm3"));
  631. p = &p[strlen (p)];
  632. }
  633. else if (format & M68XG_OP_R)
  634. {
  635. if (example)
  636. sprintf (p, "R%d", rand () & 0x07);
  637. else
  638. strcpy (p, _("RD"));
  639. p = &p[strlen (p)];
  640. }
  641. else if (format & M68XG_OP_R_R)
  642. {
  643. if (example)
  644. sprintf (p, "R%d,R%d", rand () & 0x07, rand () & 0x07);
  645. else
  646. strcpy (p, _("RD,RS"));
  647. p = &p[strlen (p)];
  648. }
  649. else if (format & M68XG_OP_R_IMM4)
  650. {
  651. if (example)
  652. sprintf (p, "R%d,#%d", rand () & 0x07, rand () & 0x0f);
  653. else
  654. strcpy (p, _("RI, #imm4"));
  655. p = &p[strlen (p)];
  656. }
  657. else if (format & M68XG_OP_R_R_R)
  658. {
  659. if (example)
  660. sprintf (p, "R%d,R%d,R%d", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  661. else
  662. strcpy (p, "RD,RS1,RS2");
  663. p = &p[strlen (p)];
  664. }
  665. else if (format & M68XG_OP_REL9)
  666. {
  667. if (example)
  668. sprintf (p, "%d", rand () & 0x1FF);
  669. else
  670. strcpy (p, "<rel9>");
  671. p = &p[strlen (p)];
  672. }
  673. else if (format & M68XG_OP_REL10)
  674. {
  675. if (example)
  676. sprintf (p, "%d", rand () & 0x3FF);
  677. else
  678. strcpy (p, "<rel10>");
  679. p = &p[strlen (p)];
  680. }
  681. else if (format & M68XG_OP_R_R_OFFS5)
  682. {
  683. if (example)
  684. sprintf (p, "R%d, (R%d, #0x%x)", rand () & 0x07, rand () & 0x07, rand () & 0x1f);
  685. else
  686. strcpy (p, _("RD, (RI,#offs5)"));
  687. p = &p[strlen (p)];
  688. }
  689. else if (format & M68XG_OP_RD_RB_RI)
  690. {
  691. if (example)
  692. sprintf (p, "R%d, (R%d, R%d)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  693. else
  694. strcpy (p, "RD, (RB, RI)");
  695. p = &p[strlen (p)];
  696. }
  697. else if (format & M68XG_OP_RD_RB_RIp)
  698. {
  699. if (example)
  700. sprintf (p, "R%d, (R%d, R%d+)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  701. else
  702. strcpy (p, "RD, (RB, RI+)");
  703. p = &p[strlen (p)];
  704. }
  705. else if (format & M68XG_OP_RD_RB_mRI)
  706. {
  707. if (example)
  708. sprintf (p, "R%d, (R%d, -R%d)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  709. else
  710. strcpy (p, "RD, (RB, -RI)");
  711. p = &p[strlen (p)];
  712. }
  713. else if (format & M68XG_OP_R_IMM8)
  714. {
  715. if (example)
  716. sprintf (p, "R%d, #0x%x", rand () & 0x07, rand () & 0xff);
  717. else
  718. strcpy (p, "RD, #imm8");
  719. p = &p[strlen (p)];
  720. }
  721. else if (format & M68XG_OP_R_IMM16)
  722. {
  723. if (example)
  724. sprintf (p, "R%d, #0x%x", rand () & 0x07, rand () & 0xffff);
  725. else
  726. strcpy (p, "RD, #imm16");
  727. p = &p[strlen (p)];
  728. }
  729. }
  730. else
  731. {
  732. if (format & M6811_OP_IMM8)
  733. {
  734. if (example)
  735. sprintf (p, "#%d", rand () & 0x0FF);
  736. else
  737. strcpy (p, _("#<imm8>"));
  738. p = &p[strlen (p)];
  739. }
  740. if (format & M6811_OP_IMM16)
  741. {
  742. if (example)
  743. sprintf (p, "#%d", rand () & 0x0FFFF);
  744. else
  745. strcpy (p, _("#<imm16>"));
  746. p = &p[strlen (p)];
  747. }
  748. if (format & M6811_OP_IX)
  749. {
  750. if (example)
  751. sprintf (p, "%d,X", rand () & 0x0FF);
  752. else
  753. strcpy (p, _("<imm8>,X"));
  754. p = &p[strlen (p)];
  755. }
  756. if (format & M6811_OP_IY)
  757. {
  758. if (example)
  759. sprintf (p, "%d,X", rand () & 0x0FF);
  760. else
  761. strcpy (p, _("<imm8>,X"));
  762. p = &p[strlen (p)];
  763. }
  764. if (format & M6812_OP_IDX)
  765. {
  766. if (example)
  767. sprintf (p, "%d,X", rand () & 0x0FF);
  768. else
  769. strcpy (p, "n,r");
  770. p = &p[strlen (p)];
  771. }
  772. if (format & M6812_OP_PAGE)
  773. {
  774. if (example)
  775. sprintf (p, ", %d", rand () & 0x0FF);
  776. else
  777. strcpy (p, ", <page>");
  778. p = &p[strlen (p)];
  779. }
  780. if (format & M6811_OP_DIRECT)
  781. {
  782. if (example)
  783. sprintf (p, "*Z%d", rand () & 0x0FF);
  784. else
  785. strcpy (p, _("*<abs8>"));
  786. p = &p[strlen (p)];
  787. }
  788. if (format & M6811_OP_BITMASK)
  789. {
  790. if (buf[0])
  791. *p++ = ' ';
  792. if (example)
  793. sprintf (p, "#$%02x", rand () & 0x0FF);
  794. else
  795. strcpy (p, _("#<mask>"));
  796. p = &p[strlen (p)];
  797. if (format & M6811_OP_JUMP_REL)
  798. *p++ = ' ';
  799. }
  800. if (format & M6811_OP_IND16)
  801. {
  802. if (example)
  803. sprintf (p, _("symbol%d"), rand () & 0x0FF);
  804. else
  805. strcpy (p, _("<abs>"));
  806. p = &p[strlen (p)];
  807. }
  808. if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  809. {
  810. if (example)
  811. {
  812. if (format & M6811_OP_BITMASK)
  813. {
  814. sprintf (p, ".+%d", rand () & 0x7F);
  815. }
  816. else
  817. {
  818. sprintf (p, "L%d", rand () & 0x0FF);
  819. }
  820. }
  821. else
  822. strcpy (p, _("<label>"));
  823. }
  824. }
  825. return buf;
  826. }
  827. /* Prints the list of instructions with the possible operands. */
  828. static void
  829. print_opcode_list (void)
  830. {
  831. int i;
  832. const char *prev_name = "";
  833. struct m68hc11_opcode *opcodes;
  834. int example = flag_print_opcodes == 2;
  835. if (example)
  836. printf (_("# Example of `%s' instructions\n\t.sect .text\n_start:\n"),
  837. default_cpu);
  838. opcodes = m68hc11_sorted_opcodes;
  839. /* Walk the list sorted on names (by md_begin). We only report
  840. one instruction per line, and we collect the different operand
  841. formats. */
  842. for (i = 0; i < num_opcodes; i++, opcodes++)
  843. {
  844. char *fmt = print_opcode_format (opcodes, example);
  845. if (example)
  846. {
  847. printf ("L%d:\t", i);
  848. printf ("%s %s\n", opcodes->name, fmt);
  849. }
  850. else
  851. {
  852. if (strcmp (prev_name, opcodes->name))
  853. {
  854. if (i > 0)
  855. printf ("\n");
  856. printf ("%-5.5s ", opcodes->name);
  857. prev_name = (char *) opcodes->name;
  858. }
  859. if (fmt[0])
  860. printf (" [%s]", fmt);
  861. }
  862. }
  863. printf ("\n");
  864. }
  865. /* Print the instruction format. This operation is called when some
  866. instruction is not correct. Instruction format is printed as an
  867. error message. */
  868. static void
  869. print_insn_format (char *name)
  870. {
  871. struct m68hc11_opcode_def *opc;
  872. struct m68hc11_opcode *opcode;
  873. char buf[128];
  874. opc = (struct m68hc11_opcode_def *) str_hash_find (m68hc11_hash, name);
  875. if (opc == NULL)
  876. {
  877. as_bad (_("Instruction `%s' is not recognized."), name);
  878. return;
  879. }
  880. opcode = opc->opcode;
  881. as_bad (_("Instruction formats for `%s':"), name);
  882. do
  883. {
  884. char *fmt;
  885. fmt = print_opcode_format (opcode, 0);
  886. sprintf (buf, "\t%-5.5s %s", opcode->name, fmt);
  887. as_bad ("%s", buf);
  888. opcode++;
  889. }
  890. while (strcmp (opcode->name, name) == 0);
  891. }
  892. /* Analysis of 68HC11 and 68HC12 operands. */
  893. /* reg_name_search() finds the register number given its name.
  894. Returns the register number or REG_NONE on failure. */
  895. static register_id
  896. reg_name_search (char *name)
  897. {
  898. if (strcasecmp (name, "x") == 0 || strcasecmp (name, "ix") == 0)
  899. return REG_X;
  900. if (strcasecmp (name, "y") == 0 || strcasecmp (name, "iy") == 0)
  901. return REG_Y;
  902. if (strcasecmp (name, "a") == 0)
  903. return REG_A;
  904. if (strcasecmp (name, "b") == 0)
  905. return REG_B;
  906. if (strcasecmp (name, "d") == 0)
  907. return REG_D;
  908. if (strcasecmp (name, "sp") == 0)
  909. return REG_SP;
  910. if (strcasecmp (name, "pc") == 0)
  911. return REG_PC;
  912. if (strcasecmp (name, "ccr") == 0)
  913. return REG_CCR;
  914. /* XGATE */
  915. if (strcasecmp (name, "r0") == 0)
  916. return REG_R0;
  917. if (strcasecmp (name, "r1") == 0)
  918. return REG_R1;
  919. if (strcasecmp (name, "r2") == 0)
  920. return REG_R2;
  921. if (strcasecmp (name, "r3") == 0)
  922. return REG_R3;
  923. if (strcasecmp (name, "r4") == 0)
  924. return REG_R4;
  925. if (strcasecmp (name, "r5") == 0)
  926. return REG_R5;
  927. if (strcasecmp (name, "r6") == 0)
  928. return REG_R6;
  929. if (strcasecmp (name, "r7") == 0)
  930. return REG_R7;
  931. if (strcasecmp (name, "sp") == 0)
  932. return REG_SP_XG;
  933. if (strcasecmp (name, "pc") == 0)
  934. return REG_PC_XG;
  935. if (strcasecmp (name, "ccr") == 0)
  936. return REG_CCR_XG;
  937. return REG_NONE;
  938. }
  939. static char *
  940. skip_whites (char *p)
  941. {
  942. while (*p == ' ' || *p == '\t')
  943. p++;
  944. return p;
  945. }
  946. /* Check the string at input_line_pointer
  947. to see if it is a valid register name. */
  948. static register_id
  949. register_name (void)
  950. {
  951. register_id reg_number;
  952. char c, *p = input_line_pointer;
  953. if (!is_name_beginner (*p++))
  954. return REG_NONE;
  955. while (is_part_of_name (*p++))
  956. continue;
  957. c = *--p;
  958. if (c)
  959. *p++ = 0;
  960. /* Look to see if it's in the register table. */
  961. reg_number = reg_name_search (input_line_pointer);
  962. if (reg_number != REG_NONE)
  963. {
  964. if (c)
  965. *--p = c;
  966. input_line_pointer = p;
  967. return reg_number;
  968. }
  969. if (c)
  970. *--p = c;
  971. return reg_number;
  972. }
  973. #define M6811_OP_CALL_ADDR 0x00800000
  974. #define M6811_OP_PAGE_ADDR 0x04000000
  975. /* Parse a string of operands and return an array of expressions.
  976. Operand mode[0] mode[1] exp[0] exp[1]
  977. #n M6811_OP_IMM16 - O_*
  978. *<exp> M6811_OP_DIRECT - O_*
  979. .{+-}<exp> M6811_OP_JUMP_REL - O_*
  980. <exp> M6811_OP_IND16 - O_*
  981. ,r N,r M6812_OP_IDX M6812_OP_REG O_constant O_register
  982. n,-r M6812_PRE_DEC M6812_OP_REG O_constant O_register
  983. n,+r M6812_PRE_INC " "
  984. n,r- M6812_POST_DEC " "
  985. n,r+ M6812_POST_INC " "
  986. A,r B,r D,r M6811_OP_REG M6812_OP_REG O_register O_register
  987. [D,r] M6811_OP_D_IDX M6812_OP_REG O_register O_register
  988. [n,r] M6811_OP_D_IDX_2 M6812_OP_REG O_constant O_register */
  989. static int
  990. get_operand (operand *oper, int which, long opmode)
  991. {
  992. char *p = input_line_pointer;
  993. int mode;
  994. register_id reg;
  995. oper->exp.X_op = O_absent;
  996. oper->reg1 = REG_NONE;
  997. oper->reg2 = REG_NONE;
  998. mode = M6811_OP_NONE;
  999. p = skip_whites (p);
  1000. if (*p == 0 || *p == '\n' || *p == '\r')
  1001. {
  1002. input_line_pointer = p;
  1003. return 0;
  1004. }
  1005. if (*p == '*' && (opmode & (M6811_OP_DIRECT | M6811_OP_IND16)))
  1006. {
  1007. mode = M6811_OP_DIRECT;
  1008. p++;
  1009. }
  1010. else if (*p == '#')
  1011. {
  1012. if (!(opmode & (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK)))
  1013. {
  1014. as_bad (_("Immediate operand is not allowed for operand %d."),
  1015. which);
  1016. return -1;
  1017. }
  1018. mode = M6811_OP_IMM16;
  1019. p++;
  1020. if (startswith (p, "%hi"))
  1021. {
  1022. p += 3;
  1023. mode |= M6811_OP_HIGH_ADDR;
  1024. }
  1025. else if (startswith (p, "%lo"))
  1026. {
  1027. p += 3;
  1028. mode |= M6811_OP_LOW_ADDR;
  1029. }
  1030. /* %page modifier is used to obtain only the page number
  1031. of the address of a function. */
  1032. else if (startswith (p, "%page"))
  1033. {
  1034. p += 5;
  1035. mode |= M6811_OP_PAGE_ADDR;
  1036. }
  1037. /* %addr modifier is used to obtain the physical address part
  1038. of the function (16-bit). For 68HC12 the function will be
  1039. mapped in the 16K window at 0x8000 and the value will be
  1040. within that window (although the function address may not fit
  1041. in 16-bit). See bfd/elf32-m68hc12.c for the translation. */
  1042. else if (startswith (p, "%addr"))
  1043. {
  1044. p += 5;
  1045. mode |= M6811_OP_CALL_ADDR;
  1046. }
  1047. }
  1048. else if (*p == '.' && (p[1] == '+' || p[1] == '-'))
  1049. {
  1050. p++;
  1051. mode = M6811_OP_JUMP_REL;
  1052. }
  1053. else if (*p == '[')
  1054. {
  1055. if (current_architecture & cpu6811)
  1056. as_bad (_("Indirect indexed addressing is not valid for 68HC11."));
  1057. p++;
  1058. mode = M6812_OP_D_IDX;
  1059. p = skip_whites (p);
  1060. }
  1061. else if (*p == ',') /* Special handling of ,x and ,y. */
  1062. {
  1063. p++;
  1064. input_line_pointer = p;
  1065. reg = register_name ();
  1066. if (reg != REG_NONE)
  1067. {
  1068. oper->reg1 = reg;
  1069. oper->exp.X_op = O_constant;
  1070. oper->exp.X_add_number = 0;
  1071. oper->mode = M6812_OP_IDX;
  1072. return 1;
  1073. }
  1074. as_bad (_("Spurious `,' or bad indirect register addressing mode."));
  1075. return -1;
  1076. }
  1077. /* Handle 68HC12 page specification in 'call foo,%page(bar)'. */
  1078. else if ((opmode & M6812_OP_PAGE) && startswith (p, "%page"))
  1079. {
  1080. p += 5;
  1081. mode = M6811_OP_PAGE_ADDR | M6812_OP_PAGE | M6811_OP_IND16;
  1082. }
  1083. input_line_pointer = p;
  1084. if (mode == M6811_OP_NONE || mode == M6812_OP_D_IDX)
  1085. reg = register_name ();
  1086. else
  1087. reg = REG_NONE;
  1088. if (reg != REG_NONE)
  1089. {
  1090. p = skip_whites (input_line_pointer);
  1091. if (*p == ']' && mode == M6812_OP_D_IDX)
  1092. {
  1093. as_bad
  1094. (_("Missing second register or offset for indexed-indirect mode."));
  1095. return -1;
  1096. }
  1097. oper->reg1 = reg;
  1098. oper->mode = mode | M6812_OP_REG;
  1099. if (*p != ',')
  1100. {
  1101. if (mode == M6812_OP_D_IDX)
  1102. {
  1103. as_bad (_("Missing second register for indexed-indirect mode."));
  1104. return -1;
  1105. }
  1106. return 1;
  1107. }
  1108. p++;
  1109. input_line_pointer = p;
  1110. reg = register_name ();
  1111. if (reg != REG_NONE)
  1112. {
  1113. p = skip_whites (input_line_pointer);
  1114. if (mode == M6812_OP_D_IDX)
  1115. {
  1116. if (*p != ']')
  1117. {
  1118. as_bad (_("Missing `]' to close indexed-indirect mode."));
  1119. return -1;
  1120. }
  1121. p++;
  1122. oper->mode = M6812_OP_D_IDX;
  1123. }
  1124. input_line_pointer = p;
  1125. oper->reg2 = reg;
  1126. return 1;
  1127. }
  1128. return 1;
  1129. }
  1130. /* In MRI mode, isolate the operand because we can't distinguish
  1131. operands from comments. */
  1132. if (flag_mri)
  1133. {
  1134. char c = 0;
  1135. p = skip_whites (p);
  1136. while (*p && *p != ' ' && *p != '\t')
  1137. p++;
  1138. if (*p)
  1139. {
  1140. c = *p;
  1141. *p = 0;
  1142. }
  1143. /* Parse as an expression. */
  1144. expression (&oper->exp);
  1145. if (c)
  1146. {
  1147. *p = c;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. expression (&oper->exp);
  1153. }
  1154. if (oper->exp.X_op == O_illegal)
  1155. {
  1156. as_bad (_("Illegal operand."));
  1157. return -1;
  1158. }
  1159. else if (oper->exp.X_op == O_absent)
  1160. {
  1161. as_bad (_("Missing operand."));
  1162. return -1;
  1163. }
  1164. p = input_line_pointer;
  1165. if (mode == M6811_OP_NONE || mode == M6811_OP_DIRECT
  1166. || mode == M6812_OP_D_IDX)
  1167. {
  1168. p = skip_whites (input_line_pointer);
  1169. if (*p == ',')
  1170. {
  1171. int possible_mode = M6811_OP_NONE;
  1172. char *old_input_line;
  1173. old_input_line = p;
  1174. p++;
  1175. /* 68HC12 pre increment or decrement. */
  1176. if (mode == M6811_OP_NONE)
  1177. {
  1178. if (*p == '-')
  1179. {
  1180. possible_mode = M6812_PRE_DEC;
  1181. p++;
  1182. }
  1183. else if (*p == '+')
  1184. {
  1185. possible_mode = M6812_PRE_INC;
  1186. p++;
  1187. }
  1188. p = skip_whites (p);
  1189. }
  1190. input_line_pointer = p;
  1191. reg = register_name ();
  1192. /* Backtrack if we have a valid constant expression and
  1193. it does not correspond to the offset of the 68HC12 indexed
  1194. addressing mode (as in N,x). */
  1195. if (reg == REG_NONE && mode == M6811_OP_NONE
  1196. && possible_mode != M6811_OP_NONE)
  1197. {
  1198. oper->mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
  1199. input_line_pointer = skip_whites (old_input_line);
  1200. return 1;
  1201. }
  1202. if (possible_mode != M6811_OP_NONE)
  1203. mode = possible_mode;
  1204. if ((current_architecture & cpu6811)
  1205. && possible_mode != M6811_OP_NONE)
  1206. as_bad (_("Pre-increment mode is not valid for 68HC11"));
  1207. /* Backtrack. */
  1208. if (which == 0 && opmode & M6812_OP_IDX_P2
  1209. && reg != REG_X && reg != REG_Y
  1210. && reg != REG_PC && reg != REG_SP)
  1211. {
  1212. reg = REG_NONE;
  1213. input_line_pointer = p;
  1214. }
  1215. if (reg == REG_NONE && mode != M6811_OP_DIRECT
  1216. && !(mode == M6811_OP_NONE && opmode & M6811_OP_IND16))
  1217. {
  1218. as_bad (_("Wrong register in register indirect mode."));
  1219. return -1;
  1220. }
  1221. if (mode == M6812_OP_D_IDX)
  1222. {
  1223. p = skip_whites (input_line_pointer);
  1224. if (*p++ != ']')
  1225. {
  1226. as_bad (_("Missing `]' to close register indirect operand."));
  1227. return -1;
  1228. }
  1229. input_line_pointer = p;
  1230. oper->reg1 = reg;
  1231. oper->mode = M6812_OP_D_IDX_2;
  1232. return 1;
  1233. }
  1234. if (reg != REG_NONE)
  1235. {
  1236. oper->reg1 = reg;
  1237. if (mode == M6811_OP_NONE)
  1238. {
  1239. p = input_line_pointer;
  1240. if (*p == '-')
  1241. {
  1242. mode = M6812_POST_DEC;
  1243. p++;
  1244. if (current_architecture & cpu6811)
  1245. as_bad
  1246. (_("Post-decrement mode is not valid for 68HC11."));
  1247. }
  1248. else if (*p == '+')
  1249. {
  1250. mode = M6812_POST_INC;
  1251. p++;
  1252. if (current_architecture & cpu6811)
  1253. as_bad
  1254. (_("Post-increment mode is not valid for 68HC11."));
  1255. }
  1256. else
  1257. mode = M6812_OP_IDX;
  1258. input_line_pointer = p;
  1259. }
  1260. else
  1261. mode |= M6812_OP_IDX;
  1262. oper->mode = mode;
  1263. return 1;
  1264. }
  1265. input_line_pointer = old_input_line;
  1266. }
  1267. if (mode == M6812_OP_D_IDX_2)
  1268. {
  1269. as_bad (_("Invalid indexed indirect mode."));
  1270. return -1;
  1271. }
  1272. }
  1273. /* If the mode is not known until now, this is either a label
  1274. or an indirect address. */
  1275. if (mode == M6811_OP_NONE)
  1276. mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
  1277. p = input_line_pointer;
  1278. while (*p == ' ' || *p == '\t')
  1279. p++;
  1280. input_line_pointer = p;
  1281. oper->mode = mode;
  1282. return 1;
  1283. }
  1284. #define M6812_AUTO_INC_DEC (M6812_PRE_INC | M6812_PRE_DEC \
  1285. | M6812_POST_INC | M6812_POST_DEC)
  1286. /* Checks that the number 'num' fits for a given mode. */
  1287. static int
  1288. check_range (long num, int mode)
  1289. {
  1290. if (current_architecture == cpuxgate)
  1291. {
  1292. switch (mode)
  1293. {
  1294. case M68XG_OP_IMM3:
  1295. return (num >= 0 && num <= 7) ? 1 : 0;
  1296. case M68XG_OP_R_IMM4:
  1297. return (num >= 0 && num <= 15) ? 1 : 0;
  1298. case M68XG_OP_R_R_OFFS5:
  1299. return (num >= 0 && num <= 31) ? 1 : 0;
  1300. case M68XG_OP_R_IMM8:
  1301. return (num >= 0 && num <= 255) ? 1 : 0;
  1302. case M68XG_OP_R_IMM16:
  1303. return (num >= 0 && num <= 65535) ? 1 : 0;
  1304. case M68XG_OP_B_MARKER:
  1305. return (num >= -512 && num <= 511) ? 1 : 0;
  1306. case M68XG_OP_BRA_MARKER:
  1307. return (num >= -1024 && num <= 1023) ? 1 : 0;
  1308. default:
  1309. return 0;
  1310. }
  1311. }
  1312. else
  1313. {
  1314. /* Auto increment and decrement are ok for [-8..8] without 0. */
  1315. if (mode & M6812_AUTO_INC_DEC)
  1316. return (num != 0 && num <= 8 && num >= -8);
  1317. /* The 68HC12 supports 5, 9 and 16-bit offsets. */
  1318. if (mode & (M6812_INDEXED_IND | M6812_INDEXED | M6812_OP_IDX))
  1319. mode = M6811_OP_IND16;
  1320. if (mode & M6812_OP_JUMP_REL16)
  1321. mode = M6811_OP_IND16;
  1322. mode &= ~M6811_OP_BRANCH;
  1323. switch (mode)
  1324. {
  1325. case M6811_OP_IX:
  1326. case M6811_OP_IY:
  1327. case M6811_OP_DIRECT:
  1328. return (num >= 0 && num <= 255) ? 1 : 0;
  1329. case M6811_OP_BITMASK:
  1330. case M6811_OP_IMM8:
  1331. case M6812_OP_PAGE:
  1332. return (((num & 0xFFFFFF00) == 0) || ((num & 0xFFFFFF00) == 0xFFFFFF00))
  1333. ? 1 : 0;
  1334. case M6811_OP_JUMP_REL:
  1335. return (num >= -128 && num <= 127) ? 1 : 0;
  1336. case M6811_OP_IND16:
  1337. case M6811_OP_IND16 | M6812_OP_PAGE:
  1338. case M6811_OP_IMM16:
  1339. return (((num & 0xFFFF0000) == 0) || ((num & 0xFFFF0000) == 0xFFFF0000))
  1340. ? 1 : 0;
  1341. case M6812_OP_IBCC_MARKER:
  1342. case M6812_OP_TBCC_MARKER:
  1343. case M6812_OP_DBCC_MARKER:
  1344. return (num >= -256 && num <= 255) ? 1 : 0;
  1345. case M6812_OP_TRAP_ID:
  1346. return ((num >= 0x30 && num <= 0x39)
  1347. || (num >= 0x40 && num <= 0x0ff)) ? 1 : 0;
  1348. default:
  1349. return 0;
  1350. }
  1351. }
  1352. }
  1353. /* Gas fixup generation. */
  1354. /* Put a 1 byte expression described by 'oper'. If this expression contains
  1355. unresolved symbols, generate an 8-bit fixup. */
  1356. static void
  1357. fixup8 (expressionS *oper, int mode, int opmode)
  1358. {
  1359. char *f;
  1360. f = frag_more (1);
  1361. if (oper->X_op == O_constant)
  1362. {
  1363. if (mode & M6812_OP_TRAP_ID
  1364. && !check_range (oper->X_add_number, M6812_OP_TRAP_ID))
  1365. {
  1366. static char trap_id_warn_once = 0;
  1367. as_bad (_("Trap id `%ld' is out of range."), oper->X_add_number);
  1368. if (trap_id_warn_once == 0)
  1369. {
  1370. trap_id_warn_once = 1;
  1371. as_bad (_("Trap id must be within [0x30..0x39] or [0x40..0xff]."));
  1372. }
  1373. }
  1374. if (!(mode & M6812_OP_TRAP_ID)
  1375. && !check_range (oper->X_add_number, mode))
  1376. {
  1377. as_bad (_("Operand out of 8-bit range: `%ld'."), oper->X_add_number);
  1378. }
  1379. number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
  1380. }
  1381. else if (oper->X_op != O_register)
  1382. {
  1383. if (mode & M6812_OP_TRAP_ID)
  1384. as_bad (_("The trap id must be a constant."));
  1385. if (mode == M6811_OP_JUMP_REL)
  1386. {
  1387. fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1388. oper, true, BFD_RELOC_8_PCREL);
  1389. }
  1390. else
  1391. {
  1392. fixS *fixp;
  1393. bfd_reloc_code_real_type reloc;
  1394. /* Now create an 8-bit fixup. If there was some %hi, %lo
  1395. or %page modifier, generate the reloc accordingly. */
  1396. if (opmode & M6811_OP_HIGH_ADDR)
  1397. reloc = BFD_RELOC_M68HC11_HI8;
  1398. else if (opmode & M6811_OP_LOW_ADDR)
  1399. reloc = BFD_RELOC_M68HC11_LO8;
  1400. else if (opmode & M6811_OP_PAGE_ADDR)
  1401. reloc = BFD_RELOC_M68HC11_PAGE;
  1402. else
  1403. reloc = BFD_RELOC_8;
  1404. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1405. oper, false, reloc);
  1406. if (reloc != BFD_RELOC_8)
  1407. fixp->fx_no_overflow = 1;
  1408. }
  1409. number_to_chars_bigendian (f, 0, 1);
  1410. }
  1411. else
  1412. {
  1413. as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
  1414. }
  1415. }
  1416. /* Put a 2 byte expression described by 'oper'. If this expression contains
  1417. unresolved symbols, generate a 16-bit fixup. */
  1418. static void
  1419. fixup16 (expressionS *oper, int mode, int opmode ATTRIBUTE_UNUSED)
  1420. {
  1421. char *f;
  1422. f = frag_more (2);
  1423. if (oper->X_op == O_constant)
  1424. {
  1425. if (!check_range (oper->X_add_number, mode))
  1426. {
  1427. as_bad (_("Operand out of 16-bit range: `%ld'."),
  1428. oper->X_add_number);
  1429. }
  1430. number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFF, 2);
  1431. }
  1432. else if (oper->X_op != O_register)
  1433. {
  1434. fixS *fixp;
  1435. bfd_reloc_code_real_type reloc;
  1436. if ((opmode & M6811_OP_CALL_ADDR) && (mode & M6811_OP_IMM16))
  1437. reloc = BFD_RELOC_M68HC11_LO16;
  1438. else if (mode & M6812_OP_JUMP_REL16)
  1439. reloc = BFD_RELOC_16_PCREL;
  1440. else if (mode & M6812_OP_PAGE)
  1441. reloc = BFD_RELOC_M68HC11_LO16;
  1442. else
  1443. reloc = BFD_RELOC_16;
  1444. /* Now create a 16-bit fixup. */
  1445. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
  1446. oper,
  1447. reloc == BFD_RELOC_16_PCREL,
  1448. reloc);
  1449. number_to_chars_bigendian (f, 0, 2);
  1450. if (reloc == BFD_RELOC_M68HC11_LO16)
  1451. fixp->fx_no_overflow = 1;
  1452. }
  1453. else
  1454. {
  1455. as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
  1456. }
  1457. }
  1458. /* Put a 3 byte expression described by 'oper'. If this expression contains
  1459. unresolved symbols, generate a 24-bit fixup. */
  1460. static void
  1461. fixup24 (expressionS *oper, int mode, int opmode ATTRIBUTE_UNUSED)
  1462. {
  1463. char *f;
  1464. f = frag_more (3);
  1465. if (oper->X_op == O_constant)
  1466. {
  1467. if (!check_range (oper->X_add_number, mode))
  1468. {
  1469. as_bad (_("Operand out of 16-bit range: `%ld'."),
  1470. oper->X_add_number);
  1471. }
  1472. number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFFFF, 3);
  1473. }
  1474. else if (oper->X_op != O_register)
  1475. {
  1476. /* Now create a 24-bit fixup. */
  1477. fix_new_exp (frag_now, f - frag_now->fr_literal, 3,
  1478. oper, false, BFD_RELOC_M68HC11_24);
  1479. number_to_chars_bigendian (f, 0, 3);
  1480. }
  1481. else
  1482. {
  1483. as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
  1484. }
  1485. }
  1486. /* XGATE Put a 1 byte expression described by 'oper'. If this expression
  1487. contains unresolved symbols, generate an 8-bit fixup. */
  1488. static void
  1489. fixup8_xg (expressionS *oper, int mode, int opmode)
  1490. {
  1491. char *f;
  1492. f = frag_more (1);
  1493. if (oper->X_op == O_constant)
  1494. {
  1495. fixS *fixp;
  1496. bfd_reloc_code_real_type reloc;
  1497. if ((opmode & M6811_OP_HIGH_ADDR) || (opmode & M6811_OP_LOW_ADDR))
  1498. {
  1499. if (opmode & M6811_OP_HIGH_ADDR)
  1500. reloc = BFD_RELOC_M68HC11_HI8;
  1501. else
  1502. reloc = BFD_RELOC_M68HC11_LO8;
  1503. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1504. oper, false, reloc);
  1505. fixp->fx_no_overflow = 1;
  1506. number_to_chars_bigendian (f, 0, 1);
  1507. }
  1508. else
  1509. {
  1510. if (!(check_range (oper->X_add_number, mode)))
  1511. as_bad (_("Operand out of 8-bit range: `%ld'."),
  1512. oper->X_add_number);
  1513. number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
  1514. }
  1515. }
  1516. else if (oper->X_op != O_register)
  1517. {
  1518. if (mode == M68XG_OP_REL9)
  1519. {
  1520. /* Future improvement:
  1521. This fixup/reloc isn't adding on constants to symbols. */
  1522. fix_new_exp (frag_now, f - frag_now->fr_literal -1, 2,
  1523. oper, true, BFD_RELOC_M68HC12_9_PCREL);
  1524. }
  1525. else if (mode == M68XG_OP_REL10)
  1526. {
  1527. /* Future improvement:
  1528. This fixup/reloc isn't adding on constants to symbols. */
  1529. fix_new_exp (frag_now, f - frag_now->fr_literal -1, 2,
  1530. oper, true, BFD_RELOC_M68HC12_10_PCREL);
  1531. }
  1532. else
  1533. {
  1534. fixS *fixp;
  1535. bfd_reloc_code_real_type reloc;
  1536. /* Now create an 8-bit fixup. If there was some %hi, %lo
  1537. modifier, generate the reloc accordingly. */
  1538. if (opmode & M6811_OP_HIGH_ADDR)
  1539. reloc = BFD_RELOC_M68HC11_HI8;
  1540. else if (opmode & M6811_OP_LOW_ADDR)
  1541. reloc = BFD_RELOC_M68HC11_LO8;
  1542. else
  1543. reloc = BFD_RELOC_8;
  1544. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1545. oper, false, reloc);
  1546. if (reloc != BFD_RELOC_8)
  1547. fixp->fx_no_overflow = 1;
  1548. }
  1549. number_to_chars_bigendian (f, 0, 1);
  1550. }
  1551. else
  1552. as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
  1553. }
  1554. /* 68HC11 and 68HC12 code generation. */
  1555. /* Translate the short branch/bsr instruction into a long branch. */
  1556. static unsigned char
  1557. convert_branch (unsigned char code)
  1558. {
  1559. if (IS_OPCODE (code, M6812_BSR))
  1560. return M6812_JSR;
  1561. else if (IS_OPCODE (code, M6811_BSR))
  1562. return M6811_JSR;
  1563. else if (IS_OPCODE (code, M6811_BRA))
  1564. return (current_architecture & cpu6812) ? M6812_JMP : M6811_JMP;
  1565. else
  1566. as_fatal (_("Unexpected branch conversion with `%x'"), code);
  1567. /* Keep gcc happy. */
  1568. return M6811_JSR;
  1569. }
  1570. /* Start a new insn that contains at least 'size' bytes. Record the
  1571. line information of that insn in the dwarf2 debug sections. */
  1572. static char *
  1573. m68hc11_new_insn (int size)
  1574. {
  1575. char *f;
  1576. f = frag_more (size);
  1577. dwarf2_emit_insn (size);
  1578. return f;
  1579. }
  1580. /* Builds a jump instruction (bra, bcc, bsr). */
  1581. static void
  1582. build_jump_insn (struct m68hc11_opcode *opcode, operand operands[],
  1583. int nb_operands, int jmp_mode)
  1584. {
  1585. unsigned char code;
  1586. char *f;
  1587. unsigned long n;
  1588. /* The relative branch conversion is not supported for
  1589. brclr and brset. */
  1590. gas_assert ((opcode->format & M6811_OP_BITMASK) == 0);
  1591. gas_assert (nb_operands == 1);
  1592. gas_assert (operands[0].reg1 == REG_NONE && operands[0].reg2 == REG_NONE);
  1593. code = opcode->opcode;
  1594. n = operands[0].exp.X_add_number;
  1595. /* Turn into a long branch:
  1596. - when force long branch option (and not for jbcc pseudos),
  1597. - when jbcc and the constant is out of -128..127 range,
  1598. - when branch optimization is allowed and branch out of range. */
  1599. if ((jmp_mode == 0 && flag_force_long_jumps)
  1600. || (operands[0].exp.X_op == O_constant
  1601. && (!check_range (n, opcode->format) &&
  1602. (jmp_mode == 1 || flag_fixed_branches == 0))))
  1603. {
  1604. fix_new (frag_now, frag_now_fix (), 0,
  1605. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1606. if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
  1607. {
  1608. code = convert_branch (code);
  1609. f = m68hc11_new_insn (1);
  1610. number_to_chars_bigendian (f, code, 1);
  1611. }
  1612. else if (current_architecture & cpu6812)
  1613. {
  1614. /* 68HC12: translate the bcc into a lbcc. */
  1615. f = m68hc11_new_insn (2);
  1616. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1617. number_to_chars_bigendian (f + 1, code, 1);
  1618. fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16,
  1619. M6812_OP_JUMP_REL16);
  1620. return;
  1621. }
  1622. else
  1623. {
  1624. /* 68HC11: translate the bcc into b!cc +3; jmp <L>. */
  1625. f = m68hc11_new_insn (3);
  1626. code ^= 1;
  1627. number_to_chars_bigendian (f, code, 1);
  1628. number_to_chars_bigendian (f + 1, 3, 1);
  1629. number_to_chars_bigendian (f + 2, M6811_JMP, 1);
  1630. }
  1631. fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
  1632. return;
  1633. }
  1634. /* Branch with a constant that must fit in 8-bits. */
  1635. if (operands[0].exp.X_op == O_constant)
  1636. {
  1637. if (!check_range (n, opcode->format))
  1638. {
  1639. as_bad (_("Operand out of range for a relative branch: `%ld'"),
  1640. n);
  1641. }
  1642. else if (opcode->format & M6812_OP_JUMP_REL16)
  1643. {
  1644. f = m68hc11_new_insn (4);
  1645. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1646. number_to_chars_bigendian (f + 1, code, 1);
  1647. number_to_chars_bigendian (f + 2, n & 0x0ffff, 2);
  1648. }
  1649. else
  1650. {
  1651. f = m68hc11_new_insn (2);
  1652. number_to_chars_bigendian (f, code, 1);
  1653. number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
  1654. }
  1655. }
  1656. else if (opcode->format & M6812_OP_JUMP_REL16)
  1657. {
  1658. fix_new (frag_now, frag_now_fix (), 0,
  1659. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1660. f = m68hc11_new_insn (2);
  1661. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1662. number_to_chars_bigendian (f + 1, code, 1);
  1663. fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16, M6812_OP_JUMP_REL16);
  1664. }
  1665. else
  1666. {
  1667. char *op;
  1668. fix_new (frag_now, frag_now_fix (), 0,
  1669. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1670. /* Branch offset must fit in 8-bits, don't do some relax. */
  1671. if (jmp_mode == 0 && flag_fixed_branches)
  1672. {
  1673. op = m68hc11_new_insn (1);
  1674. number_to_chars_bigendian (op, code, 1);
  1675. fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
  1676. }
  1677. /* bra/bsr made be changed into jmp/jsr. */
  1678. else if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
  1679. {
  1680. /* Allocate worst case storage. */
  1681. op = m68hc11_new_insn (3);
  1682. number_to_chars_bigendian (op, code, 1);
  1683. number_to_chars_bigendian (op + 1, 0, 1);
  1684. frag_variant (rs_machine_dependent, 1, 1,
  1685. ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
  1686. operands[0].exp.X_add_symbol, (offsetT) n,
  1687. op);
  1688. }
  1689. else if (current_architecture & cpu6812)
  1690. {
  1691. op = m68hc11_new_insn (2);
  1692. number_to_chars_bigendian (op, code, 1);
  1693. number_to_chars_bigendian (op + 1, 0, 1);
  1694. frag_var (rs_machine_dependent, 2, 2,
  1695. ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_UNDF),
  1696. operands[0].exp.X_add_symbol, (offsetT) n, op);
  1697. }
  1698. else
  1699. {
  1700. op = m68hc11_new_insn (2);
  1701. number_to_chars_bigendian (op, code, 1);
  1702. number_to_chars_bigendian (op + 1, 0, 1);
  1703. frag_var (rs_machine_dependent, 3, 3,
  1704. ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF),
  1705. operands[0].exp.X_add_symbol, (offsetT) n, op);
  1706. }
  1707. }
  1708. }
  1709. /* Builds a dbne/dbeq/tbne/tbeq instruction. */
  1710. static void
  1711. build_dbranch_insn (struct m68hc11_opcode *opcode, operand operands[],
  1712. int nb_operands, int jmp_mode)
  1713. {
  1714. unsigned char code;
  1715. char *f;
  1716. unsigned long n;
  1717. /* The relative branch conversion is not supported for
  1718. brclr and brset. */
  1719. gas_assert ((opcode->format & M6811_OP_BITMASK) == 0);
  1720. gas_assert (nb_operands == 2);
  1721. gas_assert (operands[0].reg1 != REG_NONE);
  1722. code = opcode->opcode & 0x0FF;
  1723. f = m68hc11_new_insn (1);
  1724. number_to_chars_bigendian (f, code, 1);
  1725. n = operands[1].exp.X_add_number;
  1726. code = operands[0].reg1;
  1727. if (operands[0].reg1 == REG_NONE || operands[0].reg1 == REG_CCR
  1728. || operands[0].reg1 == REG_PC)
  1729. as_bad (_("Invalid register for dbcc/tbcc instruction."));
  1730. if (opcode->format & M6812_OP_IBCC_MARKER)
  1731. code |= 0x80;
  1732. else if (opcode->format & M6812_OP_TBCC_MARKER)
  1733. code |= 0x40;
  1734. if (!(opcode->format & M6812_OP_EQ_MARKER))
  1735. code |= 0x20;
  1736. /* Turn into a long branch:
  1737. - when force long branch option (and not for jbcc pseudos),
  1738. - when jdbcc and the constant is out of -256..255 range,
  1739. - when branch optimization is allowed and branch out of range. */
  1740. if ((jmp_mode == 0 && flag_force_long_jumps)
  1741. || (operands[1].exp.X_op == O_constant
  1742. && (!check_range (n, M6812_OP_IBCC_MARKER) &&
  1743. (jmp_mode == 1 || flag_fixed_branches == 0))))
  1744. {
  1745. f = frag_more (2);
  1746. code ^= 0x20;
  1747. number_to_chars_bigendian (f, code, 1);
  1748. number_to_chars_bigendian (f + 1, M6812_JMP, 1);
  1749. fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
  1750. return;
  1751. }
  1752. /* Branch with a constant that must fit in 9-bits. */
  1753. if (operands[1].exp.X_op == O_constant)
  1754. {
  1755. if (!check_range (n, M6812_OP_IBCC_MARKER))
  1756. {
  1757. as_bad (_("Operand out of range for a relative branch: `%ld'"),
  1758. n);
  1759. }
  1760. else
  1761. {
  1762. if ((long) n < 0)
  1763. code |= 0x10;
  1764. f = frag_more (2);
  1765. number_to_chars_bigendian (f, code, 1);
  1766. number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
  1767. }
  1768. }
  1769. else
  1770. {
  1771. /* Branch offset must fit in 8-bits, don't do some relax. */
  1772. if (jmp_mode == 0 && flag_fixed_branches)
  1773. {
  1774. fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
  1775. }
  1776. else
  1777. {
  1778. f = frag_more (2);
  1779. number_to_chars_bigendian (f, code, 1);
  1780. number_to_chars_bigendian (f + 1, 0, 1);
  1781. frag_var (rs_machine_dependent, 3, 3,
  1782. ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_UNDF),
  1783. operands[1].exp.X_add_symbol, (offsetT) n, f);
  1784. }
  1785. }
  1786. }
  1787. #define OP_EXTENDED (M6811_OP_PAGE2 | M6811_OP_PAGE3 | M6811_OP_PAGE4)
  1788. /* Assemble the post index byte for 68HC12 extended addressing modes. */
  1789. static int
  1790. build_indexed_byte (operand *op, int format ATTRIBUTE_UNUSED, int move_insn)
  1791. {
  1792. unsigned char byte = 0;
  1793. char *f;
  1794. int mode;
  1795. long val;
  1796. val = op->exp.X_add_number;
  1797. mode = op->mode;
  1798. if (mode & M6812_AUTO_INC_DEC)
  1799. {
  1800. byte = 0x20;
  1801. if (mode & (M6812_POST_INC | M6812_POST_DEC))
  1802. byte |= 0x10;
  1803. if (op->exp.X_op == O_constant)
  1804. {
  1805. if (!check_range (val, mode))
  1806. as_bad (_("Increment/decrement value is out of range: `%ld'."),
  1807. val);
  1808. if (mode & (M6812_POST_INC | M6812_PRE_INC))
  1809. byte |= (val - 1) & 0x07;
  1810. else
  1811. byte |= (8 - ((val) & 7)) | 0x8;
  1812. }
  1813. switch (op->reg1)
  1814. {
  1815. case REG_NONE:
  1816. as_fatal (_("Expecting a register."));
  1817. case REG_X:
  1818. byte |= 0;
  1819. break;
  1820. case REG_Y:
  1821. byte |= 0x40;
  1822. break;
  1823. case REG_SP:
  1824. byte |= 0x80;
  1825. break;
  1826. default:
  1827. as_bad (_("Invalid register for post/pre increment."));
  1828. break;
  1829. }
  1830. f = frag_more (1);
  1831. number_to_chars_bigendian (f, byte, 1);
  1832. return 1;
  1833. }
  1834. if (mode & (M6812_OP_IDX | M6812_OP_D_IDX_2))
  1835. {
  1836. switch (op->reg1)
  1837. {
  1838. case REG_X:
  1839. byte = 0;
  1840. break;
  1841. case REG_Y:
  1842. byte = 1;
  1843. break;
  1844. case REG_SP:
  1845. byte = 2;
  1846. break;
  1847. case REG_PC:
  1848. byte = 3;
  1849. break;
  1850. default:
  1851. as_bad (_("Invalid register."));
  1852. break;
  1853. }
  1854. if (op->exp.X_op == O_constant)
  1855. {
  1856. if (!check_range (val, M6812_OP_IDX))
  1857. as_bad (_("Offset out of 16-bit range: %ld."), val);
  1858. if (move_insn && !(val >= -16 && val <= 15)
  1859. && ((!(mode & M6812_OP_IDX) && !(mode & M6812_OP_D_IDX_2))
  1860. || !(current_architecture & cpu9s12x)))
  1861. {
  1862. as_bad (_("Offset out of 5-bit range for movw/movb insn: %ld."),
  1863. val);
  1864. return -1;
  1865. }
  1866. if (val >= -16 && val <= 15 && !(mode & M6812_OP_D_IDX_2))
  1867. {
  1868. byte = byte << 6;
  1869. byte |= val & 0x1f;
  1870. f = frag_more (1);
  1871. number_to_chars_bigendian (f, byte, 1);
  1872. return 1;
  1873. }
  1874. else if (val >= -256 && val <= 255 && !(mode & M6812_OP_D_IDX_2))
  1875. {
  1876. byte = byte << 3;
  1877. byte |= 0xe0;
  1878. if (val < 0)
  1879. byte |= 0x1;
  1880. f = frag_more (2);
  1881. number_to_chars_bigendian (f, byte, 1);
  1882. number_to_chars_bigendian (f + 1, val & 0x0FF, 1);
  1883. return 2;
  1884. }
  1885. else
  1886. {
  1887. byte = byte << 3;
  1888. if (mode & M6812_OP_D_IDX_2)
  1889. byte |= 0xe3;
  1890. else
  1891. byte |= 0xe2;
  1892. f = frag_more (3);
  1893. number_to_chars_bigendian (f, byte, 1);
  1894. number_to_chars_bigendian (f + 1, val & 0x0FFFF, 2);
  1895. return 3;
  1896. }
  1897. }
  1898. if (mode & M6812_OP_D_IDX_2)
  1899. {
  1900. byte = (byte << 3) | 0xe3;
  1901. f = frag_more (1);
  1902. number_to_chars_bigendian (f, byte, 1);
  1903. fixup16 (&op->exp, 0, 0);
  1904. }
  1905. else if (op->reg1 != REG_PC)
  1906. {
  1907. symbolS *sym;
  1908. offsetT off;
  1909. f = frag_more (1);
  1910. number_to_chars_bigendian (f, byte, 1);
  1911. sym = op->exp.X_add_symbol;
  1912. off = op->exp.X_add_number;
  1913. if (op->exp.X_op != O_symbol)
  1914. {
  1915. sym = make_expr_symbol (&op->exp);
  1916. off = 0;
  1917. }
  1918. /* movb/movw cannot be relaxed. */
  1919. if (move_insn)
  1920. {
  1921. if ((mode & M6812_OP_IDX) && (current_architecture & cpu9s12x))
  1922. {
  1923. /* Must treat as a 16bit relocate as size of final result is unknown. */
  1924. byte <<= 3;
  1925. byte |= 0xe2;
  1926. number_to_chars_bigendian (f, byte, 1);
  1927. f = frag_more (2);
  1928. fix_new (frag_now, f - frag_now->fr_literal, 2,
  1929. sym, off, 0, BFD_RELOC_M68HC12_16B);
  1930. return 1;
  1931. }
  1932. else
  1933. {
  1934. /* Non-S12X will fail at relocate stage if offset out of range. */
  1935. byte <<= 6;
  1936. number_to_chars_bigendian (f, byte, 1);
  1937. fix_new (frag_now, f - frag_now->fr_literal, 1,
  1938. sym, off, 0, BFD_RELOC_M68HC12_5B);
  1939. return 1;
  1940. }
  1941. }
  1942. else
  1943. {
  1944. number_to_chars_bigendian (f, byte, 1);
  1945. frag_var (rs_machine_dependent, 2, 2,
  1946. ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_UNDF),
  1947. sym, off, f);
  1948. }
  1949. }
  1950. else
  1951. {
  1952. f = frag_more (1);
  1953. /* movb/movw cannot be relaxed. */
  1954. if (move_insn)
  1955. {
  1956. byte <<= 6;
  1957. number_to_chars_bigendian (f, byte, 1);
  1958. fix_new (frag_now, f - frag_now->fr_literal, 1,
  1959. op->exp.X_add_symbol, op->exp.X_add_number, 0, BFD_RELOC_M68HC12_5B);
  1960. return 1;
  1961. }
  1962. else
  1963. {
  1964. number_to_chars_bigendian (f, byte, 1);
  1965. frag_var (rs_machine_dependent, 2, 2,
  1966. ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_UNDF),
  1967. op->exp.X_add_symbol,
  1968. op->exp.X_add_number, f);
  1969. }
  1970. }
  1971. return 3;
  1972. }
  1973. if (mode & (M6812_OP_REG | M6812_OP_D_IDX))
  1974. {
  1975. if (mode & M6812_OP_D_IDX)
  1976. {
  1977. if (op->reg1 != REG_D)
  1978. as_bad (_("Expecting register D for indexed indirect mode."));
  1979. if ((move_insn) && (!(current_architecture & cpu9s12x)))
  1980. as_bad (_("Indexed indirect mode is not allowed for movb/movw."));
  1981. byte = 0xE7;
  1982. }
  1983. else
  1984. {
  1985. switch (op->reg1)
  1986. {
  1987. case REG_A:
  1988. byte = 0xE4;
  1989. break;
  1990. case REG_B:
  1991. byte = 0xE5;
  1992. break;
  1993. default:
  1994. as_bad (_("Invalid accumulator register."));
  1995. /* Fall through. */
  1996. case REG_D:
  1997. byte = 0xE6;
  1998. break;
  1999. }
  2000. }
  2001. switch (op->reg2)
  2002. {
  2003. case REG_X:
  2004. break;
  2005. case REG_Y:
  2006. byte |= (1 << 3);
  2007. break;
  2008. case REG_SP:
  2009. byte |= (2 << 3);
  2010. break;
  2011. case REG_PC:
  2012. byte |= (3 << 3);
  2013. break;
  2014. default:
  2015. as_bad (_("Invalid indexed register."));
  2016. break;
  2017. }
  2018. f = frag_more (1);
  2019. number_to_chars_bigendian (f, byte, 1);
  2020. return 1;
  2021. }
  2022. fprintf (stderr, "mode = 0x%x\nop->reg1 = 0x%x\nop->reg2 = 0x%x\n",
  2023. mode, op->reg1, op->reg2);
  2024. as_fatal (_("Addressing mode not implemented yet."));
  2025. return 0;
  2026. }
  2027. /* Assemble the 68HC12 register mode byte. */
  2028. static int
  2029. build_reg_mode (operand *op, int format)
  2030. {
  2031. unsigned char byte;
  2032. char *f;
  2033. if ((format & M6812_OP_SEX_MARKER)
  2034. && (op->reg1 != REG_A) && (op->reg1 != REG_B) && (op->reg1 != REG_CCR)
  2035. && (!(current_architecture & cpu9s12x)))
  2036. as_bad (_("Invalid source register for this instruction, use 'tfr'."));
  2037. else if (op->reg1 == REG_NONE || op->reg1 == REG_PC)
  2038. as_bad (_("Invalid source register."));
  2039. if (format & M6812_OP_SEX_MARKER
  2040. && op->reg2 != REG_D
  2041. && op->reg2 != REG_X && op->reg2 != REG_Y && op->reg2 != REG_SP)
  2042. as_bad (_("Invalid destination register for this instruction, use 'tfr'."));
  2043. else if (op->reg2 == REG_NONE || op->reg2 == REG_PC)
  2044. as_bad (_("Invalid destination register."));
  2045. byte = (op->reg1 << 4) | (op->reg2);
  2046. if (format & M6812_OP_EXG_MARKER)
  2047. byte |= 0x80;
  2048. if ((format & M6812_OP_SEX_MARKER)
  2049. && (op->reg1 == REG_D) && (current_architecture & cpu9s12x))
  2050. byte |= 0x08;
  2051. f = frag_more (1);
  2052. number_to_chars_bigendian (f, byte, 1);
  2053. return 1;
  2054. }
  2055. /* build_insn_xg takes a pointer to the opcode entry in the opcode table,
  2056. the array of operand expressions and builds the corresponding instruction. */
  2057. static void
  2058. build_insn_xg (struct m68hc11_opcode *opcode,
  2059. operand operands[],
  2060. int nb_operands ATTRIBUTE_UNUSED)
  2061. {
  2062. char *f;
  2063. long format;
  2064. /* Put the page code instruction if there is one. */
  2065. format = opcode->format;
  2066. if (!(operands[0].mode & (M6811_OP_LOW_ADDR | M6811_OP_HIGH_ADDR)))
  2067. /* Need to retain those two modes, but clear for others. */
  2068. operands[0].mode = 0;
  2069. if (format & M68XG_OP_R_IMM8)
  2070. {
  2071. /* These opcodes are byte followed by imm8. */
  2072. f = m68hc11_new_insn (1);
  2073. number_to_chars_bigendian (f, opcode->opcode >> 8, 1);
  2074. fixup8_xg (&operands[0].exp, format, operands[0].mode);
  2075. }
  2076. else if (format & M68XG_OP_R_IMM16)
  2077. {
  2078. fixS *fixp;
  2079. /* These opcodes expand into two imm8 instructions.
  2080. Emit as low:high as per the Freescale datasheet.
  2081. The linker requires them to be adjacent to handle the upper byte. */
  2082. /* Build low byte. */
  2083. f = m68hc11_new_insn (1);
  2084. number_to_chars_bigendian (f, opcode->opcode >> 8, 1);
  2085. operands[0].mode = M6811_OP_LOW_ADDR;
  2086. f = frag_more (1);
  2087. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  2088. &operands[0].exp, false, BFD_RELOC_M68HC12_LO8XG);
  2089. fixp->fx_no_overflow = 1;
  2090. number_to_chars_bigendian (f, 0, 1);
  2091. /* Build high byte. */
  2092. f = m68hc11_new_insn (1);
  2093. number_to_chars_bigendian (f, (opcode->opcode >> 8) | 0x08, 1);
  2094. operands[0].mode = M6811_OP_HIGH_ADDR;
  2095. f = frag_more (1);
  2096. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  2097. &operands[0].exp, false, BFD_RELOC_M68HC12_HI8XG);
  2098. fixp->fx_no_overflow = 1;
  2099. number_to_chars_bigendian (f, 0, 1);
  2100. }
  2101. else if (format & M68XG_OP_REL9)
  2102. {
  2103. f = m68hc11_new_insn (1);
  2104. number_to_chars_bigendian (f, opcode->opcode >> 8, 1); /* High byte. */
  2105. fixup8_xg (&operands[0].exp, format, M68XG_OP_REL9);
  2106. }
  2107. else if (format & M68XG_OP_REL10)
  2108. {
  2109. f = m68hc11_new_insn (1);
  2110. number_to_chars_bigendian (f, opcode->opcode >> 8, 1); /* High byte. */
  2111. fixup8_xg (&operands[0].exp, format, M68XG_OP_REL10);
  2112. }
  2113. else
  2114. {
  2115. f = m68hc11_new_insn (2);
  2116. number_to_chars_bigendian (f, opcode->opcode, 2);
  2117. }
  2118. return;
  2119. }
  2120. /* build_insn takes a pointer to the opcode entry in the opcode table,
  2121. the array of operand expressions and builds the corresponding instruction.
  2122. This operation only deals with non relative jumps insn (need special
  2123. handling). */
  2124. static void
  2125. build_insn (struct m68hc11_opcode *opcode,
  2126. operand operands[],
  2127. int nb_operands ATTRIBUTE_UNUSED)
  2128. {
  2129. int i;
  2130. char *f;
  2131. long format;
  2132. int move_insn = 0;
  2133. /* Put the page code instruction if there is one. */
  2134. format = opcode->format;
  2135. if (format & M6811_OP_BRANCH)
  2136. fix_new (frag_now, frag_now_fix (), 0,
  2137. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  2138. if (format & OP_EXTENDED)
  2139. {
  2140. int page_code;
  2141. f = m68hc11_new_insn (2);
  2142. if (format & M6811_OP_PAGE2)
  2143. page_code = M6811_OPCODE_PAGE2;
  2144. else if (format & M6811_OP_PAGE3)
  2145. page_code = M6811_OPCODE_PAGE3;
  2146. else
  2147. page_code = M6811_OPCODE_PAGE4;
  2148. number_to_chars_bigendian (f, page_code, 1);
  2149. f++;
  2150. }
  2151. else
  2152. f = m68hc11_new_insn (1);
  2153. number_to_chars_bigendian (f, opcode->opcode, 1);
  2154. i = 0;
  2155. /* The 68HC12 movb and movw instructions are special. We have to handle
  2156. them in a special way. */
  2157. if (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  2158. {
  2159. move_insn = 1;
  2160. if (format & M6812_OP_IDX)
  2161. {
  2162. build_indexed_byte (&operands[0], format, 1);
  2163. i = 1;
  2164. format &= ~M6812_OP_IDX;
  2165. }
  2166. if (format & M6812_OP_IDX_P2)
  2167. {
  2168. build_indexed_byte (&operands[1], format, 1);
  2169. i = 0;
  2170. format &= ~M6812_OP_IDX_P2;
  2171. }
  2172. }
  2173. if (format & (M6811_OP_DIRECT | M6811_OP_IMM8))
  2174. {
  2175. fixup8 (&operands[i].exp,
  2176. format & (M6811_OP_DIRECT | M6811_OP_IMM8 | M6812_OP_TRAP_ID),
  2177. operands[i].mode);
  2178. i++;
  2179. }
  2180. else if (IS_CALL_SYMBOL (format) && nb_operands == 1)
  2181. {
  2182. format &= ~M6812_OP_PAGE;
  2183. fixup24 (&operands[i].exp, format & M6811_OP_IND16,
  2184. operands[i].mode);
  2185. i++;
  2186. }
  2187. else if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
  2188. {
  2189. fixup16 (&operands[i].exp,
  2190. format & (M6811_OP_IMM16 | M6811_OP_IND16 | M6812_OP_PAGE),
  2191. operands[i].mode);
  2192. i++;
  2193. }
  2194. else if (format & (M6811_OP_IX | M6811_OP_IY))
  2195. {
  2196. if ((format & M6811_OP_IX) && (operands[0].reg1 != REG_X))
  2197. as_bad (_("Invalid indexed register, expecting register X."));
  2198. if ((format & M6811_OP_IY) && (operands[0].reg1 != REG_Y))
  2199. as_bad (_("Invalid indexed register, expecting register Y."));
  2200. fixup8 (&operands[0].exp, M6811_OP_IX, operands[0].mode);
  2201. i = 1;
  2202. }
  2203. else if (format &
  2204. (M6812_OP_IDX | M6812_OP_IDX_2 | M6812_OP_IDX_1
  2205. | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  2206. {
  2207. build_indexed_byte (&operands[i], format, move_insn);
  2208. i++;
  2209. }
  2210. else if (format & M6812_OP_REG && current_architecture & cpu6812)
  2211. {
  2212. build_reg_mode (&operands[i], format);
  2213. i++;
  2214. }
  2215. if (format & M6811_OP_BITMASK)
  2216. {
  2217. fixup8 (&operands[i].exp, M6811_OP_BITMASK, operands[i].mode);
  2218. i++;
  2219. }
  2220. if (format & M6811_OP_JUMP_REL)
  2221. {
  2222. fixup8 (&operands[i].exp, M6811_OP_JUMP_REL, operands[i].mode);
  2223. }
  2224. else if (format & M6812_OP_IND16_P2)
  2225. {
  2226. fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
  2227. }
  2228. if (format & M6812_OP_PAGE)
  2229. {
  2230. fixup8 (&operands[i].exp, M6812_OP_PAGE, operands[i].mode);
  2231. }
  2232. }
  2233. /* Opcode identification and operand analysis. */
  2234. /* find() gets a pointer to an entry in the opcode table. It must look at all
  2235. opcodes with the same name and use the operands to choose the correct
  2236. opcode. Returns the opcode pointer if there was a match and 0 if none. */
  2237. static struct m68hc11_opcode *
  2238. find (struct m68hc11_opcode_def *opc, operand operands[], int nb_operands)
  2239. {
  2240. int i, match, pos;
  2241. struct m68hc11_opcode *opcode;
  2242. struct m68hc11_opcode *op_indirect;
  2243. op_indirect = 0;
  2244. opcode = opc->opcode;
  2245. /* Now search the opcode table table for one with operands
  2246. that matches what we've got. */
  2247. if (current_architecture & cpuxgate)
  2248. {
  2249. /* Many XGATE insns are simple enough that we get an exact match. */
  2250. for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
  2251. if (opcode->format == operands[nb_operands-1].mode)
  2252. return opcode;
  2253. return 0;
  2254. }
  2255. /* Non XGATE */
  2256. /* Now search the opcode table table for one with operands
  2257. that matches what we've got. We're only done if the operands matched so
  2258. far AND there are no more to check. */
  2259. for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
  2260. {
  2261. int poss_indirect = 0;
  2262. long format = opcode->format;
  2263. int expect;
  2264. expect = 0;
  2265. if (opcode->format & M6811_OP_MASK)
  2266. expect++;
  2267. if (opcode->format & M6811_OP_BITMASK)
  2268. expect++;
  2269. if (opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2270. expect++;
  2271. if (opcode->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  2272. expect++;
  2273. if ((opcode->format & M6812_OP_PAGE)
  2274. && (!IS_CALL_SYMBOL (opcode->format) || nb_operands == 2))
  2275. expect++;
  2276. for (i = 0; expect == nb_operands && i < nb_operands; i++)
  2277. {
  2278. int mode = operands[i].mode;
  2279. if (mode & M6811_OP_IMM16)
  2280. {
  2281. if (format &
  2282. (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK))
  2283. continue;
  2284. break;
  2285. }
  2286. if (mode == M6811_OP_DIRECT)
  2287. {
  2288. if (format & M6811_OP_DIRECT)
  2289. continue;
  2290. /* If the operand is a page 0 operand, remember a
  2291. possible <abs-16> addressing mode. We mark
  2292. this and continue to check other operands. */
  2293. if (format & M6811_OP_IND16
  2294. && flag_strict_direct_addressing && op_indirect == 0)
  2295. {
  2296. poss_indirect = 1;
  2297. continue;
  2298. }
  2299. break;
  2300. }
  2301. if (mode & M6811_OP_IND16)
  2302. {
  2303. if (i == 0 && (format & M6811_OP_IND16) != 0)
  2304. continue;
  2305. if (i != 0 && (format & M6812_OP_PAGE) != 0)
  2306. continue;
  2307. if (i != 0 && (format & M6812_OP_IND16_P2) != 0)
  2308. continue;
  2309. if (i == 0 && (format & M6811_OP_BITMASK))
  2310. break;
  2311. }
  2312. if (mode & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2313. {
  2314. if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2315. continue;
  2316. }
  2317. if (mode & M6812_OP_REG)
  2318. {
  2319. if (i == 0
  2320. && (format & M6812_OP_REG)
  2321. && (operands[i].reg2 == REG_NONE))
  2322. continue;
  2323. if (i == 0
  2324. && (format & M6812_OP_REG)
  2325. && (format & M6812_OP_REG_2)
  2326. && (operands[i].reg2 != REG_NONE))
  2327. continue;
  2328. if (i == 0
  2329. && (format & M6812_OP_IDX)
  2330. && (operands[i].reg2 != REG_NONE))
  2331. continue;
  2332. if (i == 0
  2333. && (format & M6812_OP_IDX)
  2334. && (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)))
  2335. continue;
  2336. if (i == 1
  2337. && (format & M6812_OP_IDX_P2))
  2338. continue;
  2339. break;
  2340. }
  2341. if (mode & M6812_OP_IDX)
  2342. {
  2343. if (format & M6811_OP_IX && operands[i].reg1 == REG_X)
  2344. continue;
  2345. if (format & M6811_OP_IY && operands[i].reg1 == REG_Y)
  2346. continue;
  2347. if (i == 0
  2348. && format & (M6812_OP_IDX | M6812_OP_IDX_1 | M6812_OP_IDX_2)
  2349. && (operands[i].reg1 == REG_X
  2350. || operands[i].reg1 == REG_Y
  2351. || operands[i].reg1 == REG_SP
  2352. || operands[i].reg1 == REG_PC))
  2353. continue;
  2354. if (i == 1 && (format & M6812_OP_IDX_P2))
  2355. continue;
  2356. }
  2357. if (mode & format & (M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  2358. {
  2359. if (i == 0)
  2360. continue;
  2361. }
  2362. if (mode & M6812_AUTO_INC_DEC)
  2363. {
  2364. if (i == 0
  2365. && format & (M6812_OP_IDX | M6812_OP_IDX_1 |
  2366. M6812_OP_IDX_2))
  2367. continue;
  2368. if (i == 1 && format & M6812_OP_IDX_P2)
  2369. continue;
  2370. }
  2371. break;
  2372. }
  2373. match = i == nb_operands;
  2374. /* Operands are ok but an operand uses page 0 addressing mode
  2375. while the insn supports abs-16 mode. Keep a reference to this
  2376. insns in case there is no insn supporting page 0 addressing. */
  2377. if (match && poss_indirect)
  2378. {
  2379. op_indirect = opcode;
  2380. match = 0;
  2381. }
  2382. if (match)
  2383. break;
  2384. }
  2385. /* Page 0 addressing is used but not supported by any insn.
  2386. If absolute addresses are supported, we use that insn. */
  2387. if (match == 0 && op_indirect)
  2388. {
  2389. opcode = op_indirect;
  2390. match = 1;
  2391. }
  2392. return match ? opcode : 0;
  2393. }
  2394. /* Find the real opcode and its associated operands. We use a progressive
  2395. approach here. On entry, 'opc' points to the first opcode in the
  2396. table that matches the opcode name in the source line. We try to
  2397. isolate an operand, find a possible match in the opcode table.
  2398. We isolate another operand if no match were found. The table 'operands'
  2399. is filled while operands are recognized.
  2400. Returns the opcode pointer that matches the opcode name in the
  2401. source line and the associated operands. */
  2402. static struct m68hc11_opcode *
  2403. find_opcode (struct m68hc11_opcode_def *opc, operand operands[],
  2404. int *nb_operands)
  2405. {
  2406. struct m68hc11_opcode *opcode;
  2407. int i;
  2408. if (opc->max_operands == 0)
  2409. {
  2410. *nb_operands = 0;
  2411. return opc->opcode;
  2412. }
  2413. for (i = 0; i < opc->max_operands;)
  2414. {
  2415. int result;
  2416. result = get_operand (&operands[i], i, opc->format);
  2417. if (result <= 0)
  2418. return 0;
  2419. /* Special case where the bitmask of the bclr/brclr
  2420. instructions is not introduced by #.
  2421. Example: bclr 3,x $80. */
  2422. if (i == 1 && (opc->format & M6811_OP_BITMASK)
  2423. && (operands[i].mode & M6811_OP_IND16))
  2424. {
  2425. operands[i].mode = M6811_OP_IMM16;
  2426. }
  2427. i += result;
  2428. *nb_operands = i;
  2429. if (i >= opc->min_operands)
  2430. {
  2431. opcode = find (opc, operands, i);
  2432. /* Another special case for 'call foo,page' instructions.
  2433. Since we support 'call foo' and 'call foo,page' we must look
  2434. if the optional page specification is present otherwise we will
  2435. assemble immediately and treat the page spec as garbage. */
  2436. if (opcode && !(opcode->format & M6812_OP_PAGE))
  2437. return opcode;
  2438. if (opcode && *input_line_pointer != ',')
  2439. return opcode;
  2440. }
  2441. if (*input_line_pointer == ',')
  2442. input_line_pointer++;
  2443. }
  2444. return 0;
  2445. }
  2446. #define M6812_XBCC_MARKER (M6812_OP_TBCC_MARKER \
  2447. | M6812_OP_DBCC_MARKER \
  2448. | M6812_OP_IBCC_MARKER)
  2449. /* Gas line assembler entry point. */
  2450. /* This is the main entry point for the machine-dependent assembler. str
  2451. points to a machine-dependent instruction. This function is supposed to
  2452. emit the frags/bytes it assembles to. */
  2453. void
  2454. md_assemble (char *str)
  2455. {
  2456. struct m68hc11_opcode_def *opc;
  2457. struct m68hc11_opcode *opcode;
  2458. struct m68hc11_opcode opcode_local;
  2459. unsigned char *op_start, *op_end;
  2460. char *save;
  2461. char name[20];
  2462. int nlen = 0;
  2463. operand operands[M6811_MAX_OPERANDS];
  2464. int nb_operands = 0;
  2465. int branch_optimize = 0;
  2466. int alias_id = -1;
  2467. /* Drop leading whitespace. */
  2468. while (*str == ' ')
  2469. str++;
  2470. /* Find the opcode end and get the opcode in 'name'. The opcode is forced
  2471. lower case (the opcode table only has lower case op-codes). */
  2472. for (op_start = op_end = (unsigned char *) str;
  2473. *op_end && !is_end_of_line[*op_end] && *op_end != ' ';
  2474. op_end++)
  2475. {
  2476. name[nlen] = TOLOWER (op_start[nlen]);
  2477. nlen++;
  2478. if (nlen == sizeof (name) - 1)
  2479. break;
  2480. }
  2481. name[nlen] = 0;
  2482. if (nlen == 0)
  2483. {
  2484. as_bad (_("No instruction or missing opcode."));
  2485. return;
  2486. }
  2487. if (current_architecture == cpuxgate)
  2488. {
  2489. /* Find the opcode definition given its name. */
  2490. opc = (struct m68hc11_opcode_def *) str_hash_find (m68hc11_hash, name);
  2491. if (opc == NULL)
  2492. {
  2493. as_bad (_("Opcode `%s' is not recognized."), name);
  2494. return;
  2495. }
  2496. /* Grab a local copy. */
  2497. opcode_local.name = opc->opcode->name;
  2498. /* These will be incomplete where multiple variants exist. */
  2499. opcode_local.opcode = opc->opcode->opcode;
  2500. opcode_local.format = opc->opcode->format;
  2501. save = input_line_pointer;
  2502. input_line_pointer = (char *) op_end;
  2503. if (opc->format == M68XG_OP_NONE)
  2504. {
  2505. /* No special handling required. */
  2506. opcode_local.format = M68XG_OP_NONE;
  2507. build_insn_xg (opc->opcode, operands, 0);
  2508. return;
  2509. }
  2510. /* Special handling of TFR. */
  2511. if (startswith (opc->opcode->name, "tfr"))
  2512. {
  2513. /* There must be two operands with a comma. */
  2514. input_line_pointer = skip_whites (input_line_pointer);
  2515. operands[0].reg1 = register_name ();
  2516. if (operands[0].reg1 == REG_NONE)
  2517. {
  2518. as_bad ("Invalid register\n");
  2519. return;
  2520. }
  2521. input_line_pointer = skip_whites (input_line_pointer);
  2522. if (*input_line_pointer != ',')
  2523. {
  2524. as_bad ("Missing comma.\n");
  2525. return;
  2526. }
  2527. input_line_pointer++;
  2528. input_line_pointer = skip_whites (input_line_pointer);
  2529. operands[1].reg1 = register_name ();
  2530. if (operands[1].reg1 == REG_NONE)
  2531. {
  2532. as_bad ("Invalid register\n");
  2533. return;
  2534. }
  2535. input_line_pointer = skip_whites (input_line_pointer);
  2536. if (*input_line_pointer != '\n' && *input_line_pointer)
  2537. {
  2538. as_bad (_("Garbage at end of instruction: `%s'."),
  2539. input_line_pointer);
  2540. return;
  2541. }
  2542. if (operands[1].reg1 == REG_CCR) /* ,CCR */
  2543. opc->opcode->opcode = 0x00f8 | ( operands[0].reg1 << 8);
  2544. else if (operands[0].reg1 == REG_CCR) /* CCR, */
  2545. opc->opcode->opcode = 0x00f9 | ( operands[1].reg1 << 8);
  2546. else if (operands[1].reg1 == REG_PC) /* ,PC */
  2547. opc->opcode->opcode = 0x00fa | ( operands[0].reg1 << 8);
  2548. else
  2549. {
  2550. as_bad ("Invalid operand to TFR\n");
  2551. return;
  2552. }
  2553. /* no special handling required */
  2554. opcode_local.format = M68XG_OP_NONE;
  2555. opcode_local.opcode = opc->opcode->opcode;
  2556. build_insn_xg (&opcode_local, operands, 0);
  2557. return;
  2558. }
  2559. /* CSEM, SSEM */
  2560. if (opc->format & M68XG_OP_IMM3)
  2561. {
  2562. /* Either IMM3 or R */
  2563. input_line_pointer = skip_whites (input_line_pointer);
  2564. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2565. {
  2566. operands[0].reg1 = register_name ();
  2567. if (operands[0].reg1 == REG_NONE)
  2568. {
  2569. as_bad ("Invalid register\n");
  2570. return;
  2571. }
  2572. operands[0].mode = M68XG_OP_R;
  2573. /* One opcode has multiple modes, so find right one. */
  2574. opcode = find (opc, operands, 1);
  2575. if (opcode)
  2576. {
  2577. opcode_local.opcode = opcode->opcode
  2578. | (operands[0].reg1 << 8);
  2579. opcode_local.format = M68XG_OP_NONE;
  2580. build_insn_xg (&opcode_local, operands, 1);
  2581. }
  2582. else
  2583. as_bad ("No opcode found\n");
  2584. return;
  2585. }
  2586. else
  2587. {
  2588. if (*input_line_pointer == '#')
  2589. input_line_pointer++;
  2590. expression (&operands[0].exp);
  2591. if (operands[0].exp.X_op == O_illegal)
  2592. {
  2593. as_bad (_("Illegal operand."));
  2594. return;
  2595. }
  2596. else if (operands[0].exp.X_op == O_absent)
  2597. {
  2598. as_bad (_("Missing operand."));
  2599. return;
  2600. }
  2601. if (check_range (operands[0].exp.X_add_number,M68XG_OP_IMM3))
  2602. {
  2603. opcode_local.opcode |= (operands[0].exp.X_add_number);
  2604. operands[0].mode = M68XG_OP_IMM3;
  2605. opcode = find (opc, operands, 1);
  2606. if (opcode)
  2607. {
  2608. opcode_local.opcode = opcode->opcode;
  2609. opcode_local.opcode
  2610. |= (operands[0].exp.X_add_number) << 8;
  2611. opcode_local.format = M68XG_OP_NONE;
  2612. build_insn_xg (&opcode_local, operands, 1);
  2613. }
  2614. else
  2615. as_bad ("No opcode found\n");
  2616. return;
  2617. }
  2618. else
  2619. {
  2620. as_bad ("Number out of range for IMM3\n");
  2621. return;
  2622. }
  2623. }
  2624. }
  2625. /* Special handling of SIF. */
  2626. if (startswith (opc->opcode->name, "sif"))
  2627. {
  2628. /* Either OP_NONE or OP_RS. */
  2629. if (*input_line_pointer != '\n')
  2630. input_line_pointer = skip_whites (input_line_pointer);
  2631. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2632. || (*input_line_pointer == '\0'))
  2633. opc->opcode->opcode = 0x0300;
  2634. else
  2635. {
  2636. operands[0].reg1 = register_name ();
  2637. if (operands[0].reg1 == REG_NONE)
  2638. {
  2639. as_bad ("Invalid register\n");
  2640. return;
  2641. }
  2642. opcode_local.opcode = 0x00f7 | (operands[0].reg1 << 8);
  2643. }
  2644. opcode_local.format = M68XG_OP_NONE;
  2645. build_insn_xg (&opcode_local, operands, 0);
  2646. return;
  2647. }
  2648. /* SEX, PAR, JAL plus aliases NEG, TST, COM */
  2649. if (opc->format & M68XG_OP_R)
  2650. {
  2651. input_line_pointer = skip_whites (input_line_pointer);
  2652. operands[0].reg1 = register_name ();
  2653. if (operands[0].reg1 == REG_NONE)
  2654. {
  2655. as_bad ("Invalid register\n");
  2656. return;
  2657. }
  2658. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2659. || (*input_line_pointer == '\0'))
  2660. {
  2661. /* Likely to be OP R. */
  2662. if (opc->format & M68XG_OP_R)
  2663. {
  2664. operands[0].mode = M68XG_OP_R;
  2665. opcode = find (opc, operands, 1);
  2666. if (opcode)
  2667. {
  2668. if ((startswith (opc->opcode->name, "com"))
  2669. || (startswith (opc->opcode->name, "neg")))
  2670. /* Special case for com RD as alias for sub RD,R0,RS */
  2671. /* Special case for neg RD as alias for sub RD,R0,RS */
  2672. opcode_local.opcode = opcode->opcode
  2673. | (operands[0].reg1 << 8) | (operands[0].reg1 << 2);
  2674. else if (startswith (opc->opcode->name, "tst"))
  2675. /* Special case for tst RS alias for sub R0, RS, R0 */
  2676. opcode_local.opcode = opcode->opcode
  2677. | (operands[0].reg1 << 5);
  2678. else
  2679. opcode_local.opcode |= (operands[0].reg1 << 8);
  2680. }
  2681. opcode_local.format = M68XG_OP_NONE;
  2682. build_insn_xg (&opcode_local, operands, 0);
  2683. }
  2684. else
  2685. as_bad ("No valid mode found\n");
  2686. return;
  2687. }
  2688. }
  2689. if (opc->format & (M68XG_OP_REL9 | M68XG_OP_REL10))
  2690. {
  2691. opcode_local.format = opc->format;
  2692. input_line_pointer = skip_whites (input_line_pointer);
  2693. expression (&operands[0].exp);
  2694. if (operands[0].exp.X_op == O_illegal)
  2695. {
  2696. as_bad (_("Illegal operand."));
  2697. return;
  2698. }
  2699. else if (operands[0].exp.X_op == O_absent)
  2700. {
  2701. as_bad (_("Missing operand."));
  2702. return;
  2703. }
  2704. opcode_local.opcode = opc->opcode->opcode;
  2705. build_insn_xg (&opcode_local, operands, 1);
  2706. return;
  2707. }
  2708. /* For other command formats, parse input line and determine the mode
  2709. we are using as we go. */
  2710. input_line_pointer = skip_whites (input_line_pointer);
  2711. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2712. || (*input_line_pointer == '\0'))
  2713. return; /* nothing left */
  2714. if (*input_line_pointer == '#')
  2715. {
  2716. as_bad ("No register specified before hash\n");
  2717. return;
  2718. }
  2719. /* first operand is expected to be a register */
  2720. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2721. {
  2722. operands[0].reg1 = register_name ();
  2723. if (operands[0].reg1 == REG_NONE)
  2724. {
  2725. as_bad ("Invalid register\n");
  2726. return;
  2727. }
  2728. }
  2729. input_line_pointer = skip_whites (input_line_pointer);
  2730. if (*input_line_pointer != ',')
  2731. {
  2732. as_bad ("Missing operand\n");
  2733. return;
  2734. }
  2735. input_line_pointer++;
  2736. input_line_pointer = skip_whites (input_line_pointer);
  2737. if (*input_line_pointer == '#')
  2738. {
  2739. /* Some kind of immediate mode, check if this is possible. */
  2740. if (!(opc->format
  2741. & (M68XG_OP_R_IMM8 | M68XG_OP_R_IMM16 | M68XG_OP_R_IMM4)))
  2742. as_bad ("Invalid immediate mode for `%s'", opc->opcode->name);
  2743. else
  2744. {
  2745. input_line_pointer++;
  2746. input_line_pointer = skip_whites (input_line_pointer);
  2747. if (startswith (input_line_pointer, "%hi"))
  2748. {
  2749. input_line_pointer += 3;
  2750. operands[0].mode = M6811_OP_HIGH_ADDR;
  2751. }
  2752. else if (startswith (input_line_pointer, "%lo"))
  2753. {
  2754. input_line_pointer += 3;
  2755. operands[0].mode = M6811_OP_LOW_ADDR;
  2756. }
  2757. else
  2758. operands[0].mode = 0;
  2759. expression (&operands[0].exp);
  2760. if (operands[0].exp.X_op == O_illegal)
  2761. {
  2762. as_bad (_("Illegal operand."));
  2763. return;
  2764. }
  2765. else if (operands[0].exp.X_op == O_absent)
  2766. {
  2767. as_bad (_("Missing operand."));
  2768. return;
  2769. }
  2770. /* ok so far, can only be one mode */
  2771. opcode_local.format = opc->format
  2772. & (M68XG_OP_R_IMM8 | M68XG_OP_R_IMM16 | M68XG_OP_R_IMM4);
  2773. if (opcode_local.format & M68XG_OP_R_IMM4)
  2774. {
  2775. operands[0].mode = M68XG_OP_R_IMM4;
  2776. /* same opcodes have multiple modes, so find right one */
  2777. opcode = find (opc, operands, 1);
  2778. if (opcode)
  2779. opcode_local.opcode = opcode->opcode
  2780. | (operands[0].reg1 << 8);
  2781. if (operands[0].exp.X_op != O_constant)
  2782. as_bad ("Only constants supported at for IMM4 mode\n");
  2783. else
  2784. {
  2785. if (check_range
  2786. (operands[0].exp.X_add_number,M68XG_OP_R_IMM4))
  2787. opcode_local.opcode
  2788. |= (operands[0].exp.X_add_number << 4);
  2789. else
  2790. as_bad ("Number out of range for IMM4\n");
  2791. }
  2792. opcode_local.format = M68XG_OP_NONE;
  2793. }
  2794. else if (opcode_local.format & M68XG_OP_R_IMM16)
  2795. {
  2796. operands[0].mode = M68XG_OP_R_IMM16;
  2797. opcode = find (opc, operands, 1);
  2798. if (opcode)
  2799. {
  2800. opcode_local.opcode = opcode->opcode
  2801. | (operands[0].reg1 << 8);
  2802. }
  2803. }
  2804. else
  2805. {
  2806. opcode_local.opcode = opc->opcode->opcode
  2807. | (operands[0].reg1 << 8);
  2808. }
  2809. build_insn_xg (&opcode_local, operands, 1);
  2810. }
  2811. }
  2812. else if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2813. {
  2814. /* we've got as far as OP R, R */
  2815. operands[1].reg1 = register_name ();
  2816. if (operands[1].reg1 == REG_NONE)
  2817. {
  2818. as_bad ("Invalid register\n");
  2819. return;
  2820. }
  2821. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2822. || (*input_line_pointer == '\0'))
  2823. {
  2824. /* looks like OP_R_R */
  2825. if (opc->format & M68XG_OP_R_R)
  2826. {
  2827. operands[0].mode = M68XG_OP_R_R;
  2828. /* same opcodes have multiple modes, so find right one */
  2829. opcode = find (opc, operands, 1);
  2830. if (opcode)
  2831. {
  2832. if ((startswith (opc->opcode->name, "com"))
  2833. || (startswith (opc->opcode->name, "mov"))
  2834. || (startswith (opc->opcode->name, "neg")))
  2835. {
  2836. /* Special cases for:
  2837. com RD, RS alias for xnor RD,R0,RS
  2838. mov RD, RS alias for or RD, R0, RS
  2839. neg RD, RS alias for sub RD, R0, RS */
  2840. opcode_local.opcode = opcode->opcode
  2841. | (operands[0].reg1 << 8) | (operands[1].reg1 << 2);
  2842. }
  2843. else if ((startswith (opc->opcode->name, "cmp"))
  2844. || (startswith (opc->opcode->name, "cpc")))
  2845. {
  2846. /* special cases for:
  2847. cmp RS1, RS2 alias for sub R0, RS1, RS2
  2848. cpc RS1, RS2 alias for sbc R0, RS1, RS2 */
  2849. opcode_local.opcode = opcode->opcode
  2850. | (operands[0].reg1 << 5) | (operands[1].reg1 << 2);
  2851. }
  2852. else
  2853. {
  2854. opcode_local.opcode = opcode->opcode
  2855. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5);
  2856. }
  2857. opcode_local.format = M68XG_OP_NONE;
  2858. build_insn_xg (&opcode_local, operands, 1);
  2859. }
  2860. }
  2861. else
  2862. {
  2863. as_bad ("No valid mode found\n");
  2864. }
  2865. }
  2866. else
  2867. {
  2868. /* more data */
  2869. if (*input_line_pointer != ',')
  2870. {
  2871. as_bad (_("Missing operand."));
  2872. return;
  2873. }
  2874. input_line_pointer++;
  2875. input_line_pointer = skip_whites (input_line_pointer);
  2876. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2877. {
  2878. operands[2].reg1 = register_name ();
  2879. if (operands[2].reg1 == REG_NONE)
  2880. {
  2881. as_bad ("Invalid register\n");
  2882. return;
  2883. }
  2884. if (opc->format & M68XG_OP_R_R_R)
  2885. {
  2886. operands[0].mode = M68XG_OP_R_R_R;
  2887. opcode = find (opc, operands, 1);
  2888. if (opcode)
  2889. {
  2890. opcode_local.opcode = opcode->opcode
  2891. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5)
  2892. | (operands[2].reg1 << 2);
  2893. opcode_local.format = M68XG_OP_NONE;
  2894. build_insn_xg (&opcode_local, operands, 1);
  2895. }
  2896. }
  2897. else
  2898. {
  2899. as_bad ("No valid mode found\n");
  2900. }
  2901. }
  2902. }
  2903. }
  2904. else if (*input_line_pointer == '(') /* Indexed modes */
  2905. {
  2906. input_line_pointer++;
  2907. input_line_pointer = skip_whites (input_line_pointer);
  2908. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2909. {
  2910. /* we've got as far as OP R, (R */
  2911. operands[1].reg1 = register_name ();
  2912. if (operands[1].reg1 == REG_NONE)
  2913. {
  2914. as_bad ("Invalid register\n");
  2915. return;
  2916. }
  2917. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2918. || (*input_line_pointer == '\0'))
  2919. {
  2920. /* Looks like OP_R_R. */
  2921. as_bad (_("Missing operand."));
  2922. return;
  2923. }
  2924. input_line_pointer = skip_whites (input_line_pointer);
  2925. if (*input_line_pointer != ',')
  2926. {
  2927. as_bad (_("Missing operand."));
  2928. return;
  2929. }
  2930. input_line_pointer++;
  2931. input_line_pointer = skip_whites (input_line_pointer);
  2932. if (*input_line_pointer == '#')
  2933. {
  2934. input_line_pointer++;
  2935. input_line_pointer = skip_whites (input_line_pointer);
  2936. expression (&operands[0].exp);
  2937. if (operands[0].exp.X_op == O_illegal)
  2938. {
  2939. as_bad (_("Illegal operand."));
  2940. return;
  2941. }
  2942. else if (operands[0].exp.X_op == O_absent)
  2943. {
  2944. as_bad (_("Missing operand."));
  2945. return;
  2946. }
  2947. input_line_pointer = skip_whites (input_line_pointer);
  2948. if (*input_line_pointer != ')')
  2949. {
  2950. as_bad ("Missing `)' to close register indirect operand.");
  2951. return;
  2952. }
  2953. else
  2954. {
  2955. input_line_pointer++;
  2956. }
  2957. /* Ok so far, can only be one mode. */
  2958. opcode_local.format = M68XG_OP_R_R_OFFS5;
  2959. operands[0].mode = M68XG_OP_R_R_OFFS5;
  2960. opcode = find (opc, operands, 1);
  2961. if (opcode)
  2962. {
  2963. opcode_local.opcode = opcode->opcode
  2964. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5);
  2965. if (operands[0].exp.X_op != O_constant)
  2966. {
  2967. as_bad
  2968. ("Only constants supported for indexed OFFS5 mode\n");
  2969. }
  2970. else
  2971. {
  2972. if (check_range (operands[0].exp.X_add_number,
  2973. M68XG_OP_R_R_OFFS5))
  2974. {
  2975. opcode_local.opcode
  2976. |= (operands[0].exp.X_add_number);
  2977. opcode_local.format = M68XG_OP_NONE;
  2978. build_insn_xg (&opcode_local, operands, 1);
  2979. }
  2980. else
  2981. {
  2982. as_bad ("Number out of range for OFFS5\n");
  2983. }
  2984. }
  2985. }
  2986. }
  2987. else
  2988. {
  2989. operands[0].mode = M68XG_OP_RD_RB_RI;
  2990. if (*input_line_pointer == '-')
  2991. {
  2992. operands[0].mode = M68XG_OP_RD_RB_mRI;
  2993. input_line_pointer++;
  2994. }
  2995. operands[2].reg1 = register_name ();
  2996. if (operands[2].reg1 == REG_NONE)
  2997. {
  2998. as_bad ("Invalid register\n");
  2999. return;
  3000. }
  3001. if (*input_line_pointer == '+')
  3002. {
  3003. if (opcode_local.format == M68XG_OP_RD_RB_mRI)
  3004. {
  3005. as_bad (_("Illegal operand."));
  3006. return;
  3007. }
  3008. operands[0].mode = M68XG_OP_RD_RB_RIp;
  3009. input_line_pointer++;
  3010. }
  3011. input_line_pointer = skip_whites (input_line_pointer);
  3012. if (*input_line_pointer != ')')
  3013. {
  3014. as_bad
  3015. ("Missing `)' to close register indirect operand.");
  3016. return;
  3017. }
  3018. else
  3019. {
  3020. input_line_pointer++;
  3021. }
  3022. opcode = find (opc, operands, 1);
  3023. if (opcode)
  3024. {
  3025. opcode_local.opcode = opcode->opcode
  3026. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5)
  3027. | (operands[2].reg1 << 2);
  3028. opcode_local.format = M68XG_OP_NONE;
  3029. build_insn_xg (&opcode_local, operands, 1);
  3030. }
  3031. else
  3032. {
  3033. as_bad ("Failed to find opcode for %s %s\n",
  3034. opc->opcode->name, (char *)op_end);
  3035. }
  3036. }
  3037. }
  3038. }
  3039. else
  3040. {
  3041. as_bad (_("Failed to find a valid mode for `%s'."),
  3042. opc->opcode->name);
  3043. }
  3044. if (opc->opcode && !flag_mri)
  3045. {
  3046. char *p = input_line_pointer;
  3047. while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
  3048. p++;
  3049. if (*p != '\n' && *p)
  3050. as_bad (_("Garbage at end of instruction: `%s'."), p);
  3051. }
  3052. input_line_pointer = save;
  3053. /* Opcode is known but does not have valid operands. Print out the
  3054. syntax for this opcode. */
  3055. if (opc->opcode == 0)
  3056. {
  3057. if (flag_print_insn_syntax)
  3058. print_insn_format (name);
  3059. as_bad (_("Invalid operand for `%s'"), name);
  3060. return;
  3061. }
  3062. return;
  3063. }
  3064. /* Find the opcode definition given its name. */
  3065. opc = (struct m68hc11_opcode_def *) str_hash_find (m68hc11_hash, name);
  3066. /* If it's not recognized, look for 'jbsr' and 'jbxx'. These are
  3067. pseudo insns for relative branch. For these branches, we always
  3068. optimize them (turned into absolute branches) even if --short-branches
  3069. is given. */
  3070. if (opc == NULL && name[0] == 'j' && name[1] == 'b')
  3071. {
  3072. opc = (struct m68hc11_opcode_def *) str_hash_find (m68hc11_hash,
  3073. &name[1]);
  3074. if (opc
  3075. && (!(opc->format & M6811_OP_JUMP_REL)
  3076. || (opc->format & M6811_OP_BITMASK)))
  3077. opc = 0;
  3078. if (opc)
  3079. branch_optimize = 1;
  3080. }
  3081. /* The following test should probably be removed. This does not conform
  3082. to Motorola assembler specs. */
  3083. if (opc == NULL && flag_mri)
  3084. {
  3085. if (*op_end == ' ' || *op_end == '\t')
  3086. {
  3087. while (*op_end == ' ' || *op_end == '\t')
  3088. op_end++;
  3089. if (nlen < 19
  3090. && (*op_end &&
  3091. (is_end_of_line[op_end[1]]
  3092. || op_end[1] == ' ' || op_end[1] == '\t'
  3093. || !ISALNUM (op_end[1])))
  3094. && (*op_end == 'a' || *op_end == 'b'
  3095. || *op_end == 'A' || *op_end == 'B'
  3096. || *op_end == 'd' || *op_end == 'D'
  3097. || *op_end == 'x' || *op_end == 'X'
  3098. || *op_end == 'y' || *op_end == 'Y'))
  3099. {
  3100. name[nlen++] = TOLOWER (*op_end++);
  3101. name[nlen] = 0;
  3102. opc = (struct m68hc11_opcode_def *) str_hash_find (m68hc11_hash,
  3103. name);
  3104. }
  3105. }
  3106. }
  3107. /* Identify a possible instruction alias. There are some on the
  3108. 68HC12 to emulate a few 68HC11 instructions. */
  3109. if (opc == NULL && (current_architecture & cpu6812))
  3110. {
  3111. int i;
  3112. for (i = 0; i < m68hc12_num_alias; i++)
  3113. if (strcmp (m68hc12_alias[i].name, name) == 0)
  3114. {
  3115. alias_id = i;
  3116. break;
  3117. }
  3118. }
  3119. if (opc == NULL && alias_id < 0)
  3120. {
  3121. as_bad (_("Opcode `%s' is not recognized."), name);
  3122. return;
  3123. }
  3124. save = input_line_pointer;
  3125. input_line_pointer = (char *) op_end;
  3126. if (opc)
  3127. {
  3128. opc->used++;
  3129. opcode = find_opcode (opc, operands, &nb_operands);
  3130. }
  3131. else
  3132. opcode = 0;
  3133. if ((opcode || alias_id >= 0) && !flag_mri)
  3134. {
  3135. char *p = input_line_pointer;
  3136. while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
  3137. p++;
  3138. if (*p != '\n' && *p)
  3139. as_bad (_("Garbage at end of instruction: `%s'."), p);
  3140. }
  3141. input_line_pointer = save;
  3142. if (alias_id >= 0)
  3143. {
  3144. char *f = m68hc11_new_insn (m68hc12_alias[alias_id].size);
  3145. number_to_chars_bigendian (f, m68hc12_alias[alias_id].code1, 1);
  3146. if (m68hc12_alias[alias_id].size > 1)
  3147. number_to_chars_bigendian (f + 1, m68hc12_alias[alias_id].code2, 1);
  3148. return;
  3149. }
  3150. /* Opcode is known but does not have valid operands. Print out the
  3151. syntax for this opcode. */
  3152. if (opcode == 0)
  3153. {
  3154. if (flag_print_insn_syntax)
  3155. print_insn_format (name);
  3156. if (((strcmp (name, "movb") == 0) || (strcmp (name, "movw") == 0))
  3157. && (current_architecture & cpu9s12x))
  3158. {
  3159. char *f;
  3160. int movb;
  3161. if (strcmp (name, "movb") == 0)
  3162. movb = 8;
  3163. else
  3164. movb = 0;
  3165. /* The existing operand extract code fell over if these additional modes
  3166. were enabled in m68hc11-opc.c. So they are commented there and
  3167. decoded here instead. */
  3168. if (operands[1].mode & (M6812_OP_IDX | M6812_OP_IDX_1
  3169. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2 | M6812_PRE_INC
  3170. | M6812_PRE_DEC | M6812_POST_INC | M6812_POST_DEC ))
  3171. {
  3172. /* first check if valid mode then start building it up */
  3173. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16
  3174. | M6811_OP_IND16 | M6812_OP_IDX | M6812_OP_IDX_1
  3175. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  3176. {
  3177. int opr16a;
  3178. if (operands[1].mode & (M6811_OP_IND16))
  3179. opr16a = 3;
  3180. else
  3181. opr16a = 0;
  3182. f = m68hc11_new_insn (2);
  3183. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16))
  3184. {
  3185. number_to_chars_bigendian (f, 0x1800 + movb + opr16a, 2);
  3186. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3187. if (movb)
  3188. fixup8 (&operands[0].exp, M6811_OP_IMM8,
  3189. operands[0].mode);
  3190. else
  3191. fixup16 (&operands[0].exp, M6811_OP_IMM16,
  3192. operands[0].mode);
  3193. return;
  3194. }
  3195. else if (operands[0].mode & M6811_OP_IND16)
  3196. {
  3197. number_to_chars_bigendian (f, 0x1801 + movb + opr16a, 2);
  3198. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3199. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3200. return;
  3201. }
  3202. else
  3203. {
  3204. number_to_chars_bigendian (f, 0x1802 + movb + opr16a, 2);
  3205. build_indexed_byte (&operands[0], operands[0].mode, 1);
  3206. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3207. return;
  3208. }
  3209. }
  3210. }
  3211. else if (operands[1].mode & M6811_OP_IND16)
  3212. {
  3213. /* First check if this is valid mode, then start building it up. */
  3214. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16
  3215. | M6811_OP_IND16 | M6812_OP_IDX | M6812_OP_IDX_1
  3216. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  3217. {
  3218. int opr16a;
  3219. if (operands[1].mode & (M6811_OP_IND16))
  3220. opr16a = 3;
  3221. else
  3222. opr16a = 0;
  3223. f = m68hc11_new_insn (2);
  3224. /* The first two cases here should actually be covered by the
  3225. normal operand code. */
  3226. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16))
  3227. {
  3228. number_to_chars_bigendian (f, 0x1800 + movb + opr16a, 2);
  3229. if (movb)
  3230. fixup8 (&operands[0].exp, M6811_OP_IMM8, operands[0].mode);
  3231. else
  3232. fixup16 (&operands[0].exp, M6811_OP_IMM16, operands[0].mode);
  3233. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3234. return;
  3235. }
  3236. else if (operands[0].mode & M6811_OP_IND16)
  3237. {
  3238. number_to_chars_bigendian (f, 0x1801 + movb + opr16a, 2);
  3239. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3240. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3241. return;
  3242. }
  3243. else
  3244. {
  3245. number_to_chars_bigendian (f, 0x1802 + movb + opr16a, 2);
  3246. build_indexed_byte (&operands[0], operands[0].mode, 1);
  3247. fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
  3248. return;
  3249. }
  3250. }
  3251. }
  3252. as_bad (_("Invalid operand for `%s'"), name);
  3253. return;
  3254. }
  3255. else
  3256. {
  3257. as_bad (_("Invalid operand for `%s'"), name);
  3258. return;
  3259. }
  3260. }
  3261. /* Treat dbeq/ibeq/tbeq instructions in a special way. The branch is
  3262. relative and must be in the range -256..255 (9-bits). */
  3263. if ((opcode->format & M6812_XBCC_MARKER)
  3264. && (opcode->format & M6811_OP_JUMP_REL))
  3265. build_dbranch_insn (opcode, operands, nb_operands, branch_optimize);
  3266. /* Relative jumps instructions are taken care of separately. We have to make
  3267. sure that the relative branch is within the range -128..127. If it's out
  3268. of range, the instructions are changed into absolute instructions.
  3269. This is not supported for the brset and brclr instructions. */
  3270. else if ((opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  3271. && !(opcode->format & M6811_OP_BITMASK))
  3272. build_jump_insn (opcode, operands, nb_operands, branch_optimize);
  3273. else
  3274. build_insn (opcode, operands, nb_operands);
  3275. }
  3276. /* Pseudo op to control the ELF flags. */
  3277. static void
  3278. s_m68hc11_mode (int x ATTRIBUTE_UNUSED)
  3279. {
  3280. char *name = input_line_pointer, ch;
  3281. while (!is_end_of_line[(unsigned char) *input_line_pointer])
  3282. input_line_pointer++;
  3283. ch = *input_line_pointer;
  3284. *input_line_pointer = '\0';
  3285. if (strcmp (name, "mshort") == 0)
  3286. {
  3287. elf_flags &= ~E_M68HC11_I32;
  3288. }
  3289. else if (strcmp (name, "mlong") == 0)
  3290. {
  3291. elf_flags |= E_M68HC11_I32;
  3292. }
  3293. else if (strcmp (name, "mshort-double") == 0)
  3294. {
  3295. elf_flags &= ~E_M68HC11_F64;
  3296. }
  3297. else if (strcmp (name, "mlong-double") == 0)
  3298. {
  3299. elf_flags |= E_M68HC11_F64;
  3300. }
  3301. else
  3302. {
  3303. as_warn (_("Invalid mode: %s\n"), name);
  3304. }
  3305. *input_line_pointer = ch;
  3306. demand_empty_rest_of_line ();
  3307. }
  3308. /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
  3309. are using 'rtc' for returning. It is necessary to use 'call'
  3310. to invoke them. This is also used by the debugger to correctly
  3311. find the stack frame. */
  3312. static void
  3313. s_m68hc11_mark_symbol (int mark)
  3314. {
  3315. char *name;
  3316. int c;
  3317. symbolS *symbolP;
  3318. asymbol *bfdsym;
  3319. elf_symbol_type *elfsym;
  3320. do
  3321. {
  3322. c = get_symbol_name (&name);
  3323. symbolP = symbol_find_or_make (name);
  3324. (void) restore_line_pointer (c);
  3325. SKIP_WHITESPACE ();
  3326. bfdsym = symbol_get_bfdsym (symbolP);
  3327. elfsym = elf_symbol_from (bfdsym);
  3328. gas_assert (elfsym);
  3329. /* Mark the symbol far (using rtc for function return). */
  3330. elfsym->internal_elf_sym.st_other |= mark;
  3331. if (c == ',')
  3332. {
  3333. input_line_pointer ++;
  3334. SKIP_WHITESPACE ();
  3335. if (*input_line_pointer == '\n')
  3336. c = '\n';
  3337. }
  3338. }
  3339. while (c == ',');
  3340. demand_empty_rest_of_line ();
  3341. }
  3342. static void
  3343. s_m68hc11_relax (int ignore ATTRIBUTE_UNUSED)
  3344. {
  3345. expressionS ex;
  3346. expression (&ex);
  3347. if (ex.X_op != O_symbol || ex.X_add_number != 0)
  3348. {
  3349. as_bad (_("bad .relax format"));
  3350. ignore_rest_of_line ();
  3351. return;
  3352. }
  3353. fix_new_exp (frag_now, frag_now_fix (), 0, &ex, 1,
  3354. BFD_RELOC_M68HC11_RL_GROUP);
  3355. demand_empty_rest_of_line ();
  3356. }
  3357. /* Relocation, relaxation and frag conversions. */
  3358. /* PC-relative offsets are relative to the start of the
  3359. next instruction. That is, the address of the offset, plus its
  3360. size, since the offset is always the last part of the insn. */
  3361. long
  3362. md_pcrel_from (fixS *fixP)
  3363. {
  3364. if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_JUMP)
  3365. return 0;
  3366. return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  3367. }
  3368. /* If while processing a fixup, a reloc really needs to be created
  3369. then it is done here. */
  3370. arelent *
  3371. tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
  3372. {
  3373. arelent *reloc;
  3374. reloc = XNEW (arelent);
  3375. reloc->sym_ptr_ptr = XNEW (asymbol *);
  3376. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  3377. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  3378. if (fixp->fx_r_type == 0)
  3379. reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
  3380. else
  3381. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  3382. if (reloc->howto == (reloc_howto_type *) NULL)
  3383. {
  3384. as_bad_where (fixp->fx_file, fixp->fx_line,
  3385. _("Relocation %d is not supported by object file format."),
  3386. (int) fixp->fx_r_type);
  3387. return NULL;
  3388. }
  3389. /* Since we use Rel instead of Rela, encode the vtable entry to be
  3390. used in the relocation's section offset. */
  3391. if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  3392. reloc->address = fixp->fx_offset;
  3393. reloc->addend = 0;
  3394. return reloc;
  3395. }
  3396. /* We need a port-specific relaxation function to cope with sym2 - sym1
  3397. relative expressions with both symbols in the same segment (but not
  3398. necessarily in the same frag as this insn), for example:
  3399. ldab sym2-(sym1-2),pc
  3400. sym1:
  3401. The offset can be 5, 9 or 16 bits long. */
  3402. long
  3403. m68hc11_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS *fragP,
  3404. long stretch ATTRIBUTE_UNUSED)
  3405. {
  3406. long growth;
  3407. offsetT aim = 0;
  3408. symbolS *symbolP;
  3409. const relax_typeS *this_type;
  3410. const relax_typeS *start_type;
  3411. relax_substateT next_state;
  3412. relax_substateT this_state;
  3413. const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
  3414. /* We only have to cope with frags as prepared by
  3415. md_estimate_size_before_relax. The STATE_BITS16 case may get here
  3416. because of the different reasons that it's not relaxable. */
  3417. switch (fragP->fr_subtype)
  3418. {
  3419. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
  3420. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
  3421. /* When we get to this state, the frag won't grow any more. */
  3422. return 0;
  3423. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
  3424. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
  3425. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
  3426. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
  3427. if (fragP->fr_symbol == NULL
  3428. || S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3429. as_fatal (_("internal inconsistency problem in %s: fr_symbol %lx"),
  3430. __FUNCTION__, (long) fragP->fr_symbol);
  3431. symbolP = fragP->fr_symbol;
  3432. if (symbol_resolved_p (symbolP))
  3433. as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
  3434. __FUNCTION__);
  3435. aim = S_GET_VALUE (symbolP);
  3436. break;
  3437. default:
  3438. as_fatal (_("internal inconsistency problem in %s: fr_subtype %d"),
  3439. __FUNCTION__, fragP->fr_subtype);
  3440. }
  3441. /* The rest is stolen from relax_frag. There's no obvious way to
  3442. share the code, but fortunately no requirement to keep in sync as
  3443. long as fragP->fr_symbol does not have its segment changed. */
  3444. this_state = fragP->fr_subtype;
  3445. start_type = this_type = table + this_state;
  3446. if (aim < 0)
  3447. {
  3448. /* Look backwards. */
  3449. for (next_state = this_type->rlx_more; next_state;)
  3450. if (aim >= this_type->rlx_backward)
  3451. next_state = 0;
  3452. else
  3453. {
  3454. /* Grow to next state. */
  3455. this_state = next_state;
  3456. this_type = table + this_state;
  3457. next_state = this_type->rlx_more;
  3458. }
  3459. }
  3460. else
  3461. {
  3462. /* Look forwards. */
  3463. for (next_state = this_type->rlx_more; next_state;)
  3464. if (aim <= this_type->rlx_forward)
  3465. next_state = 0;
  3466. else
  3467. {
  3468. /* Grow to next state. */
  3469. this_state = next_state;
  3470. this_type = table + this_state;
  3471. next_state = this_type->rlx_more;
  3472. }
  3473. }
  3474. growth = this_type->rlx_length - start_type->rlx_length;
  3475. if (growth != 0)
  3476. fragP->fr_subtype = this_state;
  3477. return growth;
  3478. }
  3479. void
  3480. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec ATTRIBUTE_UNUSED,
  3481. fragS *fragP)
  3482. {
  3483. long value;
  3484. long disp;
  3485. char *buffer_address = fragP->fr_literal;
  3486. /* Address in object code of the displacement. */
  3487. int object_address = fragP->fr_fix + fragP->fr_address;
  3488. buffer_address += fragP->fr_fix;
  3489. /* The displacement of the address, from current location. */
  3490. value = S_GET_VALUE (fragP->fr_symbol);
  3491. disp = (value + fragP->fr_offset) - object_address;
  3492. switch (fragP->fr_subtype)
  3493. {
  3494. case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
  3495. fragP->fr_opcode[1] = disp;
  3496. break;
  3497. case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
  3498. /* This relax is only for bsr and bra. */
  3499. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3500. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3501. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3502. fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
  3503. fix_new (fragP, fragP->fr_fix - 1, 2,
  3504. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3505. fragP->fr_fix += 1;
  3506. break;
  3507. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
  3508. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_BYTE):
  3509. fragP->fr_opcode[1] = disp;
  3510. break;
  3511. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
  3512. /* Invert branch. */
  3513. fragP->fr_opcode[0] ^= 1;
  3514. fragP->fr_opcode[1] = 3; /* Branch offset. */
  3515. buffer_address[0] = M6811_JMP;
  3516. fix_new (fragP, fragP->fr_fix + 1, 2,
  3517. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3518. fragP->fr_fix += 3;
  3519. break;
  3520. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD):
  3521. /* Translate branch into a long branch. */
  3522. fragP->fr_opcode[1] = fragP->fr_opcode[0];
  3523. fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
  3524. fix_new (fragP, fragP->fr_fix, 2,
  3525. fragP->fr_symbol, fragP->fr_offset, 1,
  3526. BFD_RELOC_16_PCREL);
  3527. fragP->fr_fix += 2;
  3528. break;
  3529. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
  3530. if (fragP->fr_symbol != 0
  3531. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3532. value = disp;
  3533. /* fall through */
  3534. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
  3535. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 6;
  3536. fragP->fr_opcode[0] |= value & 0x1f;
  3537. break;
  3538. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
  3539. /* For a PC-relative offset, use the displacement with a -1 correction
  3540. to take into account the additional byte of the insn. */
  3541. if (fragP->fr_symbol != 0
  3542. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3543. value = disp - 1;
  3544. /* fall through */
  3545. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
  3546. fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
  3547. fragP->fr_opcode[0] |= 0xE0;
  3548. fragP->fr_opcode[0] |= (value >> 8) & 1;
  3549. fragP->fr_opcode[1] = value;
  3550. fragP->fr_fix += 1;
  3551. break;
  3552. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
  3553. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
  3554. fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
  3555. fragP->fr_opcode[0] |= 0xe2;
  3556. if ((fragP->fr_opcode[0] & 0x0ff) == 0x0fa
  3557. && fragP->fr_symbol != 0
  3558. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3559. {
  3560. fix_new (fragP, fragP->fr_fix, 2,
  3561. fragP->fr_symbol, fragP->fr_offset,
  3562. 1, BFD_RELOC_16_PCREL);
  3563. }
  3564. else
  3565. {
  3566. fix_new (fragP, fragP->fr_fix, 2,
  3567. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3568. }
  3569. fragP->fr_fix += 2;
  3570. break;
  3571. case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE):
  3572. if (disp < 0)
  3573. fragP->fr_opcode[0] |= 0x10;
  3574. fragP->fr_opcode[1] = disp & 0x0FF;
  3575. break;
  3576. case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD):
  3577. /* Invert branch. */
  3578. fragP->fr_opcode[0] ^= 0x20;
  3579. fragP->fr_opcode[1] = 3; /* Branch offset. */
  3580. buffer_address[0] = M6812_JMP;
  3581. fix_new (fragP, fragP->fr_fix + 1, 2,
  3582. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3583. fragP->fr_fix += 3;
  3584. break;
  3585. default:
  3586. break;
  3587. }
  3588. }
  3589. /* On an ELF system, we can't relax a weak symbol. The weak symbol
  3590. can be overridden at final link time by a non weak symbol. We can
  3591. relax externally visible symbol because there is no shared library
  3592. and such symbol can't be overridden (unless they are weak). */
  3593. static int
  3594. relaxable_symbol (symbolS *symbol)
  3595. {
  3596. return ! S_IS_WEAK (symbol);
  3597. }
  3598. /* Force truly undefined symbols to their maximum size, and generally set up
  3599. the frag list to be relaxed. */
  3600. int
  3601. md_estimate_size_before_relax (fragS *fragP, asection *segment)
  3602. {
  3603. if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
  3604. {
  3605. if (S_GET_SEGMENT (fragP->fr_symbol) != segment
  3606. || !relaxable_symbol (fragP->fr_symbol)
  3607. || (segment != absolute_section
  3608. && RELAX_STATE (fragP->fr_subtype) == STATE_INDEXED_OFFSET))
  3609. {
  3610. /* Non-relaxable cases. */
  3611. int old_fr_fix;
  3612. char *buffer_address;
  3613. old_fr_fix = fragP->fr_fix;
  3614. buffer_address = fragP->fr_fix + fragP->fr_literal;
  3615. switch (RELAX_STATE (fragP->fr_subtype))
  3616. {
  3617. case STATE_PC_RELATIVE:
  3618. /* This relax is only for bsr and bra. */
  3619. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3620. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3621. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3622. if (flag_fixed_branches)
  3623. as_bad_where (fragP->fr_file, fragP->fr_line,
  3624. _("bra or bsr with undefined symbol."));
  3625. /* The symbol is undefined or in a separate section.
  3626. Turn bra into a jmp and bsr into a jsr. The insn
  3627. becomes 3 bytes long (instead of 2). A fixup is
  3628. necessary for the unresolved symbol address. */
  3629. fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
  3630. fix_new (fragP, fragP->fr_fix - 1, 2, fragP->fr_symbol,
  3631. fragP->fr_offset, 0, BFD_RELOC_16);
  3632. fragP->fr_fix++;
  3633. break;
  3634. case STATE_CONDITIONAL_BRANCH:
  3635. gas_assert (current_architecture & cpu6811);
  3636. fragP->fr_opcode[0] ^= 1; /* Reverse sense of branch. */
  3637. fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
  3638. /* Don't use fr_opcode[2] because this may be
  3639. in a different frag. */
  3640. buffer_address[0] = M6811_JMP;
  3641. fragP->fr_fix++;
  3642. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3643. fragP->fr_offset, 0, BFD_RELOC_16);
  3644. fragP->fr_fix += 2;
  3645. break;
  3646. case STATE_INDEXED_OFFSET:
  3647. gas_assert (current_architecture & cpu6812);
  3648. if (fragP->fr_symbol
  3649. && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
  3650. {
  3651. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
  3652. STATE_BITS5);
  3653. /* Return the size of the variable part of the frag. */
  3654. return md_relax_table[fragP->fr_subtype].rlx_length;
  3655. }
  3656. else
  3657. {
  3658. /* Switch the indexed operation to 16-bit mode. */
  3659. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
  3660. fragP->fr_opcode[0] |= 0xe2;
  3661. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3662. fragP->fr_offset, 0, BFD_RELOC_16);
  3663. fragP->fr_fix += 2;
  3664. }
  3665. break;
  3666. case STATE_INDEXED_PCREL:
  3667. gas_assert (current_architecture & cpu6812);
  3668. if (fragP->fr_symbol
  3669. && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
  3670. {
  3671. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
  3672. STATE_BITS5);
  3673. /* Return the size of the variable part of the frag. */
  3674. return md_relax_table[fragP->fr_subtype].rlx_length;
  3675. }
  3676. else
  3677. {
  3678. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
  3679. fragP->fr_opcode[0] |= 0xe2;
  3680. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3681. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  3682. fragP->fr_fix += 2;
  3683. }
  3684. break;
  3685. case STATE_XBCC_BRANCH:
  3686. gas_assert (current_architecture & cpu6812);
  3687. fragP->fr_opcode[0] ^= 0x20; /* Reverse sense of branch. */
  3688. fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
  3689. /* Don't use fr_opcode[2] because this may be
  3690. in a different frag. */
  3691. buffer_address[0] = M6812_JMP;
  3692. fragP->fr_fix++;
  3693. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3694. fragP->fr_offset, 0, BFD_RELOC_16);
  3695. fragP->fr_fix += 2;
  3696. break;
  3697. case STATE_CONDITIONAL_BRANCH_6812:
  3698. gas_assert (current_architecture & cpu6812);
  3699. /* Translate into a lbcc branch. */
  3700. fragP->fr_opcode[1] = fragP->fr_opcode[0];
  3701. fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
  3702. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3703. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  3704. fragP->fr_fix += 2;
  3705. break;
  3706. default:
  3707. as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
  3708. }
  3709. frag_wane (fragP);
  3710. /* Return the growth in the fixed part of the frag. */
  3711. return fragP->fr_fix - old_fr_fix;
  3712. }
  3713. /* Relaxable cases. */
  3714. switch (RELAX_STATE (fragP->fr_subtype))
  3715. {
  3716. case STATE_PC_RELATIVE:
  3717. /* This relax is only for bsr and bra. */
  3718. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3719. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3720. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3721. fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
  3722. break;
  3723. case STATE_CONDITIONAL_BRANCH:
  3724. gas_assert (current_architecture & cpu6811);
  3725. fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH,
  3726. STATE_BYTE);
  3727. break;
  3728. case STATE_INDEXED_OFFSET:
  3729. gas_assert (current_architecture & cpu6812);
  3730. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
  3731. STATE_BITS5);
  3732. break;
  3733. case STATE_INDEXED_PCREL:
  3734. gas_assert (current_architecture & cpu6812);
  3735. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
  3736. STATE_BITS5);
  3737. break;
  3738. case STATE_XBCC_BRANCH:
  3739. gas_assert (current_architecture & cpu6812);
  3740. fragP->fr_subtype = ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE);
  3741. break;
  3742. case STATE_CONDITIONAL_BRANCH_6812:
  3743. gas_assert (current_architecture & cpu6812);
  3744. fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812,
  3745. STATE_BYTE);
  3746. break;
  3747. }
  3748. }
  3749. if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
  3750. as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
  3751. /* Return the size of the variable part of the frag. */
  3752. return md_relax_table[fragP->fr_subtype].rlx_length;
  3753. }
  3754. /* See whether we need to force a relocation into the output file. */
  3755. int
  3756. tc_m68hc11_force_relocation (fixS *fixP)
  3757. {
  3758. if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_GROUP)
  3759. return 1;
  3760. return generic_force_reloc (fixP);
  3761. }
  3762. /* Here we decide which fixups can be adjusted to make them relative
  3763. to the beginning of the section instead of the symbol. Basically
  3764. we need to make sure that the linker relaxation is done
  3765. correctly, so in some cases we force the original symbol to be
  3766. used. */
  3767. int
  3768. tc_m68hc11_fix_adjustable (fixS *fixP)
  3769. {
  3770. switch (fixP->fx_r_type)
  3771. {
  3772. /* For the linker relaxation to work correctly, these relocs
  3773. need to be on the symbol itself. */
  3774. case BFD_RELOC_16:
  3775. case BFD_RELOC_M68HC11_RL_JUMP:
  3776. case BFD_RELOC_M68HC11_RL_GROUP:
  3777. case BFD_RELOC_VTABLE_INHERIT:
  3778. case BFD_RELOC_VTABLE_ENTRY:
  3779. case BFD_RELOC_32:
  3780. /* The memory bank addressing translation also needs the original
  3781. symbol. */
  3782. case BFD_RELOC_M68HC11_LO16:
  3783. case BFD_RELOC_M68HC11_PAGE:
  3784. case BFD_RELOC_M68HC11_24:
  3785. return 0;
  3786. default:
  3787. return 1;
  3788. }
  3789. }
  3790. void
  3791. md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
  3792. {
  3793. char *where;
  3794. long value = * valP;
  3795. if (fixP->fx_addsy == (symbolS *) NULL)
  3796. fixP->fx_done = 1;
  3797. /* We don't actually support subtracting a symbol. */
  3798. if (fixP->fx_subsy != (symbolS *) NULL)
  3799. as_bad_subtract (fixP);
  3800. /* Patch the instruction with the resolved operand. Elf relocation
  3801. info will also be generated to take care of linker/loader fixups.
  3802. The 68HC11 addresses only 64Kb, we are only concerned by 8 and 16-bit
  3803. relocs. BFD_RELOC_8 is basically used for .page0 access (the linker
  3804. will warn for overflows). BFD_RELOC_8_PCREL should not be generated
  3805. because it's either resolved or turned out into non-relative insns (see
  3806. relax table, bcc, bra, bsr transformations)
  3807. The BFD_RELOC_32 is necessary for the support of --gstabs. */
  3808. where = fixP->fx_frag->fr_literal + fixP->fx_where;
  3809. switch (fixP->fx_r_type)
  3810. {
  3811. case BFD_RELOC_32:
  3812. bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
  3813. break;
  3814. case BFD_RELOC_24:
  3815. case BFD_RELOC_M68HC11_24:
  3816. bfd_putb16 ((bfd_vma) (value & 0x0ffff), (unsigned char *) where);
  3817. ((bfd_byte*) where)[2] = ((value >> 16) & 0x0ff);
  3818. break;
  3819. case BFD_RELOC_16:
  3820. case BFD_RELOC_16_PCREL:
  3821. case BFD_RELOC_M68HC11_LO16:
  3822. bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
  3823. if (value < -65537 || value > 65535)
  3824. as_bad_where (fixP->fx_file, fixP->fx_line,
  3825. _("Value out of 16-bit range."));
  3826. break;
  3827. case BFD_RELOC_M68HC11_HI8:
  3828. /* Caution, %hi(<symbol>+%ld) will generate incorrect code if %lo
  3829. causes a carry. */
  3830. case BFD_RELOC_M68HC12_HI8XG:
  3831. value = value >> 8;
  3832. /* Fall through. */
  3833. case BFD_RELOC_M68HC12_LO8XG:
  3834. case BFD_RELOC_M68HC11_LO8:
  3835. case BFD_RELOC_8:
  3836. case BFD_RELOC_M68HC11_PAGE:
  3837. ((bfd_byte *) where)[0] = (bfd_byte) value;
  3838. break;
  3839. case BFD_RELOC_8_PCREL:
  3840. ((bfd_byte *) where)[0] = (bfd_byte) value;
  3841. if (value < -128 || value > 127)
  3842. as_bad_where (fixP->fx_file, fixP->fx_line,
  3843. _("Value %ld too large for 8-bit PC-relative branch."),
  3844. value);
  3845. break;
  3846. /* These next two are for XGATE. */
  3847. case BFD_RELOC_M68HC12_9_PCREL:
  3848. ((bfd_byte *) where)[0] |= (bfd_byte) ((value >>9) & 0x01);
  3849. ((bfd_byte *) where)[1] = (bfd_byte) ((value>>1) & 0xff);
  3850. if (value < -512 || value > 511)
  3851. as_bad_where (fixP->fx_file, fixP->fx_line,
  3852. _("Value %ld too large for 9-bit PC-relative branch."),
  3853. value);
  3854. break;
  3855. case BFD_RELOC_M68HC12_10_PCREL:
  3856. ((bfd_byte *) where)[0] |= (bfd_byte) ((value >>9) & 0x03);
  3857. ((bfd_byte *) where)[1] = (bfd_byte) ((value>>1) & 0xff);
  3858. if (value < -1024 || value > 1023)
  3859. as_bad_where (fixP->fx_file, fixP->fx_line,
  3860. _("Value %ld too large for 10-bit PC-relative branch."),
  3861. value);
  3862. break;
  3863. case BFD_RELOC_M68HC11_3B:
  3864. if (value <= 0 || value > 8)
  3865. as_bad_where (fixP->fx_file, fixP->fx_line,
  3866. _("Auto increment/decrement offset '%ld' is out of range."),
  3867. value);
  3868. if (where[0] & 0x8)
  3869. value = 8 - value;
  3870. else
  3871. value--;
  3872. where[0] = where[0] | (value & 0x07);
  3873. break;
  3874. case BFD_RELOC_M68HC12_5B:
  3875. if (value < -16 || value > 15)
  3876. as_bad_where (fixP->fx_file, fixP->fx_line,
  3877. _("Offset out of 5-bit range for movw/movb insn: %ld"),
  3878. value);
  3879. if (value >= 0)
  3880. where[0] |= value;
  3881. else
  3882. where[0] |= (0x10 | (16 + value));
  3883. break;
  3884. case BFD_RELOC_M68HC12_9B:
  3885. if (value < -256 || value > 255)
  3886. as_bad_where (fixP->fx_file, fixP->fx_line,
  3887. _("Offset out of 9-bit range for movw/movb insn: %ld"),
  3888. value);
  3889. /* sign bit already in xb postbyte */
  3890. if (value >= 0)
  3891. where[1] = value;
  3892. else
  3893. where[1] = (256 + value);
  3894. break;
  3895. case BFD_RELOC_M68HC12_16B:
  3896. if (value < -32768 || value > 32767)
  3897. as_bad_where (fixP->fx_file, fixP->fx_line,
  3898. _("Offset out of 16-bit range for movw/movb insn: %ld"),
  3899. value);
  3900. if (value < 0)
  3901. value += 65536;
  3902. where[0] = (value >> 8);
  3903. where[1] = (value & 0xff);
  3904. break;
  3905. case BFD_RELOC_M68HC11_RL_JUMP:
  3906. case BFD_RELOC_M68HC11_RL_GROUP:
  3907. case BFD_RELOC_VTABLE_INHERIT:
  3908. case BFD_RELOC_VTABLE_ENTRY:
  3909. fixP->fx_done = 0;
  3910. return;
  3911. default:
  3912. as_fatal (_("Line %d: unknown relocation type: 0x%x."),
  3913. fixP->fx_line, fixP->fx_r_type);
  3914. }
  3915. }
  3916. /* Set the ELF specific flags. */
  3917. void
  3918. m68hc11_elf_final_processing (void)
  3919. {
  3920. if (current_architecture & cpu6812s)
  3921. elf_flags |= EF_M68HCS12_MACH;
  3922. elf_elfheader (stdoutput)->e_flags &= ~EF_M68HC11_ABI;
  3923. elf_elfheader (stdoutput)->e_flags |= elf_flags;
  3924. }
  3925. /* Process directives specified via pseudo ops */
  3926. static void
  3927. s_m68hc11_parse_pseudo_instruction (int pseudo_insn)
  3928. {
  3929. switch (pseudo_insn)
  3930. {
  3931. case E_M68HC11_NO_BANK_WARNING:
  3932. elf_flags |= E_M68HC11_NO_BANK_WARNING;
  3933. break;
  3934. default:
  3935. as_bad (_("Invalid directive"));
  3936. }
  3937. }