tc-nios2.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112
  1. /* Altera Nios II assembler.
  2. Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. Contributed by Nigel Gray (ngray@altera.com).
  4. Contributed by Mentor Graphics, Inc.
  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 the Free
  16. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. #include "as.h"
  19. #include "opcode/nios2.h"
  20. #include "elf/nios2.h"
  21. #include "tc-nios2.h"
  22. #include "bfd.h"
  23. #include "libbfd.h"
  24. #include "dwarf2dbg.h"
  25. #include "subsegs.h"
  26. #include "safe-ctype.h"
  27. #include "dw2gencfi.h"
  28. #ifndef OBJ_ELF
  29. /* We are not supporting any other target so we throw a compile time error. */
  30. OBJ_ELF not defined
  31. #endif
  32. /* We can choose our endianness at run-time, regardless of configuration. */
  33. extern int target_big_endian;
  34. /* This array holds the chars that always start a comment. If the
  35. pre-processor is disabled, these aren't very useful. */
  36. const char comment_chars[] = "#";
  37. /* This array holds the chars that only start a comment at the beginning of
  38. a line. If the line seems to have the form '# 123 filename'
  39. .line and .file directives will appear in the pre-processed output. */
  40. /* Note that input_file.c hand checks for '#' at the beginning of the
  41. first line of the input file. This is because the compiler outputs
  42. #NO_APP at the beginning of its output. */
  43. /* Also note that C style comments are always supported. */
  44. const char line_comment_chars[] = "#";
  45. /* This array holds machine specific line separator characters. */
  46. const char line_separator_chars[] = ";";
  47. /* Chars that can be used to separate mant from exp in floating point nums. */
  48. const char EXP_CHARS[] = "eE";
  49. /* Chars that mean this number is a floating point constant. */
  50. /* As in 0f12.456 */
  51. /* or 0d1.2345e12 */
  52. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  53. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  54. changed in read.c. Ideally it shouldn't have to know about it at all,
  55. but nothing is ideal around here. */
  56. /* Machine-dependent command-line options. */
  57. const char *md_shortopts = "r";
  58. struct option md_longopts[] = {
  59. #define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
  60. {"relax-all", no_argument, NULL, OPTION_RELAX_ALL},
  61. #define OPTION_NORELAX (OPTION_MD_BASE + 1)
  62. {"no-relax", no_argument, NULL, OPTION_NORELAX},
  63. #define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
  64. {"relax-section", no_argument, NULL, OPTION_RELAX_SECTION},
  65. #define OPTION_EB (OPTION_MD_BASE + 3)
  66. {"EB", no_argument, NULL, OPTION_EB},
  67. #define OPTION_EL (OPTION_MD_BASE + 4)
  68. {"EL", no_argument, NULL, OPTION_EL},
  69. #define OPTION_MARCH (OPTION_MD_BASE + 5)
  70. {"march", required_argument, NULL, OPTION_MARCH}
  71. };
  72. size_t md_longopts_size = sizeof (md_longopts);
  73. /* The assembler supports three different relaxation modes, controlled by
  74. command-line options. */
  75. typedef enum
  76. {
  77. relax_section = 0,
  78. relax_none,
  79. relax_all
  80. } relax_optionT;
  81. /* Struct contains all assembler options set with .set. */
  82. struct
  83. {
  84. /* .set noat -> noat = 1 allows assembly code to use at without warning
  85. and macro expansions generate a warning.
  86. .set at -> noat = 0, assembly code using at warn but macro expansions
  87. do not generate warnings. */
  88. bfd_boolean noat;
  89. /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
  90. warning.
  91. .set break -> nobreak = 0, assembly code using ba,bt warns. */
  92. bfd_boolean nobreak;
  93. /* .cmd line option -relax-all allows all branches and calls to be replaced
  94. with longer versions.
  95. -no-relax inhibits branch/call conversion.
  96. The default value is relax_section, which relaxes branches within
  97. a section. */
  98. relax_optionT relax;
  99. } nios2_as_options = {FALSE, FALSE, relax_section};
  100. typedef struct nios2_insn_reloc
  101. {
  102. /* Any expression in the instruction is parsed into this field,
  103. which is passed to fix_new_exp() to generate a fixup. */
  104. expressionS reloc_expression;
  105. /* The type of the relocation to be applied. */
  106. bfd_reloc_code_real_type reloc_type;
  107. /* PC-relative. */
  108. unsigned int reloc_pcrel;
  109. /* The next relocation to be applied to the instruction. */
  110. struct nios2_insn_reloc *reloc_next;
  111. } nios2_insn_relocS;
  112. /* This struct is used to hold state when assembling instructions. */
  113. typedef struct nios2_insn_info
  114. {
  115. /* Assembled instruction. */
  116. unsigned long insn_code;
  117. /* Constant bits masked into insn_code for self-check mode. */
  118. unsigned long constant_bits;
  119. /* Pointer to the relevant bit of the opcode table. */
  120. const struct nios2_opcode *insn_nios2_opcode;
  121. /* After parsing ptrs to the tokens in the instruction fill this array
  122. it is terminated with a null pointer (hence the first +1).
  123. The second +1 is because in some parts of the code the opcode
  124. is not counted as a token, but still placed in this array. */
  125. const char *insn_tokens[NIOS2_MAX_INSN_TOKENS + 1 + 1];
  126. /* This holds information used to generate fixups
  127. and eventually relocations if it is not null. */
  128. nios2_insn_relocS *insn_reloc;
  129. } nios2_insn_infoS;
  130. /* This struct is used to convert Nios II pseudo-ops into the
  131. corresponding real op. */
  132. typedef struct nios2_ps_insn_info
  133. {
  134. /* Map this pseudo_op... */
  135. const char *pseudo_insn;
  136. /* ...to this real instruction. */
  137. const char *insn;
  138. /* Call this function to modify the operands.... */
  139. void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num,
  140. int start);
  141. /* ...with these arguments. */
  142. const char *arg_modifier;
  143. int num;
  144. int index;
  145. /* If arg_modifier_func allocates new memory, provide this function
  146. to free it afterwards. */
  147. void (*arg_cleanup_func) (char **parsed_args, int num, int start);
  148. } nios2_ps_insn_infoS;
  149. /* Opcode hash table. */
  150. static struct hash_control *nios2_opcode_hash = NULL;
  151. #define nios2_opcode_lookup(NAME) \
  152. ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
  153. /* Register hash table. */
  154. static struct hash_control *nios2_reg_hash = NULL;
  155. #define nios2_reg_lookup(NAME) \
  156. ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
  157. /* Pseudo-op hash table. */
  158. static struct hash_control *nios2_ps_hash = NULL;
  159. #define nios2_ps_lookup(NAME) \
  160. ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
  161. /* The known current alignment of the current section. */
  162. static int nios2_current_align;
  163. static segT nios2_current_align_seg;
  164. static int nios2_auto_align_on = 1;
  165. /* The last seen label in the current section. This is used to auto-align
  166. labels preceeding instructions. */
  167. static symbolS *nios2_last_label;
  168. /* If we saw a 16-bit CDX instruction, we can align on 2-byte boundaries
  169. instead of 4-bytes. Use this to keep track of the minimum power-of-2
  170. alignment. */
  171. static int nios2_min_align = 2;
  172. #ifdef OBJ_ELF
  173. /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
  174. symbolS *GOT_symbol;
  175. #endif
  176. /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
  177. static int nios2_architecture = EF_NIOS2_ARCH_R1;
  178. /** Utility routines. */
  179. /* Function md_chars_to_number takes the sequence of
  180. bytes in buf and returns the corresponding value
  181. in an int. n must be 1, 2 or 4. */
  182. static valueT
  183. md_chars_to_number (char *buf, int n)
  184. {
  185. int i;
  186. valueT val;
  187. gas_assert (n == 1 || n == 2 || n == 4);
  188. val = 0;
  189. if (target_big_endian)
  190. for (i = 0; i < n; ++i)
  191. val = val | ((buf[i] & 0xff) << 8 * (n - (i + 1)));
  192. else
  193. for (i = 0; i < n; ++i)
  194. val = val | ((buf[i] & 0xff) << 8 * i);
  195. return val;
  196. }
  197. /* This function turns a C long int, short int or char
  198. into the series of bytes that represent the number
  199. on the target machine. */
  200. void
  201. md_number_to_chars (char *buf, valueT val, int n)
  202. {
  203. gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
  204. if (target_big_endian)
  205. number_to_chars_bigendian (buf, val, n);
  206. else
  207. number_to_chars_littleendian (buf, val, n);
  208. }
  209. /* Turn a string in input_line_pointer into a floating point constant
  210. of type TYPE, and store the appropriate bytes in *LITP. The number
  211. of LITTLENUMS emitted is stored in *SIZEP. An error message is
  212. returned, or NULL on OK. */
  213. char *
  214. md_atof (int type, char *litP, int *sizeP)
  215. {
  216. int prec;
  217. LITTLENUM_TYPE words[4];
  218. char *t;
  219. int i;
  220. switch (type)
  221. {
  222. case 'f':
  223. prec = 2;
  224. break;
  225. case 'd':
  226. prec = 4;
  227. break;
  228. default:
  229. *sizeP = 0;
  230. return _("bad call to md_atof");
  231. }
  232. t = atof_ieee (input_line_pointer, type, words);
  233. if (t)
  234. input_line_pointer = t;
  235. *sizeP = prec * 2;
  236. if (! target_big_endian)
  237. for (i = prec - 1; i >= 0; i--, litP += 2)
  238. md_number_to_chars (litP, (valueT) words[i], 2);
  239. else
  240. for (i = 0; i < prec; i++, litP += 2)
  241. md_number_to_chars (litP, (valueT) words[i], 2);
  242. return NULL;
  243. }
  244. /* Return true if STR starts with PREFIX, which should be a string literal. */
  245. #define strprefix(STR, PREFIX) \
  246. (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
  247. /* Return true if STR is prefixed with a special relocation operator. */
  248. static int
  249. nios2_special_relocation_p (const char *str)
  250. {
  251. return (strprefix (str, "%lo")
  252. || strprefix (str, "%hi")
  253. || strprefix (str, "%hiadj")
  254. || strprefix (str, "%gprel")
  255. || strprefix (str, "%got")
  256. || strprefix (str, "%call")
  257. || strprefix (str, "%gotoff_lo")
  258. || strprefix (str, "%gotoff_hiadj")
  259. || strprefix (str, "%tls_gd")
  260. || strprefix (str, "%tls_ldm")
  261. || strprefix (str, "%tls_ldo")
  262. || strprefix (str, "%tls_ie")
  263. || strprefix (str, "%tls_le")
  264. || strprefix (str, "%gotoff"));
  265. }
  266. /* nop fill patterns for text section. */
  267. static char const nop_r1[4] = { 0x3a, 0x88, 0x01, 0x00 };
  268. static char const nop_r2[4] = { 0x20, 0x00, 0x00, 0xc4 };
  269. static char const nop_r2_cdx[2] = { 0x3b, 0x00 };
  270. static char const *nop32 = nop_r1;
  271. static char const *nop16 = NULL;
  272. /* Handles all machine-dependent alignment needs. */
  273. static void
  274. nios2_align (int log_size, const char *pfill, symbolS *label)
  275. {
  276. int align;
  277. long max_alignment = 15;
  278. /* The front end is prone to changing segments out from under us
  279. temporarily when -g is in effect. */
  280. int switched_seg_p = (nios2_current_align_seg != now_seg);
  281. align = log_size;
  282. if (align > max_alignment)
  283. {
  284. align = max_alignment;
  285. as_bad (_("Alignment too large: %d. assumed"), align);
  286. }
  287. else if (align < 0)
  288. {
  289. as_warn (_("Alignment negative: 0 assumed"));
  290. align = 0;
  291. }
  292. if (align != 0)
  293. {
  294. if (subseg_text_p (now_seg) && align >= nios2_min_align)
  295. {
  296. /* First, make sure we're on the minimum boundary, in case
  297. someone has been putting .byte values the text section. */
  298. if (nios2_current_align < nios2_min_align || switched_seg_p)
  299. frag_align (nios2_min_align, 0, 0);
  300. /* If we might be on a 2-byte boundary, first align to a
  301. 4-byte boundary using the 2-byte nop as fill. */
  302. if (nios2_min_align == 1
  303. && align > nios2_min_align
  304. && pfill == nop32 )
  305. {
  306. gas_assert (nop16);
  307. frag_align_pattern (2, nop16, 2, 0);
  308. }
  309. /* Now fill in the alignment pattern. */
  310. if (pfill != NULL)
  311. frag_align_pattern (align, pfill, 4, 0);
  312. else
  313. frag_align (align, 0, 0);
  314. }
  315. else
  316. frag_align (align, 0, 0);
  317. if (!switched_seg_p)
  318. nios2_current_align = align;
  319. /* If the last label was in a different section we can't align it. */
  320. if (label != NULL && !switched_seg_p)
  321. {
  322. symbolS *sym;
  323. int label_seen = FALSE;
  324. struct frag *old_frag;
  325. valueT old_value;
  326. valueT new_value;
  327. gas_assert (S_GET_SEGMENT (label) == now_seg);
  328. old_frag = symbol_get_frag (label);
  329. old_value = S_GET_VALUE (label);
  330. new_value = (valueT) frag_now_fix ();
  331. /* It is possible to have more than one label at a particular
  332. address, especially if debugging is enabled, so we must
  333. take care to adjust all the labels at this address in this
  334. fragment. To save time we search from the end of the symbol
  335. list, backwards, since the symbols we are interested in are
  336. almost certainly the ones that were most recently added.
  337. Also to save time we stop searching once we have seen at least
  338. one matching label, and we encounter a label that is no longer
  339. in the target fragment. Note, this search is guaranteed to
  340. find at least one match when sym == label, so no special case
  341. code is necessary. */
  342. for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
  343. if (symbol_get_frag (sym) == old_frag
  344. && S_GET_VALUE (sym) == old_value)
  345. {
  346. label_seen = TRUE;
  347. symbol_set_frag (sym, frag_now);
  348. S_SET_VALUE (sym, new_value);
  349. }
  350. else if (label_seen && symbol_get_frag (sym) != old_frag)
  351. break;
  352. }
  353. record_alignment (now_seg, align);
  354. }
  355. }
  356. /** Support for self-check mode. */
  357. /* Mode of the assembler. */
  358. typedef enum
  359. {
  360. NIOS2_MODE_ASSEMBLE, /* Ordinary operation. */
  361. NIOS2_MODE_TEST /* Hidden mode used for self testing. */
  362. } NIOS2_MODE;
  363. static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE;
  364. /* This function is used to in self-checking mode
  365. to check the assembled instruction
  366. opcode should be the assembled opcode, and exp_opcode
  367. the parsed string representing the expected opcode. */
  368. static void
  369. nios2_check_assembly (unsigned int opcode, const char *exp_opcode)
  370. {
  371. if (nios2_mode == NIOS2_MODE_TEST)
  372. {
  373. if (exp_opcode == NULL)
  374. as_bad (_("expecting opcode string in self test mode"));
  375. else if (opcode != strtoul (exp_opcode, NULL, 16))
  376. as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode);
  377. }
  378. }
  379. /** Support for machine-dependent assembler directives. */
  380. /* Handle the .align pseudo-op. This aligns to a power of two. It
  381. also adjusts any current instruction label. We treat this the same
  382. way the MIPS port does: .align 0 turns off auto alignment. */
  383. static void
  384. s_nios2_align (int ignore ATTRIBUTE_UNUSED)
  385. {
  386. int align;
  387. char fill;
  388. const char *pfill = NULL;
  389. long max_alignment = 15;
  390. align = get_absolute_expression ();
  391. if (align > max_alignment)
  392. {
  393. align = max_alignment;
  394. as_bad (_("Alignment too large: %d. assumed"), align);
  395. }
  396. else if (align < 0)
  397. {
  398. as_warn (_("Alignment negative: 0 assumed"));
  399. align = 0;
  400. }
  401. if (*input_line_pointer == ',')
  402. {
  403. input_line_pointer++;
  404. fill = get_absolute_expression ();
  405. pfill = (const char *) &fill;
  406. }
  407. else if (subseg_text_p (now_seg))
  408. pfill = (const char *) nop32;
  409. else
  410. {
  411. pfill = NULL;
  412. nios2_last_label = NULL;
  413. }
  414. if (align != 0)
  415. {
  416. nios2_auto_align_on = 1;
  417. nios2_align (align, pfill, nios2_last_label);
  418. nios2_last_label = NULL;
  419. }
  420. else
  421. nios2_auto_align_on = 0;
  422. demand_empty_rest_of_line ();
  423. }
  424. /* Handle the .text pseudo-op. This is like the usual one, but it
  425. clears the saved last label and resets known alignment. */
  426. static void
  427. s_nios2_text (int i)
  428. {
  429. s_text (i);
  430. nios2_last_label = NULL;
  431. nios2_current_align = 0;
  432. nios2_current_align_seg = now_seg;
  433. }
  434. /* Handle the .data pseudo-op. This is like the usual one, but it
  435. clears the saved last label and resets known alignment. */
  436. static void
  437. s_nios2_data (int i)
  438. {
  439. s_data (i);
  440. nios2_last_label = NULL;
  441. nios2_current_align = 0;
  442. nios2_current_align_seg = now_seg;
  443. }
  444. /* Handle the .section pseudo-op. This is like the usual one, but it
  445. clears the saved last label and resets known alignment. */
  446. static void
  447. s_nios2_section (int ignore)
  448. {
  449. obj_elf_section (ignore);
  450. nios2_last_label = NULL;
  451. nios2_current_align = 0;
  452. nios2_current_align_seg = now_seg;
  453. }
  454. /* Explicitly unaligned cons. */
  455. static void
  456. s_nios2_ucons (int nbytes)
  457. {
  458. int hold;
  459. hold = nios2_auto_align_on;
  460. nios2_auto_align_on = 0;
  461. cons (nbytes);
  462. nios2_auto_align_on = hold;
  463. }
  464. /* Handle the .sdata directive. */
  465. static void
  466. s_nios2_sdata (int ignore ATTRIBUTE_UNUSED)
  467. {
  468. get_absolute_expression (); /* Ignored. */
  469. subseg_new (".sdata", 0);
  470. demand_empty_rest_of_line ();
  471. }
  472. /* .set sets assembler options eg noat/at and is also used
  473. to set symbol values (.equ, .equiv ). */
  474. static void
  475. s_nios2_set (int equiv)
  476. {
  477. char *save = input_line_pointer;
  478. char *directive;
  479. char delim = get_symbol_name (&directive);
  480. char *endline = input_line_pointer;
  481. (void) restore_line_pointer (delim);
  482. /* We only want to handle ".set XXX" if the
  483. user has tried ".set XXX, YYY" they are not
  484. trying a directive. This prevents
  485. us from polluting the name space. */
  486. SKIP_WHITESPACE ();
  487. if (is_end_of_line[(unsigned char) *input_line_pointer])
  488. {
  489. bfd_boolean done = TRUE;
  490. *endline = 0;
  491. if (!strcmp (directive, "noat"))
  492. nios2_as_options.noat = TRUE;
  493. else if (!strcmp (directive, "at"))
  494. nios2_as_options.noat = FALSE;
  495. else if (!strcmp (directive, "nobreak"))
  496. nios2_as_options.nobreak = TRUE;
  497. else if (!strcmp (directive, "break"))
  498. nios2_as_options.nobreak = FALSE;
  499. else if (!strcmp (directive, "norelax"))
  500. nios2_as_options.relax = relax_none;
  501. else if (!strcmp (directive, "relaxsection"))
  502. nios2_as_options.relax = relax_section;
  503. else if (!strcmp (directive, "relaxall"))
  504. nios2_as_options.relax = relax_all;
  505. else
  506. done = FALSE;
  507. if (done)
  508. {
  509. *endline = delim;
  510. demand_empty_rest_of_line ();
  511. return;
  512. }
  513. }
  514. /* If we fall through to here, either we have ".set XXX, YYY"
  515. or we have ".set XXX" where XXX is unknown or we have
  516. a syntax error. */
  517. input_line_pointer = save;
  518. s_set (equiv);
  519. }
  520. /* Machine-dependent assembler directives.
  521. Format of each entry is:
  522. { "directive", handler_func, param } */
  523. const pseudo_typeS md_pseudo_table[] = {
  524. {"align", s_nios2_align, 0},
  525. {"text", s_nios2_text, 0},
  526. {"data", s_nios2_data, 0},
  527. {"section", s_nios2_section, 0},
  528. {"section.s", s_nios2_section, 0},
  529. {"sect", s_nios2_section, 0},
  530. {"sect.s", s_nios2_section, 0},
  531. /* .dword and .half are included for compatibility with MIPS. */
  532. {"dword", cons, 8},
  533. {"half", cons, 2},
  534. /* NIOS2 native word size is 4 bytes, so we override
  535. the GAS default of 2. */
  536. {"word", cons, 4},
  537. /* Explicitly unaligned directives. */
  538. {"2byte", s_nios2_ucons, 2},
  539. {"4byte", s_nios2_ucons, 4},
  540. {"8byte", s_nios2_ucons, 8},
  541. {"16byte", s_nios2_ucons, 16},
  542. #ifdef OBJ_ELF
  543. {"sdata", s_nios2_sdata, 0},
  544. #endif
  545. {"set", s_nios2_set, 0},
  546. {NULL, NULL, 0}
  547. };
  548. /** Relaxation support. */
  549. /* We support two relaxation modes: a limited PC-relative mode with
  550. -relax-section (the default), and an absolute jump mode with -relax-all.
  551. Nios II PC-relative branch instructions only support 16-bit offsets.
  552. And, there's no good way to add a 32-bit constant to the PC without
  553. using two registers.
  554. To deal with this, for the pc-relative relaxation mode we convert
  555. br label
  556. into a series of 16-bit adds, like:
  557. nextpc at
  558. addi at, at, 32767
  559. ...
  560. addi at, at, remainder
  561. jmp at
  562. Similarly, conditional branches are converted from
  563. b(condition) r, s, label
  564. into a series like:
  565. b(opposite condition) r, s, skip
  566. nextpc at
  567. addi at, at, 32767
  568. ...
  569. addi at, at, remainder
  570. jmp at
  571. skip:
  572. The compiler can do a better job, either by converting the branch
  573. directly into a JMP (going through the GOT for PIC) or by allocating
  574. a second register for the 32-bit displacement.
  575. For the -relax-all relaxation mode, the conversions are
  576. movhi at, %hi(symbol+offset)
  577. ori at, %lo(symbol+offset)
  578. jmp at
  579. and
  580. b(opposite condition), r, s, skip
  581. movhi at, %hi(symbol+offset)
  582. ori at, %lo(symbol+offset)
  583. jmp at
  584. skip:
  585. respectively.
  586. 16-bit CDX branch instructions are relaxed first into equivalent
  587. 32-bit branches and then the above transformations are applied
  588. if necessary.
  589. */
  590. /* Arbitrarily limit the number of addis we can insert; we need to be able
  591. to specify the maximum growth size for each frag that contains a
  592. relaxable branch. There's no point in specifying a huge number here
  593. since that means the assembler needs to allocate that much extra
  594. memory for every branch, and almost no real code will ever need it.
  595. Plus, as already noted a better solution is to just use a jmp, or
  596. allocate a second register to hold a 32-bit displacement.
  597. FIXME: Rather than making this a constant, it could be controlled by
  598. a command-line argument. */
  599. #define RELAX_MAX_ADDI 32
  600. /* The fr_subtype field represents the target-specific relocation state.
  601. It has type relax_substateT (unsigned int). We use it to track the
  602. number of addis necessary, plus a bit to track whether this is a
  603. conditional branch and a bit for 16-bit CDX instructions.
  604. Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
  605. in the fr_subtype to encode the number of addis so that the whole
  606. theoretically-valid range is representable.
  607. For the -relax-all mode, N = 0 represents an in-range branch and N = 1
  608. represents a branch that needs to be relaxed. */
  609. #define UBRANCH (0 << 16)
  610. #define CBRANCH (1 << 16)
  611. #define CDXBRANCH (1 << 17)
  612. #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
  613. #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
  614. #define IS_CDXBRANCH(SUBTYPE) ((SUBTYPE) & CDXBRANCH)
  615. #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
  616. #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
  617. #define CDX_UBRANCH_SUBTYPE(N) (CDXBRANCH | UBRANCH | (N))
  618. #define CDX_CBRANCH_SUBTYPE(N) (CDXBRANCH | CBRANCH | (N))
  619. #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
  620. /* For the -relax-section mode, unconditional branches require 2 extra i
  621. nstructions besides the addis, conditional branches require 3. */
  622. #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
  623. #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
  624. /* For the -relax-all mode, unconditional branches require 3 instructions
  625. and conditional branches require 4. */
  626. #define UBRANCH_JUMP_SIZE 12
  627. #define CBRANCH_JUMP_SIZE 16
  628. /* Maximum sizes of relaxation sequences. */
  629. #define UBRANCH_MAX_SIZE \
  630. (nios2_as_options.relax == relax_all \
  631. ? UBRANCH_JUMP_SIZE \
  632. : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
  633. #define CBRANCH_MAX_SIZE \
  634. (nios2_as_options.relax == relax_all \
  635. ? CBRANCH_JUMP_SIZE \
  636. : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
  637. /* Register number of AT, the assembler temporary. */
  638. #define AT_REGNUM 1
  639. /* Determine how many bytes are required to represent the sequence
  640. indicated by SUBTYPE. */
  641. static int
  642. nios2_relax_subtype_size (relax_substateT subtype)
  643. {
  644. int n = SUBTYPE_ADDIS (subtype);
  645. if (n == 0)
  646. /* Regular conditional/unconditional branch instruction. */
  647. return (IS_CDXBRANCH (subtype) ? 2 : 4);
  648. else if (nios2_as_options.relax == relax_all)
  649. return (IS_CBRANCH (subtype) ? CBRANCH_JUMP_SIZE : UBRANCH_JUMP_SIZE);
  650. else if (IS_CBRANCH (subtype))
  651. return CBRANCH_ADDIS_TO_SIZE (n);
  652. else
  653. return UBRANCH_ADDIS_TO_SIZE (n);
  654. }
  655. /* Estimate size of fragp before relaxation.
  656. This could also examine the offset in fragp and adjust
  657. fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
  658. int
  659. md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED)
  660. {
  661. return nios2_relax_subtype_size (fragp->fr_subtype);
  662. }
  663. /* Implement md_relax_frag, returning the change in size of the frag. */
  664. long
  665. nios2_relax_frag (segT segment, fragS *fragp, long stretch)
  666. {
  667. addressT target = fragp->fr_offset;
  668. relax_substateT subtype = fragp->fr_subtype;
  669. symbolS *symbolp = fragp->fr_symbol;
  670. if (symbolp)
  671. {
  672. fragS *sym_frag = symbol_get_frag (symbolp);
  673. offsetT offset;
  674. int n;
  675. bfd_boolean is_cdx = FALSE;
  676. target += S_GET_VALUE (symbolp);
  677. /* See comments in write.c:relax_frag about handling of stretch. */
  678. if (stretch != 0
  679. && sym_frag->relax_marker != fragp->relax_marker)
  680. {
  681. if (stretch < 0 || sym_frag->region == fragp->region)
  682. target += stretch;
  683. else if (target < fragp->fr_address)
  684. target = fragp->fr_next->fr_address + stretch;
  685. }
  686. /* We subtract fr_var (4 for 32-bit insns) because all pc relative
  687. branches are from the next instruction. */
  688. offset = target - fragp->fr_address - fragp->fr_fix - fragp->fr_var;
  689. if (IS_CDXBRANCH (subtype) && IS_UBRANCH (subtype)
  690. && offset >= -1024 && offset < 1024)
  691. /* PC-relative CDX branch with 11-bit offset. */
  692. is_cdx = TRUE;
  693. else if (IS_CDXBRANCH (subtype) && IS_CBRANCH (subtype)
  694. && offset >= -128 && offset < 128)
  695. /* PC-relative CDX branch with 8-bit offset. */
  696. is_cdx = TRUE;
  697. else if (offset >= -32768 && offset < 32768)
  698. /* Fits in PC-relative branch. */
  699. n = 0;
  700. else if (nios2_as_options.relax == relax_all)
  701. /* Convert to jump. */
  702. n = 1;
  703. else if (nios2_as_options.relax == relax_section
  704. && S_GET_SEGMENT (symbolp) == segment
  705. && S_IS_DEFINED (symbolp))
  706. /* Attempt a PC-relative relaxation on a branch to a defined
  707. symbol in the same segment. */
  708. {
  709. /* The relaxation for conditional branches is offset by 4
  710. bytes because we insert the inverted branch around the
  711. sequence. */
  712. if (IS_CBRANCH (subtype))
  713. offset = offset - 4;
  714. if (offset > 0)
  715. n = offset / 32767 + 1;
  716. else
  717. n = offset / -32768 + 1;
  718. /* Bail out immediately if relaxation has failed. If we try to
  719. defer the diagnostic to md_convert_frag, some pathological test
  720. cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
  721. apparently never converge. By returning 0 here we could pretend
  722. to the caller that nothing has changed, but that leaves things
  723. in an inconsistent state when we get to md_convert_frag. */
  724. if (n > RELAX_MAX_ADDI)
  725. {
  726. as_bad_where (fragp->fr_file, fragp->fr_line,
  727. _("branch offset out of range\n"));
  728. as_fatal (_("branch relaxation failed\n"));
  729. }
  730. }
  731. else
  732. /* We cannot handle this case, diagnose overflow later. */
  733. return 0;
  734. if (is_cdx)
  735. fragp->fr_subtype = subtype;
  736. else if (IS_CBRANCH (subtype))
  737. fragp->fr_subtype = CBRANCH_SUBTYPE (n);
  738. else
  739. fragp->fr_subtype = UBRANCH_SUBTYPE (n);
  740. return (nios2_relax_subtype_size (fragp->fr_subtype)
  741. - nios2_relax_subtype_size (subtype));
  742. }
  743. /* If we got here, it's probably an error. */
  744. return 0;
  745. }
  746. /* Complete fragp using the data from the relaxation pass. */
  747. void
  748. md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
  749. fragS *fragp)
  750. {
  751. char *buffer = fragp->fr_literal + fragp->fr_fix;
  752. relax_substateT subtype = fragp->fr_subtype;
  753. int n = SUBTYPE_ADDIS (subtype);
  754. addressT target = fragp->fr_offset;
  755. symbolS *symbolp = fragp->fr_symbol;
  756. offsetT offset;
  757. unsigned int addend_mask, addi_mask, op;
  758. offsetT addend, remainder;
  759. int i;
  760. bfd_boolean is_r2 = (bfd_get_mach (stdoutput) == bfd_mach_nios2r2);
  761. /* If this is a CDX branch we're not relaxing, just generate the fixup. */
  762. if (IS_CDXBRANCH (subtype))
  763. {
  764. gas_assert (is_r2);
  765. fix_new (fragp, fragp->fr_fix, 2, fragp->fr_symbol,
  766. fragp->fr_offset, 1,
  767. (IS_UBRANCH (subtype)
  768. ? BFD_RELOC_NIOS2_R2_I10_1_PCREL
  769. : BFD_RELOC_NIOS2_R2_T1I7_1_PCREL));
  770. fragp->fr_fix += 2;
  771. return;
  772. }
  773. /* If this is a CDX branch we are relaxing, turn it into an equivalent
  774. 32-bit branch and then fall through to the normal non-CDX cases. */
  775. if (fragp->fr_var == 2)
  776. {
  777. unsigned int opcode = md_chars_to_number (buffer, 2);
  778. gas_assert (is_r2);
  779. if (IS_CBRANCH (subtype))
  780. {
  781. unsigned int reg = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (opcode)];
  782. if (GET_IW_R2_OP (opcode) == R2_OP_BNEZ_N)
  783. opcode = MATCH_R2_BNE | SET_IW_F2I16_A (reg);
  784. else
  785. opcode = MATCH_R2_BEQ | SET_IW_F2I16_A (reg);
  786. }
  787. else
  788. opcode = MATCH_R2_BR;
  789. md_number_to_chars (buffer, opcode, 4);
  790. fragp->fr_var = 4;
  791. }
  792. /* If we didn't or can't relax, this is a regular branch instruction.
  793. We just need to generate the fixup for the symbol and offset. */
  794. if (n == 0)
  795. {
  796. fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol,
  797. fragp->fr_offset, 1, BFD_RELOC_16_PCREL);
  798. fragp->fr_fix += 4;
  799. return;
  800. }
  801. /* Replace the cbranch at fr_fix with one that has the opposite condition
  802. in order to jump around the block of instructions we'll be adding. */
  803. if (IS_CBRANCH (subtype))
  804. {
  805. unsigned int br_opcode;
  806. unsigned int old_op, new_op;
  807. int nbytes;
  808. /* Account for the nextpc and jmp in the pc-relative case, or the two
  809. load instructions and jump in the absolute case. */
  810. if (nios2_as_options.relax == relax_section)
  811. nbytes = (n + 2) * 4;
  812. else
  813. nbytes = 12;
  814. br_opcode = md_chars_to_number (buffer, 4);
  815. if (is_r2)
  816. {
  817. old_op = GET_IW_R2_OP (br_opcode);
  818. switch (old_op)
  819. {
  820. case R2_OP_BEQ:
  821. new_op = R2_OP_BNE;
  822. break;
  823. case R2_OP_BNE:
  824. new_op = R2_OP_BEQ;
  825. break;
  826. case R2_OP_BGE:
  827. new_op = R2_OP_BLT;
  828. break;
  829. case R2_OP_BGEU:
  830. new_op = R2_OP_BLTU;
  831. break;
  832. case R2_OP_BLT:
  833. new_op = R2_OP_BGE;
  834. break;
  835. case R2_OP_BLTU:
  836. new_op = R2_OP_BGEU;
  837. break;
  838. default:
  839. abort ();
  840. }
  841. br_opcode = ((br_opcode & ~IW_R2_OP_SHIFTED_MASK)
  842. | SET_IW_R2_OP (new_op));
  843. br_opcode = br_opcode | SET_IW_F2I16_IMM16 (nbytes);
  844. }
  845. else
  846. {
  847. old_op = GET_IW_R1_OP (br_opcode);
  848. switch (old_op)
  849. {
  850. case R1_OP_BEQ:
  851. new_op = R1_OP_BNE;
  852. break;
  853. case R1_OP_BNE:
  854. new_op = R1_OP_BEQ;
  855. break;
  856. case R1_OP_BGE:
  857. new_op = R1_OP_BLT;
  858. break;
  859. case R1_OP_BGEU:
  860. new_op = R1_OP_BLTU;
  861. break;
  862. case R1_OP_BLT:
  863. new_op = R1_OP_BGE;
  864. break;
  865. case R1_OP_BLTU:
  866. new_op = R1_OP_BGEU;
  867. break;
  868. default:
  869. abort ();
  870. }
  871. br_opcode = ((br_opcode & ~IW_R1_OP_SHIFTED_MASK)
  872. | SET_IW_R1_OP (new_op));
  873. br_opcode = br_opcode | SET_IW_I_IMM16 (nbytes);
  874. }
  875. md_number_to_chars (buffer, br_opcode, 4);
  876. fragp->fr_fix += 4;
  877. buffer += 4;
  878. }
  879. /* Load at for the PC-relative case. */
  880. if (nios2_as_options.relax == relax_section)
  881. {
  882. /* Insert the nextpc instruction. */
  883. if (is_r2)
  884. op = MATCH_R2_NEXTPC | SET_IW_F3X6L5_C (AT_REGNUM);
  885. else
  886. op = MATCH_R1_NEXTPC | SET_IW_R_C (AT_REGNUM);
  887. md_number_to_chars (buffer, op, 4);
  888. fragp->fr_fix += 4;
  889. buffer += 4;
  890. /* We need to know whether the offset is positive or negative. */
  891. target += S_GET_VALUE (symbolp);
  892. offset = target - fragp->fr_address - fragp->fr_fix;
  893. if (offset > 0)
  894. addend = 32767;
  895. else
  896. addend = -32768;
  897. if (is_r2)
  898. addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)addend);
  899. else
  900. addend_mask = SET_IW_I_IMM16 ((unsigned int)addend);
  901. /* Insert n-1 addi instructions. */
  902. if (is_r2)
  903. addi_mask = (MATCH_R2_ADDI
  904. | SET_IW_F2I16_B (AT_REGNUM)
  905. | SET_IW_F2I16_A (AT_REGNUM));
  906. else
  907. addi_mask = (MATCH_R1_ADDI
  908. | SET_IW_I_B (AT_REGNUM)
  909. | SET_IW_I_A (AT_REGNUM));
  910. for (i = 0; i < n - 1; i ++)
  911. {
  912. md_number_to_chars (buffer, addi_mask | addend_mask, 4);
  913. fragp->fr_fix += 4;
  914. buffer += 4;
  915. }
  916. /* Insert the last addi instruction to hold the remainder. */
  917. remainder = offset - addend * (n - 1);
  918. gas_assert (remainder >= -32768 && remainder <= 32767);
  919. if (is_r2)
  920. addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)remainder);
  921. else
  922. addend_mask = SET_IW_I_IMM16 ((unsigned int)remainder);
  923. md_number_to_chars (buffer, addi_mask | addend_mask, 4);
  924. fragp->fr_fix += 4;
  925. buffer += 4;
  926. }
  927. /* Load at for the absolute case. */
  928. else
  929. {
  930. if (is_r2)
  931. op = MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM) | SET_IW_F2I16_A (0);
  932. else
  933. op = MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM) | SET_IW_I_A (0);
  934. md_number_to_chars (buffer, op, 4);
  935. fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
  936. 0, BFD_RELOC_NIOS2_HI16);
  937. fragp->fr_fix += 4;
  938. buffer += 4;
  939. if (is_r2)
  940. op = (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM)
  941. | SET_IW_F2I16_A (AT_REGNUM));
  942. else
  943. op = (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
  944. | SET_IW_I_A (AT_REGNUM));
  945. md_number_to_chars (buffer, op, 4);
  946. fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
  947. 0, BFD_RELOC_NIOS2_LO16);
  948. fragp->fr_fix += 4;
  949. buffer += 4;
  950. }
  951. /* Insert the jmp instruction. */
  952. if (is_r2)
  953. op = MATCH_R2_JMP | SET_IW_F3X6L5_A (AT_REGNUM);
  954. else
  955. op = MATCH_R1_JMP | SET_IW_R_A (AT_REGNUM);
  956. md_number_to_chars (buffer, op, 4);
  957. fragp->fr_fix += 4;
  958. buffer += 4;
  959. }
  960. /** Fixups and overflow checking. */
  961. /* Check a fixup for overflow. */
  962. static bfd_boolean
  963. nios2_check_overflow (valueT fixup, reloc_howto_type *howto)
  964. {
  965. /* If there is a rightshift, check that the low-order bits are
  966. zero before applying it. */
  967. if (howto->rightshift)
  968. {
  969. if ((~(~((valueT) 0) << howto->rightshift) & fixup)
  970. && howto->complain_on_overflow != complain_overflow_dont)
  971. return TRUE;
  972. fixup = ((signed)fixup) >> howto->rightshift;
  973. }
  974. /* Check for overflow - return TRUE if overflow, FALSE if not. */
  975. switch (howto->complain_on_overflow)
  976. {
  977. case complain_overflow_dont:
  978. break;
  979. case complain_overflow_bitfield:
  980. if ((fixup >> howto->bitsize) != 0
  981. && ((signed) fixup >> howto->bitsize) != -1)
  982. return TRUE;
  983. break;
  984. case complain_overflow_signed:
  985. if ((fixup & 0x80000000) > 0)
  986. {
  987. /* Check for negative overflow. */
  988. if ((signed) fixup < ((signed) ~0 << (howto->bitsize-1)))
  989. return TRUE;
  990. }
  991. else
  992. {
  993. /* Check for positive overflow. */
  994. if (fixup >= ((unsigned) 1 << (howto->bitsize - 1)))
  995. return TRUE;
  996. }
  997. break;
  998. case complain_overflow_unsigned:
  999. if ((fixup >> howto->bitsize) != 0)
  1000. return TRUE;
  1001. break;
  1002. default:
  1003. as_bad (_("error checking for overflow - broken assembler"));
  1004. break;
  1005. }
  1006. return FALSE;
  1007. }
  1008. /* Emit diagnostic for fixup overflow. */
  1009. static void
  1010. nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
  1011. fixS *fixP, valueT value)
  1012. {
  1013. if (fixP->fx_r_type == BFD_RELOC_8
  1014. || fixP->fx_r_type == BFD_RELOC_16
  1015. || fixP->fx_r_type == BFD_RELOC_32)
  1016. /* These relocs are against data, not instructions. */
  1017. as_bad_where (fixP->fx_file, fixP->fx_line,
  1018. _("immediate value 0x%x truncated to 0x%x"),
  1019. (unsigned int) fixup,
  1020. (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup));
  1021. else
  1022. {
  1023. /* What opcode is the instruction? This will determine
  1024. whether we check for overflow in immediate values
  1025. and what error message we get. */
  1026. const struct nios2_opcode *opcode;
  1027. enum overflow_type overflow_msg_type;
  1028. unsigned int range_min;
  1029. unsigned int range_max;
  1030. unsigned int address;
  1031. opcode = nios2_find_opcode_hash (value, bfd_get_mach (stdoutput));
  1032. gas_assert (opcode);
  1033. gas_assert (fixP->fx_size == opcode->size);
  1034. overflow_msg_type = opcode->overflow_msg;
  1035. switch (overflow_msg_type)
  1036. {
  1037. case call_target_overflow:
  1038. range_min
  1039. = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
  1040. range_max = range_min + 0x0fffffff;
  1041. address = fixup | range_min;
  1042. as_bad_where (fixP->fx_file, fixP->fx_line,
  1043. _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
  1044. address, range_min, range_max);
  1045. break;
  1046. case branch_target_overflow:
  1047. if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type)
  1048. as_bad_where (fixP->fx_file, fixP->fx_line,
  1049. _("branch offset %d out of range %d to %d"),
  1050. (int)fixup, -32768, 32767);
  1051. else
  1052. as_bad_where (fixP->fx_file, fixP->fx_line,
  1053. _("branch offset %d out of range"),
  1054. (int)fixup);
  1055. break;
  1056. case address_offset_overflow:
  1057. if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type)
  1058. as_bad_where (fixP->fx_file, fixP->fx_line,
  1059. _("%s offset %d out of range %d to %d"),
  1060. opcode->name, (int)fixup, -32768, 32767);
  1061. else
  1062. as_bad_where (fixP->fx_file, fixP->fx_line,
  1063. _("%s offset %d out of range"),
  1064. opcode->name, (int)fixup);
  1065. break;
  1066. case signed_immed16_overflow:
  1067. as_bad_where (fixP->fx_file, fixP->fx_line,
  1068. _("immediate value %d out of range %d to %d"),
  1069. (int)fixup, -32768, 32767);
  1070. break;
  1071. case unsigned_immed16_overflow:
  1072. as_bad_where (fixP->fx_file, fixP->fx_line,
  1073. _("immediate value %u out of range %u to %u"),
  1074. (unsigned int)fixup, 0, 65535);
  1075. break;
  1076. case unsigned_immed5_overflow:
  1077. as_bad_where (fixP->fx_file, fixP->fx_line,
  1078. _("immediate value %u out of range %u to %u"),
  1079. (unsigned int)fixup, 0, 31);
  1080. break;
  1081. case signed_immed12_overflow:
  1082. as_bad_where (fixP->fx_file, fixP->fx_line,
  1083. _("immediate value %d out of range %d to %d"),
  1084. (int)fixup, -2048, 2047);
  1085. break;
  1086. case custom_opcode_overflow:
  1087. as_bad_where (fixP->fx_file, fixP->fx_line,
  1088. _("custom instruction opcode %u out of range %u to %u"),
  1089. (unsigned int)fixup, 0, 255);
  1090. break;
  1091. default:
  1092. as_bad_where (fixP->fx_file, fixP->fx_line,
  1093. _("overflow in immediate argument"));
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. /* Apply a fixup to the object file. */
  1099. void
  1100. md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
  1101. {
  1102. /* Assert that the fixup is one we can handle. */
  1103. gas_assert (fixP != NULL && valP != NULL
  1104. && (fixP->fx_r_type == BFD_RELOC_8
  1105. || fixP->fx_r_type == BFD_RELOC_16
  1106. || fixP->fx_r_type == BFD_RELOC_32
  1107. || fixP->fx_r_type == BFD_RELOC_64
  1108. || fixP->fx_r_type == BFD_RELOC_NIOS2_S16
  1109. || fixP->fx_r_type == BFD_RELOC_NIOS2_U16
  1110. || fixP->fx_r_type == BFD_RELOC_16_PCREL
  1111. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26
  1112. || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM5
  1113. || fixP->fx_r_type == BFD_RELOC_NIOS2_CACHE_OPX
  1114. || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM6
  1115. || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM8
  1116. || fixP->fx_r_type == BFD_RELOC_NIOS2_HI16
  1117. || fixP->fx_r_type == BFD_RELOC_NIOS2_LO16
  1118. || fixP->fx_r_type == BFD_RELOC_NIOS2_HIADJ16
  1119. || fixP->fx_r_type == BFD_RELOC_NIOS2_GPREL
  1120. || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  1121. || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
  1122. || fixP->fx_r_type == BFD_RELOC_NIOS2_UJMP
  1123. || fixP->fx_r_type == BFD_RELOC_NIOS2_CJMP
  1124. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALLR
  1125. || fixP->fx_r_type == BFD_RELOC_NIOS2_ALIGN
  1126. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT16
  1127. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL16
  1128. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
  1129. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
  1130. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
  1131. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
  1132. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
  1133. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
  1134. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
  1135. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
  1136. || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
  1137. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26_NOAT
  1138. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
  1139. || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
  1140. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
  1141. || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
  1142. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_S12
  1143. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_I10_1_PCREL
  1144. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
  1145. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_2
  1146. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4
  1147. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_1
  1148. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_2
  1149. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X1I7_2
  1150. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X2L5
  1151. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_F1I5_2
  1152. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_L5I4X1
  1153. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6
  1154. || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6_2
  1155. /* Add other relocs here as we generate them. */
  1156. ));
  1157. if (fixP->fx_r_type == BFD_RELOC_64)
  1158. {
  1159. /* We may reach here due to .8byte directives, but we never output
  1160. BFD_RELOC_64; it must be resolved. */
  1161. if (fixP->fx_addsy != NULL)
  1162. as_bad_where (fixP->fx_file, fixP->fx_line,
  1163. _("cannot create 64-bit relocation"));
  1164. else
  1165. {
  1166. md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
  1167. *valP, 8);
  1168. fixP->fx_done = 1;
  1169. }
  1170. return;
  1171. }
  1172. /* The value passed in valP can be the value of a fully
  1173. resolved expression, or it can be the value of a partially
  1174. resolved expression. In the former case, both fixP->fx_addsy
  1175. and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
  1176. we can fix up the instruction that fixP relates to.
  1177. In the latter case, one or both of fixP->fx_addsy and
  1178. fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
  1179. equal *valP. We don't need to check for fixP->fx_subsy being null
  1180. because the generic part of the assembler generates an error if
  1181. it is not an absolute symbol. */
  1182. if (fixP->fx_addsy != NULL)
  1183. /* Partially resolved expression. */
  1184. {
  1185. fixP->fx_addnumber = fixP->fx_offset;
  1186. fixP->fx_done = 0;
  1187. switch (fixP->fx_r_type)
  1188. {
  1189. case BFD_RELOC_NIOS2_TLS_GD16:
  1190. case BFD_RELOC_NIOS2_TLS_LDM16:
  1191. case BFD_RELOC_NIOS2_TLS_LDO16:
  1192. case BFD_RELOC_NIOS2_TLS_IE16:
  1193. case BFD_RELOC_NIOS2_TLS_LE16:
  1194. case BFD_RELOC_NIOS2_TLS_DTPMOD:
  1195. case BFD_RELOC_NIOS2_TLS_DTPREL:
  1196. case BFD_RELOC_NIOS2_TLS_TPREL:
  1197. S_SET_THREAD_LOCAL (fixP->fx_addsy);
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. }
  1203. else
  1204. /* Fully resolved fixup. */
  1205. {
  1206. reloc_howto_type *howto
  1207. = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
  1208. if (howto == NULL)
  1209. as_bad_where (fixP->fx_file, fixP->fx_line,
  1210. _("relocation is not supported"));
  1211. else
  1212. {
  1213. valueT fixup = *valP;
  1214. valueT value;
  1215. char *buf;
  1216. /* If this is a pc-relative relocation, we need to
  1217. subtract the current offset within the object file
  1218. FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
  1219. so I'm using the howto structure instead to determine this. */
  1220. if (howto->pc_relative == 1)
  1221. {
  1222. fixup = (fixup - (fixP->fx_frag->fr_address + fixP->fx_where
  1223. + fixP->fx_size));
  1224. *valP = fixup;
  1225. }
  1226. /* Get the instruction or data to be fixed up. */
  1227. buf = fixP->fx_frag->fr_literal + fixP->fx_where;
  1228. value = md_chars_to_number (buf, fixP->fx_size);
  1229. /* Check for overflow, emitting a diagnostic if necessary. */
  1230. if (nios2_check_overflow (fixup, howto))
  1231. nios2_diagnose_overflow (fixup, howto, fixP, value);
  1232. /* Apply the right shift. */
  1233. fixup = ((signed)fixup) >> howto->rightshift;
  1234. /* Truncate the fixup to right size. */
  1235. switch (fixP->fx_r_type)
  1236. {
  1237. case BFD_RELOC_NIOS2_HI16:
  1238. fixup = (fixup >> 16) & 0xFFFF;
  1239. break;
  1240. case BFD_RELOC_NIOS2_LO16:
  1241. fixup = fixup & 0xFFFF;
  1242. break;
  1243. case BFD_RELOC_NIOS2_HIADJ16:
  1244. fixup = ((((fixup >> 16) & 0xFFFF) + ((fixup >> 15) & 0x01))
  1245. & 0xFFFF);
  1246. break;
  1247. default:
  1248. {
  1249. int n = sizeof (fixup) * 8 - howto->bitsize;
  1250. fixup = (fixup << n) >> n;
  1251. break;
  1252. }
  1253. }
  1254. /* Fix up the instruction. */
  1255. value = (value & ~howto->dst_mask) | (fixup << howto->bitpos);
  1256. md_number_to_chars (buf, value, fixP->fx_size);
  1257. }
  1258. fixP->fx_done = 1;
  1259. }
  1260. if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
  1261. {
  1262. fixP->fx_done = 0;
  1263. if (fixP->fx_addsy
  1264. && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
  1265. S_SET_WEAK (fixP->fx_addsy);
  1266. }
  1267. else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  1268. fixP->fx_done = 0;
  1269. }
  1270. /** Instruction parsing support. */
  1271. /* General internal error routine. */
  1272. static void
  1273. bad_opcode (const struct nios2_opcode *op)
  1274. {
  1275. fprintf (stderr, _("internal error: broken opcode descriptor for `%s %s'\n"),
  1276. op->name, op->args);
  1277. as_fatal (_("Broken assembler. No assembly attempted."));
  1278. }
  1279. /* Special relocation directive strings. */
  1280. struct nios2_special_relocS
  1281. {
  1282. const char *string;
  1283. bfd_reloc_code_real_type reloc_type;
  1284. };
  1285. /* This table is sorted so that prefix strings are listed after the longer
  1286. strings that include them -- e.g., %got after %got_hiadj, etc. */
  1287. struct nios2_special_relocS nios2_special_reloc[] = {
  1288. {"%hiadj", BFD_RELOC_NIOS2_HIADJ16},
  1289. {"%hi", BFD_RELOC_NIOS2_HI16},
  1290. {"%lo", BFD_RELOC_NIOS2_LO16},
  1291. {"%gprel", BFD_RELOC_NIOS2_GPREL},
  1292. {"%call_lo", BFD_RELOC_NIOS2_CALL_LO},
  1293. {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA},
  1294. {"%call", BFD_RELOC_NIOS2_CALL16},
  1295. {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO},
  1296. {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA},
  1297. {"%gotoff", BFD_RELOC_NIOS2_GOTOFF},
  1298. {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA},
  1299. {"%got_lo", BFD_RELOC_NIOS2_GOT_LO},
  1300. {"%got", BFD_RELOC_NIOS2_GOT16},
  1301. {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16},
  1302. {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16},
  1303. {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16},
  1304. {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16},
  1305. {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16},
  1306. };
  1307. #define NIOS2_NUM_SPECIAL_RELOCS \
  1308. (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
  1309. const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS;
  1310. /* Creates a new nios2_insn_relocS and returns a pointer to it. */
  1311. static nios2_insn_relocS *
  1312. nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
  1313. {
  1314. nios2_insn_relocS *retval;
  1315. retval = (nios2_insn_relocS *) malloc (sizeof (nios2_insn_relocS));
  1316. if (retval == NULL)
  1317. {
  1318. as_bad (_("can't create relocation"));
  1319. abort ();
  1320. }
  1321. /* Fill out the fields with default values. */
  1322. retval->reloc_next = NULL;
  1323. retval->reloc_type = reloc_type;
  1324. retval->reloc_pcrel = pcrel;
  1325. return retval;
  1326. }
  1327. /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
  1328. /* FIXME: this is never called; memory leak? */
  1329. #if 0
  1330. static void
  1331. nios2_insn_reloc_destroy (nios2_insn_relocS *reloc)
  1332. {
  1333. gas_assert (reloc != NULL);
  1334. free (reloc);
  1335. }
  1336. #endif
  1337. /* Look up a register name and validate it for the given regtype.
  1338. Return the register mapping or NULL on failure. */
  1339. static struct nios2_reg *
  1340. nios2_parse_reg (const char *token, unsigned long regtype)
  1341. {
  1342. struct nios2_reg *reg = nios2_reg_lookup (token);
  1343. if (reg == NULL)
  1344. {
  1345. as_bad (_("unknown register %s"), token);
  1346. return NULL;
  1347. }
  1348. /* Matched a register, but is it the wrong type? */
  1349. if (!(regtype & reg->regtype))
  1350. {
  1351. if (regtype & REG_CONTROL)
  1352. as_bad (_("expecting control register"));
  1353. else if (reg->regtype & REG_CONTROL)
  1354. as_bad (_("illegal use of control register"));
  1355. else if (reg->regtype & REG_COPROCESSOR)
  1356. as_bad (_("illegal use of coprocessor register"));
  1357. else
  1358. as_bad (_("invalid register %s"), token);
  1359. return NULL;
  1360. }
  1361. /* Warn for explicit use of special registers. */
  1362. if (reg->regtype & REG_NORMAL)
  1363. {
  1364. if (!nios2_as_options.noat && reg->index == 1)
  1365. as_warn (_("Register at (r1) can sometimes be corrupted by "
  1366. "assembler optimizations.\n"
  1367. "Use .set noat to turn off those optimizations "
  1368. "(and this warning)."));
  1369. if (!nios2_as_options.nobreak && reg->index == 25)
  1370. as_warn (_("The debugger will corrupt bt (r25).\n"
  1371. "If you don't need to debug this "
  1372. "code use .set nobreak to turn off this warning."));
  1373. if (!nios2_as_options.nobreak && reg->index == 30)
  1374. as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
  1375. "If you don't need to debug this "
  1376. "code use .set nobreak to turn off this warning."));
  1377. }
  1378. return reg;
  1379. }
  1380. /* This function parses a reglist for ldwm/stwm and push.n/pop.n
  1381. instructions, given as a brace-enclosed register list. The tokenizer
  1382. has replaced commas in the token with spaces.
  1383. The return value is a bitmask of registers in the set. It also
  1384. sets nios2_reglist_mask and nios2_reglist_dir to allow error checking
  1385. when parsing the base register. */
  1386. static unsigned long nios2_reglist_mask;
  1387. static int nios2_reglist_dir;
  1388. static unsigned long
  1389. nios2_parse_reglist (char *token, const struct nios2_opcode *op)
  1390. {
  1391. unsigned long mask = 0;
  1392. int dir = 0;
  1393. unsigned long regtype = 0;
  1394. int last = -1;
  1395. const char *regname;
  1396. nios2_reglist_mask = 0;
  1397. nios2_reglist_dir = 0;
  1398. if (op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM)
  1399. {
  1400. regtype = REG_LDWM;
  1401. dir = 0;
  1402. }
  1403. else if (op->match == MATCH_R2_PUSH_N)
  1404. {
  1405. regtype = REG_POP;
  1406. dir = -1;
  1407. }
  1408. else if (op->match == MATCH_R2_POP_N)
  1409. {
  1410. regtype = REG_POP;
  1411. dir = 1;
  1412. }
  1413. else
  1414. bad_opcode (op);
  1415. for (regname = strtok (token, "{ }");
  1416. regname;
  1417. regname = strtok (NULL, "{ }"))
  1418. {
  1419. int regno;
  1420. struct nios2_reg *reg = nios2_parse_reg (regname, regtype);
  1421. if (!reg)
  1422. break;
  1423. regno = reg->index;
  1424. /* Make sure registers are listed in proper sequence. */
  1425. if (last >= 0)
  1426. {
  1427. if (regno == last)
  1428. {
  1429. as_bad ("duplicate register %s\n", reg->name);
  1430. return 0;
  1431. }
  1432. else if (dir == 0)
  1433. dir = (regno < last ? -1 : 1);
  1434. else if ((dir > 0 && regno < last)
  1435. || (dir < 0 && regno > last)
  1436. || (op->match == MATCH_R2_PUSH_N
  1437. && ! ((last == 31 && regno == 28)
  1438. || (last == 31 && regno <= 23)
  1439. || (last == 28 && regno <= 23)
  1440. || (regno < 23 && regno == last - 1)))
  1441. || (op->match == MATCH_R2_POP_N
  1442. && ! ((regno == 31 && last == 28)
  1443. || (regno == 31 && last <= 23)
  1444. || (regno == 28 && last <= 23)
  1445. || (last < 23 && last == regno - 1))))
  1446. {
  1447. as_bad ("invalid register order");
  1448. return 0;
  1449. }
  1450. }
  1451. mask |= 1 << regno;
  1452. last = regno;
  1453. }
  1454. /* Check that all ldwm/stwm regs belong to the same set. */
  1455. if ((op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM)
  1456. && (mask & 0x00003ffc) && (mask & 0x90ffc000))
  1457. {
  1458. as_bad ("invalid register set in reglist");
  1459. return 0;
  1460. }
  1461. /* Check that push.n/pop.n regs include RA. */
  1462. if ((op->match == MATCH_R2_PUSH_N || op->match == MATCH_R2_POP_N)
  1463. && ((mask & 0x80000000) == 0))
  1464. {
  1465. as_bad ("reglist must include ra (r31)");
  1466. return 0;
  1467. }
  1468. /* Check that there is at least one register in the set. */
  1469. if (!mask)
  1470. {
  1471. as_bad ("reglist must include at least one register");
  1472. return 0;
  1473. }
  1474. /* OK, reglist passed validation. */
  1475. nios2_reglist_mask = mask;
  1476. nios2_reglist_dir = dir;
  1477. return mask;
  1478. }
  1479. /* This function parses the base register and options used by the ldwm/stwm
  1480. instructions. Returns the base register and sets the option arguments
  1481. accordingly. On failure, returns NULL. */
  1482. static struct nios2_reg *
  1483. nios2_parse_base_register (char *str, int *direction, int *writeback, int *ret)
  1484. {
  1485. char *regname;
  1486. struct nios2_reg *reg;
  1487. *direction = 0;
  1488. *writeback = 0;
  1489. *ret = 0;
  1490. /* Check for --. */
  1491. if (strncmp (str, "--", 2) == 0)
  1492. {
  1493. str += 2;
  1494. *direction -= 1;
  1495. }
  1496. /* Extract the base register. */
  1497. if (*str != '(')
  1498. {
  1499. as_bad ("expected '(' before base register");
  1500. return NULL;
  1501. }
  1502. str++;
  1503. regname = str;
  1504. str = strchr (str, ')');
  1505. if (!str)
  1506. {
  1507. as_bad ("expected ')' after base register");
  1508. return NULL;
  1509. }
  1510. *str = '\0';
  1511. str++;
  1512. reg = nios2_parse_reg (regname, REG_NORMAL);
  1513. if (reg == NULL)
  1514. return NULL;
  1515. /* Check for ++. */
  1516. if (strncmp (str, "++", 2) == 0)
  1517. {
  1518. str += 2;
  1519. *direction += 1;
  1520. }
  1521. /* Ensure that either -- or ++ is specified, but not both. */
  1522. if (*direction == 0)
  1523. {
  1524. as_bad ("invalid base register syntax");
  1525. return NULL;;
  1526. }
  1527. /* Check for options. The tokenizer has replaced commas with spaces. */
  1528. while (*str)
  1529. {
  1530. while (*str == ' ')
  1531. str++;
  1532. if (strncmp (str, "writeback", 9) == 0)
  1533. {
  1534. *writeback = 1;
  1535. str += 9;
  1536. }
  1537. else if (strncmp (str, "ret", 3) == 0)
  1538. {
  1539. *ret = 1;
  1540. str += 3;
  1541. }
  1542. else if (*str)
  1543. {
  1544. as_bad ("invalid option syntax");
  1545. return NULL;
  1546. }
  1547. }
  1548. return reg;
  1549. }
  1550. /* The various nios2_assemble_* functions call this
  1551. function to generate an expression from a string representing an expression.
  1552. It then tries to evaluate the expression, and if it can, returns its value.
  1553. If not, it creates a new nios2_insn_relocS and stores the expression and
  1554. reloc_type for future use. */
  1555. static unsigned long
  1556. nios2_assemble_expression (const char *exprstr,
  1557. nios2_insn_infoS *insn,
  1558. bfd_reloc_code_real_type orig_reloc_type,
  1559. unsigned int pcrel)
  1560. {
  1561. nios2_insn_relocS *reloc;
  1562. char *saved_line_ptr;
  1563. unsigned long value = 0;
  1564. int i;
  1565. bfd_reloc_code_real_type reloc_type = orig_reloc_type;
  1566. gas_assert (exprstr != NULL);
  1567. gas_assert (insn != NULL);
  1568. /* Check for relocation operators.
  1569. Change the relocation type and advance the ptr to the start of
  1570. the expression proper. */
  1571. for (i = 0; i < nios2_num_special_relocs; i++)
  1572. if (strstr (exprstr, nios2_special_reloc[i].string) != NULL)
  1573. {
  1574. reloc_type = nios2_special_reloc[i].reloc_type;
  1575. exprstr += strlen (nios2_special_reloc[i].string) + 1;
  1576. /* %lo and %hiadj have different meanings for PC-relative
  1577. expressions. */
  1578. if (pcrel)
  1579. {
  1580. if (reloc_type == BFD_RELOC_NIOS2_LO16)
  1581. reloc_type = BFD_RELOC_NIOS2_PCREL_LO;
  1582. if (reloc_type == BFD_RELOC_NIOS2_HIADJ16)
  1583. reloc_type = BFD_RELOC_NIOS2_PCREL_HA;
  1584. }
  1585. break;
  1586. }
  1587. /* No relocation allowed; we must have a constant expression. */
  1588. if (orig_reloc_type == BFD_RELOC_NONE)
  1589. {
  1590. expressionS exp;
  1591. /* Parse the expression string. */
  1592. saved_line_ptr = input_line_pointer;
  1593. input_line_pointer = (char *) exprstr;
  1594. expression (&exp);
  1595. input_line_pointer = saved_line_ptr;
  1596. /* If we don't have a constant, give an error. */
  1597. if (reloc_type != orig_reloc_type || exp.X_op != O_constant)
  1598. as_bad (_("expression must be constant"));
  1599. else
  1600. value = exp.X_add_number;
  1601. return (unsigned long) value;
  1602. }
  1603. /* We potentially have a relocation. */
  1604. reloc = nios2_insn_reloc_new (reloc_type, pcrel);
  1605. reloc->reloc_next = insn->insn_reloc;
  1606. insn->insn_reloc = reloc;
  1607. /* Parse the expression string. */
  1608. saved_line_ptr = input_line_pointer;
  1609. input_line_pointer = (char *) exprstr;
  1610. expression (&reloc->reloc_expression);
  1611. input_line_pointer = saved_line_ptr;
  1612. /* This is redundant as the fixup will put this into
  1613. the instruction, but it is included here so that
  1614. self-test mode (-r) works. */
  1615. if (nios2_mode == NIOS2_MODE_TEST
  1616. && reloc->reloc_expression.X_op == O_constant)
  1617. value = reloc->reloc_expression.X_add_number;
  1618. return (unsigned long) value;
  1619. }
  1620. /* Encode a 3-bit register number, giving an error if this is not possible. */
  1621. static unsigned int
  1622. nios2_assemble_reg3 (const char *token)
  1623. {
  1624. struct nios2_reg *reg = nios2_parse_reg (token, REG_3BIT);
  1625. int j;
  1626. if (reg == NULL)
  1627. return 0;
  1628. for (j = 0; j < nios2_num_r2_reg3_mappings; j++)
  1629. if (nios2_r2_reg3_mappings[j] == reg->index)
  1630. return j;
  1631. /* Should never get here if we passed validation. */
  1632. as_bad (_("invalid register %s"), token);
  1633. return 0;
  1634. }
  1635. /* Argument assemble functions. */
  1636. /* Control register index. */
  1637. static void
  1638. nios2_assemble_arg_c (const char *token, nios2_insn_infoS *insn)
  1639. {
  1640. struct nios2_reg *reg = nios2_parse_reg (token, REG_CONTROL);
  1641. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1642. if (reg == NULL)
  1643. return;
  1644. switch (op->format)
  1645. {
  1646. case iw_r_type:
  1647. insn->insn_code |= SET_IW_R_IMM5 (reg->index);
  1648. break;
  1649. case iw_F3X6L5_type:
  1650. insn->insn_code |= SET_IW_F3X6L5_IMM5 (reg->index);
  1651. break;
  1652. default:
  1653. bad_opcode (op);
  1654. }
  1655. }
  1656. /* Destination register. */
  1657. static void
  1658. nios2_assemble_arg_d (const char *token, nios2_insn_infoS *insn)
  1659. {
  1660. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1661. unsigned long regtype = REG_NORMAL;
  1662. struct nios2_reg *reg;
  1663. if (op->format == iw_custom_type || op->format == iw_F3X8_type)
  1664. regtype |= REG_COPROCESSOR;
  1665. reg = nios2_parse_reg (token, regtype);
  1666. if (reg == NULL)
  1667. return;
  1668. switch (op->format)
  1669. {
  1670. case iw_r_type:
  1671. insn->insn_code |= SET_IW_R_C (reg->index);
  1672. break;
  1673. case iw_custom_type:
  1674. insn->insn_code |= SET_IW_CUSTOM_C (reg->index);
  1675. if (reg->regtype & REG_COPROCESSOR)
  1676. insn->insn_code |= SET_IW_CUSTOM_READC (0);
  1677. else
  1678. insn->insn_code |= SET_IW_CUSTOM_READC (1);
  1679. break;
  1680. case iw_F3X6L5_type:
  1681. case iw_F3X6_type:
  1682. insn->insn_code |= SET_IW_F3X6L5_C (reg->index);
  1683. break;
  1684. case iw_F3X8_type:
  1685. insn->insn_code |= SET_IW_F3X8_C (reg->index);
  1686. if (reg->regtype & REG_COPROCESSOR)
  1687. insn->insn_code |= SET_IW_F3X8_READC (0);
  1688. else
  1689. insn->insn_code |= SET_IW_F3X8_READC (1);
  1690. break;
  1691. case iw_F2_type:
  1692. insn->insn_code |= SET_IW_F2_B (reg->index);
  1693. break;
  1694. default:
  1695. bad_opcode (op);
  1696. }
  1697. }
  1698. /* Source register 1. */
  1699. static void
  1700. nios2_assemble_arg_s (const char *token, nios2_insn_infoS *insn)
  1701. {
  1702. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1703. unsigned long regtype = REG_NORMAL;
  1704. struct nios2_reg *reg;
  1705. if (op->format == iw_custom_type || op->format == iw_F3X8_type)
  1706. regtype |= REG_COPROCESSOR;
  1707. reg = nios2_parse_reg (token, regtype);
  1708. if (reg == NULL)
  1709. return;
  1710. switch (op->format)
  1711. {
  1712. case iw_r_type:
  1713. if (op->match == MATCH_R1_JMP && reg->index == 31)
  1714. as_bad (_("r31 cannot be used with jmp; use ret instead"));
  1715. insn->insn_code |= SET_IW_R_A (reg->index);
  1716. break;
  1717. case iw_i_type:
  1718. insn->insn_code |= SET_IW_I_A (reg->index);
  1719. break;
  1720. case iw_custom_type:
  1721. insn->insn_code |= SET_IW_CUSTOM_A (reg->index);
  1722. if (reg->regtype & REG_COPROCESSOR)
  1723. insn->insn_code |= SET_IW_CUSTOM_READA (0);
  1724. else
  1725. insn->insn_code |= SET_IW_CUSTOM_READA (1);
  1726. break;
  1727. case iw_F2I16_type:
  1728. insn->insn_code |= SET_IW_F2I16_A (reg->index);
  1729. break;
  1730. case iw_F2X4I12_type:
  1731. insn->insn_code |= SET_IW_F2X4I12_A (reg->index);
  1732. break;
  1733. case iw_F1X4I12_type:
  1734. insn->insn_code |= SET_IW_F1X4I12_A (reg->index);
  1735. break;
  1736. case iw_F1X4L17_type:
  1737. insn->insn_code |= SET_IW_F1X4L17_A (reg->index);
  1738. break;
  1739. case iw_F3X6L5_type:
  1740. case iw_F3X6_type:
  1741. if (op->match == MATCH_R2_JMP && reg->index == 31)
  1742. as_bad (_("r31 cannot be used with jmp; use ret instead"));
  1743. insn->insn_code |= SET_IW_F3X6L5_A (reg->index);
  1744. break;
  1745. case iw_F2X6L10_type:
  1746. insn->insn_code |= SET_IW_F2X6L10_A (reg->index);
  1747. break;
  1748. case iw_F3X8_type:
  1749. insn->insn_code |= SET_IW_F3X8_A (reg->index);
  1750. if (reg->regtype & REG_COPROCESSOR)
  1751. insn->insn_code |= SET_IW_F3X8_READA (0);
  1752. else
  1753. insn->insn_code |= SET_IW_F3X8_READA (1);
  1754. break;
  1755. case iw_F1X1_type:
  1756. if (op->match == MATCH_R2_JMPR_N && reg->index == 31)
  1757. as_bad (_("r31 cannot be used with jmpr.n; use ret.n instead"));
  1758. insn->insn_code |= SET_IW_F1X1_A (reg->index);
  1759. break;
  1760. case iw_F1I5_type:
  1761. /* Implicit stack pointer reference. */
  1762. if (reg->index != 27)
  1763. as_bad (_("invalid register %s"), token);
  1764. break;
  1765. case iw_F2_type:
  1766. insn->insn_code |= SET_IW_F2_A (reg->index);
  1767. break;
  1768. default:
  1769. bad_opcode (op);
  1770. }
  1771. }
  1772. /* Source register 2. */
  1773. static void
  1774. nios2_assemble_arg_t (const char *token, nios2_insn_infoS *insn)
  1775. {
  1776. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1777. unsigned long regtype = REG_NORMAL;
  1778. struct nios2_reg *reg;
  1779. if (op->format == iw_custom_type || op->format == iw_F3X8_type)
  1780. regtype |= REG_COPROCESSOR;
  1781. reg = nios2_parse_reg (token, regtype);
  1782. if (reg == NULL)
  1783. return;
  1784. switch (op->format)
  1785. {
  1786. case iw_r_type:
  1787. insn->insn_code |= SET_IW_R_B (reg->index);
  1788. break;
  1789. case iw_i_type:
  1790. insn->insn_code |= SET_IW_I_B (reg->index);
  1791. break;
  1792. case iw_custom_type:
  1793. insn->insn_code |= SET_IW_CUSTOM_B (reg->index);
  1794. if (reg->regtype & REG_COPROCESSOR)
  1795. insn->insn_code |= SET_IW_CUSTOM_READB (0);
  1796. else
  1797. insn->insn_code |= SET_IW_CUSTOM_READB (1);
  1798. break;
  1799. case iw_F2I16_type:
  1800. insn->insn_code |= SET_IW_F2I16_B (reg->index);
  1801. break;
  1802. case iw_F2X4I12_type:
  1803. insn->insn_code |= SET_IW_F2X4I12_B (reg->index);
  1804. break;
  1805. case iw_F3X6L5_type:
  1806. case iw_F3X6_type:
  1807. insn->insn_code |= SET_IW_F3X6L5_B (reg->index);
  1808. break;
  1809. case iw_F2X6L10_type:
  1810. insn->insn_code |= SET_IW_F2X6L10_B (reg->index);
  1811. break;
  1812. case iw_F3X8_type:
  1813. insn->insn_code |= SET_IW_F3X8_B (reg->index);
  1814. if (reg->regtype & REG_COPROCESSOR)
  1815. insn->insn_code |= SET_IW_F3X8_READB (0);
  1816. else
  1817. insn->insn_code |= SET_IW_F3X8_READB (1);
  1818. break;
  1819. case iw_F1I5_type:
  1820. insn->insn_code |= SET_IW_F1I5_B (reg->index);
  1821. break;
  1822. case iw_F2_type:
  1823. insn->insn_code |= SET_IW_F2_B (reg->index);
  1824. break;
  1825. case iw_T1X1I6_type:
  1826. /* Implicit zero register reference. */
  1827. if (reg->index != 0)
  1828. as_bad (_("invalid register %s"), token);
  1829. break;
  1830. default:
  1831. bad_opcode (op);
  1832. }
  1833. }
  1834. /* Destination register w/3-bit encoding. */
  1835. static void
  1836. nios2_assemble_arg_D (const char *token, nios2_insn_infoS *insn)
  1837. {
  1838. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1839. int reg = nios2_assemble_reg3 (token);
  1840. switch (op->format)
  1841. {
  1842. case iw_T1I7_type:
  1843. insn->insn_code |= SET_IW_T1I7_A3 (reg);
  1844. break;
  1845. case iw_T2X1L3_type:
  1846. insn->insn_code |= SET_IW_T2X1L3_B3 (reg);
  1847. break;
  1848. case iw_T2X1I3_type:
  1849. insn->insn_code |= SET_IW_T2X1I3_B3 (reg);
  1850. break;
  1851. case iw_T3X1_type:
  1852. insn->insn_code |= SET_IW_T3X1_C3 (reg);
  1853. break;
  1854. case iw_T2X3_type:
  1855. /* Some instructions using this encoding take 3 register arguments,
  1856. requiring the destination register to be the same as the first
  1857. source register. */
  1858. if (op->num_args == 3)
  1859. insn->insn_code |= SET_IW_T2X3_A3 (reg);
  1860. else
  1861. insn->insn_code |= SET_IW_T2X3_B3 (reg);
  1862. break;
  1863. default:
  1864. bad_opcode (op);
  1865. }
  1866. }
  1867. /* Source register w/3-bit encoding. */
  1868. static void
  1869. nios2_assemble_arg_S (const char *token, nios2_insn_infoS *insn)
  1870. {
  1871. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1872. int reg = nios2_assemble_reg3 (token);
  1873. switch (op->format)
  1874. {
  1875. case iw_T1I7_type:
  1876. insn->insn_code |= SET_IW_T1I7_A3 (reg);
  1877. break;
  1878. case iw_T2I4_type:
  1879. insn->insn_code |= SET_IW_T2I4_A3 (reg);
  1880. break;
  1881. case iw_T2X1L3_type:
  1882. insn->insn_code |= SET_IW_T2X1L3_A3 (reg);
  1883. break;
  1884. case iw_T2X1I3_type:
  1885. insn->insn_code |= SET_IW_T2X1I3_A3 (reg);
  1886. break;
  1887. case iw_T3X1_type:
  1888. insn->insn_code |= SET_IW_T3X1_A3 (reg);
  1889. break;
  1890. case iw_T2X3_type:
  1891. /* Some instructions using this encoding take 3 register arguments,
  1892. requiring the destination register to be the same as the first
  1893. source register. */
  1894. if (op->num_args == 3)
  1895. {
  1896. int dreg = GET_IW_T2X3_A3 (insn->insn_code);
  1897. if (dreg != reg)
  1898. as_bad ("source and destination registers must be the same");
  1899. }
  1900. else
  1901. insn->insn_code |= SET_IW_T2X3_A3 (reg);
  1902. break;
  1903. case iw_T1X1I6_type:
  1904. insn->insn_code |= SET_IW_T1X1I6_A3 (reg);
  1905. break;
  1906. default:
  1907. bad_opcode (op);
  1908. }
  1909. }
  1910. /* Source register 2 w/3-bit encoding. */
  1911. static void
  1912. nios2_assemble_arg_T (const char *token, nios2_insn_infoS *insn)
  1913. {
  1914. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1915. int reg = nios2_assemble_reg3 (token);
  1916. switch (op->format)
  1917. {
  1918. case iw_T2I4_type:
  1919. insn->insn_code |= SET_IW_T2I4_B3 (reg);
  1920. break;
  1921. case iw_T3X1_type:
  1922. insn->insn_code |= SET_IW_T3X1_B3 (reg);
  1923. break;
  1924. case iw_T2X3_type:
  1925. insn->insn_code |= SET_IW_T2X3_B3 (reg);
  1926. break;
  1927. default:
  1928. bad_opcode (op);
  1929. }
  1930. }
  1931. /* 16-bit signed immediate. */
  1932. static void
  1933. nios2_assemble_arg_i (const char *token, nios2_insn_infoS *insn)
  1934. {
  1935. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1936. unsigned int val;
  1937. switch (op->format)
  1938. {
  1939. case iw_i_type:
  1940. val = nios2_assemble_expression (token, insn,
  1941. BFD_RELOC_NIOS2_S16, 0);
  1942. insn->constant_bits |= SET_IW_I_IMM16 (val);
  1943. break;
  1944. case iw_F2I16_type:
  1945. val = nios2_assemble_expression (token, insn,
  1946. BFD_RELOC_NIOS2_S16, 0);
  1947. insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
  1948. break;
  1949. default:
  1950. bad_opcode (op);
  1951. }
  1952. }
  1953. /* 12-bit signed immediate. */
  1954. static void
  1955. nios2_assemble_arg_I (const char *token, nios2_insn_infoS *insn)
  1956. {
  1957. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1958. unsigned int val;
  1959. switch (op->format)
  1960. {
  1961. case iw_F2X4I12_type:
  1962. val = nios2_assemble_expression (token, insn,
  1963. BFD_RELOC_NIOS2_R2_S12, 0);
  1964. insn->constant_bits |= SET_IW_F2X4I12_IMM12 (val);
  1965. break;
  1966. case iw_F1X4I12_type:
  1967. val = nios2_assemble_expression (token, insn,
  1968. BFD_RELOC_NIOS2_R2_S12, 0);
  1969. insn->constant_bits |= SET_IW_F2X4I12_IMM12 (val);
  1970. break;
  1971. default:
  1972. bad_opcode (op);
  1973. }
  1974. }
  1975. /* 16-bit unsigned immediate. */
  1976. static void
  1977. nios2_assemble_arg_u (const char *token, nios2_insn_infoS *insn)
  1978. {
  1979. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  1980. unsigned int val;
  1981. switch (op->format)
  1982. {
  1983. case iw_i_type:
  1984. val = nios2_assemble_expression (token, insn,
  1985. BFD_RELOC_NIOS2_U16, 0);
  1986. insn->constant_bits |= SET_IW_I_IMM16 (val);
  1987. break;
  1988. case iw_F2I16_type:
  1989. val = nios2_assemble_expression (token, insn,
  1990. BFD_RELOC_NIOS2_U16, 0);
  1991. insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
  1992. break;
  1993. default:
  1994. bad_opcode (op);
  1995. }
  1996. }
  1997. /* 7-bit unsigned immediate with 2-bit shift. */
  1998. static void
  1999. nios2_assemble_arg_U (const char *token, nios2_insn_infoS *insn)
  2000. {
  2001. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2002. unsigned int val;
  2003. switch (op->format)
  2004. {
  2005. case iw_T1I7_type:
  2006. val = nios2_assemble_expression (token, insn,
  2007. BFD_RELOC_NIOS2_R2_T1I7_2, 0);
  2008. insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 2);
  2009. break;
  2010. case iw_X1I7_type:
  2011. val = nios2_assemble_expression (token, insn,
  2012. BFD_RELOC_NIOS2_R2_X1I7_2, 0);
  2013. insn->constant_bits |= SET_IW_X1I7_IMM7 (val >> 2);
  2014. break;
  2015. default:
  2016. bad_opcode (op);
  2017. }
  2018. }
  2019. /* 5-bit unsigned immediate with 2-bit shift. */
  2020. static void
  2021. nios2_assemble_arg_V (const char *token, nios2_insn_infoS *insn)
  2022. {
  2023. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2024. unsigned int val;
  2025. switch (op->format)
  2026. {
  2027. case iw_F1I5_type:
  2028. val = nios2_assemble_expression (token, insn,
  2029. BFD_RELOC_NIOS2_R2_F1I5_2, 0);
  2030. insn->constant_bits |= SET_IW_F1I5_IMM5 (val >> 2);
  2031. break;
  2032. default:
  2033. bad_opcode (op);
  2034. }
  2035. }
  2036. /* 4-bit unsigned immediate with 2-bit shift. */
  2037. static void
  2038. nios2_assemble_arg_W (const char *token, nios2_insn_infoS *insn)
  2039. {
  2040. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2041. unsigned int val;
  2042. switch (op->format)
  2043. {
  2044. case iw_T2I4_type:
  2045. val = nios2_assemble_expression (token, insn,
  2046. BFD_RELOC_NIOS2_R2_T2I4_2, 0);
  2047. insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 2);
  2048. break;
  2049. case iw_L5I4X1_type:
  2050. /* This argument is optional for push.n/pop.n, and defaults to
  2051. zero if unspecified. */
  2052. if (token == NULL)
  2053. return;
  2054. val = nios2_assemble_expression (token, insn,
  2055. BFD_RELOC_NIOS2_R2_L5I4X1, 0);
  2056. insn->constant_bits |= SET_IW_L5I4X1_IMM4 (val >> 2);
  2057. break;
  2058. default:
  2059. bad_opcode (op);
  2060. }
  2061. }
  2062. /* 4-bit unsigned immediate with 1-bit shift. */
  2063. static void
  2064. nios2_assemble_arg_X (const char *token, nios2_insn_infoS *insn)
  2065. {
  2066. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2067. unsigned int val;
  2068. switch (op->format)
  2069. {
  2070. case iw_T2I4_type:
  2071. val = nios2_assemble_expression (token, insn,
  2072. BFD_RELOC_NIOS2_R2_T2I4_1, 0);
  2073. insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 1);
  2074. break;
  2075. default:
  2076. bad_opcode (op);
  2077. }
  2078. }
  2079. /* 4-bit unsigned immediate without shift. */
  2080. static void
  2081. nios2_assemble_arg_Y (const char *token, nios2_insn_infoS *insn)
  2082. {
  2083. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2084. unsigned int val;
  2085. switch (op->format)
  2086. {
  2087. case iw_T2I4_type:
  2088. val = nios2_assemble_expression (token, insn,
  2089. BFD_RELOC_NIOS2_R2_T2I4, 0);
  2090. insn->constant_bits |= SET_IW_T2I4_IMM4 (val);
  2091. break;
  2092. default:
  2093. bad_opcode (op);
  2094. }
  2095. }
  2096. /* 16-bit signed immediate address offset. */
  2097. static void
  2098. nios2_assemble_arg_o (const char *token, nios2_insn_infoS *insn)
  2099. {
  2100. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2101. unsigned int val;
  2102. switch (op->format)
  2103. {
  2104. case iw_i_type:
  2105. val = nios2_assemble_expression (token, insn,
  2106. BFD_RELOC_16_PCREL, 1);
  2107. insn->constant_bits |= SET_IW_I_IMM16 (val);
  2108. break;
  2109. case iw_F2I16_type:
  2110. val = nios2_assemble_expression (token, insn,
  2111. BFD_RELOC_16_PCREL, 1);
  2112. insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
  2113. break;
  2114. default:
  2115. bad_opcode (op);
  2116. }
  2117. }
  2118. /* 10-bit signed address offset with 1-bit shift. */
  2119. static void
  2120. nios2_assemble_arg_O (const char *token, nios2_insn_infoS *insn)
  2121. {
  2122. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2123. unsigned int val;
  2124. switch (op->format)
  2125. {
  2126. case iw_I10_type:
  2127. val = nios2_assemble_expression (token, insn,
  2128. BFD_RELOC_NIOS2_R2_I10_1_PCREL, 1);
  2129. insn->constant_bits |= SET_IW_I10_IMM10 (val >> 1);
  2130. break;
  2131. default:
  2132. bad_opcode (op);
  2133. }
  2134. }
  2135. /* 7-bit signed address offset with 1-bit shift. */
  2136. static void
  2137. nios2_assemble_arg_P (const char *token, nios2_insn_infoS *insn)
  2138. {
  2139. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2140. unsigned int val;
  2141. switch (op->format)
  2142. {
  2143. case iw_T1I7_type:
  2144. val = nios2_assemble_expression (token, insn,
  2145. BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, 1);
  2146. insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 1);
  2147. break;
  2148. default:
  2149. bad_opcode (op);
  2150. }
  2151. }
  2152. /* 5-bit unsigned immediate. */
  2153. static void
  2154. nios2_assemble_arg_j (const char *token, nios2_insn_infoS *insn)
  2155. {
  2156. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2157. unsigned int val;
  2158. switch (op->format)
  2159. {
  2160. case iw_r_type:
  2161. val = nios2_assemble_expression (token, insn,
  2162. BFD_RELOC_NIOS2_IMM5, 0);
  2163. insn->constant_bits |= SET_IW_R_IMM5 (val);
  2164. break;
  2165. case iw_F3X6L5_type:
  2166. if (op->match == MATCH_R2_ENI)
  2167. /* Value must be constant 0 or 1. */
  2168. {
  2169. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2170. if (val != 0 && val != 1)
  2171. as_bad ("invalid eni argument %u", val);
  2172. insn->insn_code |= SET_IW_F3X6L5_IMM5 (val);
  2173. }
  2174. else
  2175. {
  2176. val = nios2_assemble_expression (token, insn,
  2177. BFD_RELOC_NIOS2_IMM5, 0);
  2178. insn->constant_bits |= SET_IW_F3X6L5_IMM5 (val);
  2179. }
  2180. break;
  2181. case iw_F2X6L10_type:
  2182. /* Only constant expression without relocation permitted for
  2183. bit position. */
  2184. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2185. if (val > 31)
  2186. as_bad ("invalid bit position %u", val);
  2187. insn->insn_code |= SET_IW_F2X6L10_MSB (val);
  2188. break;
  2189. case iw_X2L5_type:
  2190. val = nios2_assemble_expression (token, insn,
  2191. BFD_RELOC_NIOS2_R2_X2L5, 0);
  2192. insn->constant_bits |= SET_IW_X2L5_IMM5 (val);
  2193. break;
  2194. default:
  2195. bad_opcode (op);
  2196. }
  2197. }
  2198. /* Second 5-bit unsigned immediate field. */
  2199. static void
  2200. nios2_assemble_arg_k (const char *token, nios2_insn_infoS *insn)
  2201. {
  2202. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2203. unsigned int val;
  2204. switch (op->format)
  2205. {
  2206. case iw_F2X6L10_type:
  2207. /* Only constant expression without relocation permitted for
  2208. bit position. */
  2209. val = nios2_assemble_expression (token, insn,
  2210. BFD_RELOC_NONE, 0);
  2211. if (val > 31)
  2212. as_bad ("invalid bit position %u", val);
  2213. else if (GET_IW_F2X6L10_MSB (insn->insn_code) < val)
  2214. as_bad ("MSB must be greater than or equal to LSB");
  2215. insn->insn_code |= SET_IW_F2X6L10_LSB (val);
  2216. break;
  2217. default:
  2218. bad_opcode (op);
  2219. }
  2220. }
  2221. /* 8-bit unsigned immediate. */
  2222. static void
  2223. nios2_assemble_arg_l (const char *token, nios2_insn_infoS *insn)
  2224. {
  2225. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2226. unsigned int val;
  2227. switch (op->format)
  2228. {
  2229. case iw_custom_type:
  2230. val = nios2_assemble_expression (token, insn,
  2231. BFD_RELOC_NIOS2_IMM8, 0);
  2232. insn->constant_bits |= SET_IW_CUSTOM_N (val);
  2233. break;
  2234. case iw_F3X8_type:
  2235. val = nios2_assemble_expression (token, insn,
  2236. BFD_RELOC_NIOS2_IMM8, 0);
  2237. insn->constant_bits |= SET_IW_F3X8_N (val);
  2238. break;
  2239. default:
  2240. bad_opcode (op);
  2241. }
  2242. }
  2243. /* 26-bit unsigned immediate. */
  2244. static void
  2245. nios2_assemble_arg_m (const char *token, nios2_insn_infoS *insn)
  2246. {
  2247. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2248. unsigned int val;
  2249. switch (op->format)
  2250. {
  2251. case iw_j_type:
  2252. val = nios2_assemble_expression (token, insn,
  2253. (nios2_as_options.noat
  2254. ? BFD_RELOC_NIOS2_CALL26_NOAT
  2255. : BFD_RELOC_NIOS2_CALL26),
  2256. 0);
  2257. insn->constant_bits |= SET_IW_J_IMM26 (val);
  2258. break;
  2259. case iw_L26_type:
  2260. val = nios2_assemble_expression (token, insn,
  2261. (nios2_as_options.noat
  2262. ? BFD_RELOC_NIOS2_CALL26_NOAT
  2263. : BFD_RELOC_NIOS2_CALL26),
  2264. 0);
  2265. insn->constant_bits |= SET_IW_L26_IMM26 (val);
  2266. break;
  2267. default:
  2268. bad_opcode (op);
  2269. }
  2270. }
  2271. /* 6-bit unsigned immediate with no shifting. */
  2272. static void
  2273. nios2_assemble_arg_M (const char *token, nios2_insn_infoS *insn)
  2274. {
  2275. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2276. unsigned int val;
  2277. switch (op->format)
  2278. {
  2279. case iw_T1X1I6_type:
  2280. val = nios2_assemble_expression (token, insn,
  2281. BFD_RELOC_NIOS2_R2_T1X1I6, 0);
  2282. insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val);
  2283. break;
  2284. default:
  2285. bad_opcode (op);
  2286. }
  2287. }
  2288. /* 6-bit unsigned immediate with 2-bit shift. */
  2289. static void
  2290. nios2_assemble_arg_N (const char *token, nios2_insn_infoS *insn)
  2291. {
  2292. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2293. unsigned int val;
  2294. switch (op->format)
  2295. {
  2296. case iw_T1X1I6_type:
  2297. val = nios2_assemble_expression (token, insn,
  2298. BFD_RELOC_NIOS2_R2_T1X1I6_2, 0);
  2299. insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val >> 2);
  2300. break;
  2301. default:
  2302. bad_opcode (op);
  2303. }
  2304. }
  2305. /* Encoded enumeration for addi.n/subi.n. */
  2306. static void
  2307. nios2_assemble_arg_e (const char *token, nios2_insn_infoS *insn)
  2308. {
  2309. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2310. unsigned int val;
  2311. int i;
  2312. switch (op->format)
  2313. {
  2314. case iw_T2X1I3_type:
  2315. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2316. for (i = 0; i < nios2_num_r2_asi_n_mappings; i++)
  2317. if (val == nios2_r2_asi_n_mappings[i])
  2318. break;
  2319. if (i == nios2_num_r2_asi_n_mappings)
  2320. {
  2321. as_bad (_("Invalid constant operand %s"), token);
  2322. return;
  2323. }
  2324. insn->insn_code |= SET_IW_T2X1I3_IMM3 ((unsigned)i);
  2325. break;
  2326. default:
  2327. bad_opcode (op);
  2328. }
  2329. }
  2330. /* Encoded enumeration for slli.n/srli.n. */
  2331. static void
  2332. nios2_assemble_arg_f (const char *token, nios2_insn_infoS *insn)
  2333. {
  2334. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2335. unsigned int val;
  2336. int i;
  2337. switch (op->format)
  2338. {
  2339. case iw_T2X1L3_type:
  2340. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2341. for (i = 0; i < nios2_num_r2_shi_n_mappings; i++)
  2342. if (val == nios2_r2_shi_n_mappings[i])
  2343. break;
  2344. if (i == nios2_num_r2_shi_n_mappings)
  2345. {
  2346. as_bad (_("Invalid constant operand %s"), token);
  2347. return;
  2348. }
  2349. insn->insn_code |= SET_IW_T2X1L3_SHAMT ((unsigned)i);
  2350. break;
  2351. default:
  2352. bad_opcode (op);
  2353. }
  2354. }
  2355. /* Encoded enumeration for andi.n. */
  2356. static void
  2357. nios2_assemble_arg_g (const char *token, nios2_insn_infoS *insn)
  2358. {
  2359. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2360. unsigned int val;
  2361. int i;
  2362. switch (op->format)
  2363. {
  2364. case iw_T2I4_type:
  2365. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2366. for (i = 0; i < nios2_num_r2_andi_n_mappings; i++)
  2367. if (val == nios2_r2_andi_n_mappings[i])
  2368. break;
  2369. if (i == nios2_num_r2_andi_n_mappings)
  2370. {
  2371. as_bad (_("Invalid constant operand %s"), token);
  2372. return;
  2373. }
  2374. insn->insn_code |= SET_IW_T2I4_IMM4 ((unsigned)i);
  2375. break;
  2376. default:
  2377. bad_opcode (op);
  2378. }
  2379. }
  2380. /* Encoded enumeration for movi.n. */
  2381. static void
  2382. nios2_assemble_arg_h (const char *token, nios2_insn_infoS *insn)
  2383. {
  2384. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2385. unsigned int val;
  2386. int i;
  2387. switch (op->format)
  2388. {
  2389. case iw_T1I7_type:
  2390. val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
  2391. i = (signed) val;
  2392. if ((signed) i == -1)
  2393. val = 127;
  2394. else if (i == -2)
  2395. val = 126;
  2396. else if (i == 0xff)
  2397. val = 125;
  2398. else if (i < 0 || i > 125)
  2399. {
  2400. as_bad (_("Invalid constant operand %s"), token);
  2401. return;
  2402. }
  2403. insn->insn_code |= SET_IW_T1I7_IMM7 (val);
  2404. break;
  2405. default:
  2406. bad_opcode (op);
  2407. }
  2408. }
  2409. /* Encoded REGMASK for ldwm/stwm or push.n/pop.n. */
  2410. static void
  2411. nios2_assemble_arg_R (const char *token, nios2_insn_infoS *insn)
  2412. {
  2413. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2414. unsigned long mask;
  2415. char *buf = strdup (token);
  2416. unsigned long reglist = nios2_parse_reglist (buf, op);
  2417. free (buf);
  2418. if (reglist == 0)
  2419. return;
  2420. switch (op->format)
  2421. {
  2422. case iw_F1X4L17_type:
  2423. /* Encoding for ldwm/stwm. */
  2424. if (reglist & 0x00003ffc)
  2425. mask = reglist >> 2;
  2426. else
  2427. {
  2428. insn->insn_code |= SET_IW_F1X4L17_RS (1);
  2429. mask = (reglist & 0x00ffc000) >> 14;
  2430. if (reglist & (1 << 28))
  2431. mask |= 1 << 10;
  2432. if (reglist & (1 << 31))
  2433. mask |= 1 << 11;
  2434. }
  2435. insn->insn_code |= SET_IW_F1X4L17_REGMASK (mask);
  2436. break;
  2437. case iw_L5I4X1_type:
  2438. /* Encoding for push.n/pop.n. */
  2439. if (reglist & (1 << 28))
  2440. insn->insn_code |= SET_IW_L5I4X1_FP (1);
  2441. mask = reglist & 0x00ff0000;
  2442. if (mask)
  2443. {
  2444. int i;
  2445. for (i = 0; i < nios2_num_r2_reg_range_mappings; i++)
  2446. if (nios2_r2_reg_range_mappings[i] == mask)
  2447. break;
  2448. if (i == nios2_num_r2_reg_range_mappings)
  2449. {
  2450. as_bad ("invalid reglist");
  2451. return;
  2452. }
  2453. insn->insn_code |= SET_IW_L5I4X1_REGRANGE (i);
  2454. insn->insn_code |= SET_IW_L5I4X1_CS (1);
  2455. }
  2456. break;
  2457. default:
  2458. bad_opcode (op);
  2459. }
  2460. }
  2461. /* Base register for ldwm/stwm. */
  2462. static void
  2463. nios2_assemble_arg_B (const char *token, nios2_insn_infoS *insn)
  2464. {
  2465. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2466. int direction, writeback, ret;
  2467. char *str = strdup (token);
  2468. struct nios2_reg *reg
  2469. = nios2_parse_base_register (str, &direction, &writeback, &ret);
  2470. free (str);
  2471. if (!reg)
  2472. return;
  2473. switch (op->format)
  2474. {
  2475. case iw_F1X4L17_type:
  2476. /* For ldwm, check to see if the base register is already inside the
  2477. register list. */
  2478. if (op->match == MATCH_R2_LDWM
  2479. && (nios2_reglist_mask & (1 << reg->index)))
  2480. {
  2481. as_bad ("invalid base register; %s is inside the reglist", reg->name);
  2482. return;
  2483. }
  2484. /* For stwm, ret option is not allowed. */
  2485. if (op->match == MATCH_R2_STWM && ret)
  2486. {
  2487. as_bad ("invalid option syntax");
  2488. return;
  2489. }
  2490. /* Check that the direction matches the ordering of the reglist. */
  2491. if (nios2_reglist_dir && direction != nios2_reglist_dir)
  2492. {
  2493. as_bad ("reglist order does not match increment/decrement mode");
  2494. return;
  2495. }
  2496. insn->insn_code |= SET_IW_F1X4L17_A (reg->index);
  2497. if (direction > 0)
  2498. insn->insn_code |= SET_IW_F1X4L17_ID (1);
  2499. if (writeback)
  2500. insn->insn_code |= SET_IW_F1X4L17_WB (1);
  2501. if (ret)
  2502. insn->insn_code |= SET_IW_F1X4L17_PC (1);
  2503. break;
  2504. default:
  2505. bad_opcode (op);
  2506. }
  2507. }
  2508. static void
  2509. nios2_assemble_args (nios2_insn_infoS *insn)
  2510. {
  2511. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  2512. const char *argptr;
  2513. unsigned int tokidx, ntok;
  2514. /* Make sure there are enough arguments. */
  2515. ntok = (op->pinfo & NIOS2_INSN_OPTARG) ? op->num_args - 1 : op->num_args;
  2516. for (tokidx = 1; tokidx <= ntok; tokidx++)
  2517. if (insn->insn_tokens[tokidx] == NULL)
  2518. {
  2519. as_bad ("missing argument");
  2520. return;
  2521. }
  2522. for (argptr = op->args, tokidx = 1;
  2523. *argptr && insn->insn_tokens[tokidx];
  2524. argptr++)
  2525. switch (*argptr)
  2526. {
  2527. case ',':
  2528. case '(':
  2529. case ')':
  2530. break;
  2531. case 'c':
  2532. nios2_assemble_arg_c (insn->insn_tokens[tokidx++], insn);
  2533. break;
  2534. case 'd':
  2535. nios2_assemble_arg_d (insn->insn_tokens[tokidx++], insn);
  2536. break;
  2537. case 's':
  2538. nios2_assemble_arg_s (insn->insn_tokens[tokidx++], insn);
  2539. break;
  2540. case 't':
  2541. nios2_assemble_arg_t (insn->insn_tokens[tokidx++], insn);
  2542. break;
  2543. case 'D':
  2544. nios2_assemble_arg_D (insn->insn_tokens[tokidx++], insn);
  2545. break;
  2546. case 'S':
  2547. nios2_assemble_arg_S (insn->insn_tokens[tokidx++], insn);
  2548. break;
  2549. case 'T':
  2550. nios2_assemble_arg_T (insn->insn_tokens[tokidx++], insn);
  2551. break;
  2552. case 'i':
  2553. nios2_assemble_arg_i (insn->insn_tokens[tokidx++], insn);
  2554. break;
  2555. case 'I':
  2556. nios2_assemble_arg_I (insn->insn_tokens[tokidx++], insn);
  2557. break;
  2558. case 'u':
  2559. nios2_assemble_arg_u (insn->insn_tokens[tokidx++], insn);
  2560. break;
  2561. case 'U':
  2562. nios2_assemble_arg_U (insn->insn_tokens[tokidx++], insn);
  2563. break;
  2564. case 'V':
  2565. nios2_assemble_arg_V (insn->insn_tokens[tokidx++], insn);
  2566. break;
  2567. case 'W':
  2568. nios2_assemble_arg_W (insn->insn_tokens[tokidx++], insn);
  2569. break;
  2570. case 'X':
  2571. nios2_assemble_arg_X (insn->insn_tokens[tokidx++], insn);
  2572. break;
  2573. case 'Y':
  2574. nios2_assemble_arg_Y (insn->insn_tokens[tokidx++], insn);
  2575. break;
  2576. case 'o':
  2577. nios2_assemble_arg_o (insn->insn_tokens[tokidx++], insn);
  2578. break;
  2579. case 'O':
  2580. nios2_assemble_arg_O (insn->insn_tokens[tokidx++], insn);
  2581. break;
  2582. case 'P':
  2583. nios2_assemble_arg_P (insn->insn_tokens[tokidx++], insn);
  2584. break;
  2585. case 'j':
  2586. nios2_assemble_arg_j (insn->insn_tokens[tokidx++], insn);
  2587. break;
  2588. case 'k':
  2589. nios2_assemble_arg_k (insn->insn_tokens[tokidx++], insn);
  2590. break;
  2591. case 'l':
  2592. nios2_assemble_arg_l (insn->insn_tokens[tokidx++], insn);
  2593. break;
  2594. case 'm':
  2595. nios2_assemble_arg_m (insn->insn_tokens[tokidx++], insn);
  2596. break;
  2597. case 'M':
  2598. nios2_assemble_arg_M (insn->insn_tokens[tokidx++], insn);
  2599. break;
  2600. case 'N':
  2601. nios2_assemble_arg_N (insn->insn_tokens[tokidx++], insn);
  2602. break;
  2603. case 'e':
  2604. nios2_assemble_arg_e (insn->insn_tokens[tokidx++], insn);
  2605. break;
  2606. case 'f':
  2607. nios2_assemble_arg_f (insn->insn_tokens[tokidx++], insn);
  2608. break;
  2609. case 'g':
  2610. nios2_assemble_arg_g (insn->insn_tokens[tokidx++], insn);
  2611. break;
  2612. case 'h':
  2613. nios2_assemble_arg_h (insn->insn_tokens[tokidx++], insn);
  2614. break;
  2615. case 'R':
  2616. nios2_assemble_arg_R (insn->insn_tokens[tokidx++], insn);
  2617. break;
  2618. case 'B':
  2619. nios2_assemble_arg_B (insn->insn_tokens[tokidx++], insn);
  2620. break;
  2621. default:
  2622. bad_opcode (op);
  2623. break;
  2624. }
  2625. /* Perform argument checking. */
  2626. nios2_check_assembly (insn->insn_code | insn->constant_bits,
  2627. insn->insn_tokens[tokidx]);
  2628. }
  2629. /* The function consume_arg takes a pointer into a string
  2630. of instruction tokens (args) and a pointer into a string
  2631. representing the expected sequence of tokens and separators.
  2632. It checks whether the first argument in argstr is of the
  2633. expected type, throwing an error if it is not, and returns
  2634. the pointer argstr. */
  2635. static char *
  2636. nios2_consume_arg (char *argstr, const char *parsestr)
  2637. {
  2638. char *temp;
  2639. switch (*parsestr)
  2640. {
  2641. case 'c':
  2642. case 'd':
  2643. case 's':
  2644. case 't':
  2645. case 'D':
  2646. case 'S':
  2647. case 'T':
  2648. break;
  2649. case 'i':
  2650. case 'u':
  2651. if (*argstr == '%')
  2652. {
  2653. if (nios2_special_relocation_p (argstr))
  2654. {
  2655. /* We zap the parentheses because we don't want them confused
  2656. with separators. */
  2657. temp = strchr (argstr, '(');
  2658. if (temp != NULL)
  2659. *temp = ' ';
  2660. temp = strchr (argstr, ')');
  2661. if (temp != NULL)
  2662. *temp = ' ';
  2663. }
  2664. else
  2665. as_bad (_("badly formed expression near %s"), argstr);
  2666. }
  2667. break;
  2668. case 'm':
  2669. case 'j':
  2670. case 'k':
  2671. case 'l':
  2672. case 'I':
  2673. case 'U':
  2674. case 'V':
  2675. case 'W':
  2676. case 'X':
  2677. case 'Y':
  2678. case 'O':
  2679. case 'P':
  2680. case 'e':
  2681. case 'f':
  2682. case 'g':
  2683. case 'h':
  2684. case 'M':
  2685. case 'N':
  2686. /* We can't have %hi, %lo or %hiadj here. */
  2687. if (*argstr == '%')
  2688. as_bad (_("badly formed expression near %s"), argstr);
  2689. break;
  2690. case 'R':
  2691. /* Register list for ldwm/stwm or push.n/pop.n. Replace the commas
  2692. in the list with spaces so we don't confuse them with separators. */
  2693. if (*argstr != '{')
  2694. {
  2695. as_bad ("missing '{' in register list");
  2696. break;
  2697. }
  2698. for (temp = argstr + 1; *temp; temp++)
  2699. {
  2700. if (*temp == '}')
  2701. break;
  2702. else if (*temp == ',')
  2703. *temp = ' ';
  2704. }
  2705. if (!*temp)
  2706. {
  2707. as_bad ("missing '}' in register list");
  2708. break;
  2709. }
  2710. break;
  2711. case 'B':
  2712. /* Base register and options for ldwm/stwm. This is the final argument
  2713. and consumes the rest of the argument string; replace commas
  2714. with spaces so that the token splitter doesn't think they are
  2715. separate arguments. */
  2716. for (temp = argstr; *temp; temp++)
  2717. if (*temp == ',')
  2718. *temp = ' ';
  2719. break;
  2720. case 'o':
  2721. case 'E':
  2722. break;
  2723. default:
  2724. BAD_CASE (*parsestr);
  2725. break;
  2726. }
  2727. return argstr;
  2728. }
  2729. /* The function consume_separator takes a pointer into a string
  2730. of instruction tokens (args) and a pointer into a string representing
  2731. the expected sequence of tokens and separators. It finds the first
  2732. instance of the character pointed to by separator in argstr, and
  2733. returns a pointer to the next element of argstr, which is the
  2734. following token in the sequence. */
  2735. static char *
  2736. nios2_consume_separator (char *argstr, const char *separator)
  2737. {
  2738. char *p;
  2739. /* If we have a opcode reg, expr(reg) type instruction, and
  2740. * we are separating the expr from the (reg), we find the last
  2741. * (, just in case the expression has parentheses. */
  2742. if (*separator == '(')
  2743. p = strrchr (argstr, *separator);
  2744. else
  2745. p = strchr (argstr, *separator);
  2746. if (p != NULL)
  2747. *p++ = 0;
  2748. return p;
  2749. }
  2750. /* The principal argument parsing function which takes a string argstr
  2751. representing the instruction arguments for insn, and extracts the argument
  2752. tokens matching parsestr into parsed_args. */
  2753. static void
  2754. nios2_parse_args (nios2_insn_infoS *insn, char *argstr,
  2755. const char *parsestr, char **parsed_args)
  2756. {
  2757. char *p;
  2758. char *end = NULL;
  2759. int i;
  2760. p = argstr;
  2761. i = 0;
  2762. bfd_boolean terminate = FALSE;
  2763. /* This rest of this function is it too fragile and it mostly works,
  2764. therefore special case this one. */
  2765. if (*parsestr == 0 && argstr != 0)
  2766. {
  2767. as_bad (_("too many arguments"));
  2768. parsed_args[0] = NULL;
  2769. return;
  2770. }
  2771. while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS)
  2772. {
  2773. parsed_args[i] = nios2_consume_arg (p, parsestr);
  2774. ++parsestr;
  2775. while (*parsestr == '(' || *parsestr == ')' || *parsestr == ',')
  2776. {
  2777. char *context = p;
  2778. p = nios2_consume_separator (p, parsestr);
  2779. /* Check for missing separators. */
  2780. if (!p && !(insn->insn_nios2_opcode->pinfo & NIOS2_INSN_OPTARG))
  2781. {
  2782. as_bad (_("expecting %c near %s"), *parsestr, context);
  2783. break;
  2784. }
  2785. ++parsestr;
  2786. }
  2787. if (*parsestr == '\0')
  2788. {
  2789. /* Check that the argument string has no trailing arguments. */
  2790. end = strpbrk (p, ",");
  2791. if (end != NULL)
  2792. as_bad (_("too many arguments"));
  2793. }
  2794. if (*parsestr == '\0' || (p != NULL && *p == '\0'))
  2795. terminate = TRUE;
  2796. ++i;
  2797. }
  2798. parsed_args[i] = NULL;
  2799. }
  2800. /** Support for pseudo-op parsing. These are macro-like opcodes that
  2801. expand into real insns by suitable fiddling with the operands. */
  2802. /* Append the string modifier to the string contained in the argument at
  2803. parsed_args[ndx]. */
  2804. static void
  2805. nios2_modify_arg (char **parsed_args, const char *modifier,
  2806. int unused ATTRIBUTE_UNUSED, int ndx)
  2807. {
  2808. char *tmp = parsed_args[ndx];
  2809. parsed_args[ndx]
  2810. = (char *) malloc (strlen (parsed_args[ndx]) + strlen (modifier) + 1);
  2811. strcpy (parsed_args[ndx], tmp);
  2812. strcat (parsed_args[ndx], modifier);
  2813. }
  2814. /* Modify parsed_args[ndx] by negating that argument. */
  2815. static void
  2816. nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED,
  2817. int unused ATTRIBUTE_UNUSED, int ndx)
  2818. {
  2819. char *tmp = parsed_args[ndx];
  2820. parsed_args[ndx]
  2821. = (char *) malloc (strlen ("~(") + strlen (parsed_args[ndx]) +
  2822. strlen (")+1") + 1);
  2823. strcpy (parsed_args[ndx], "~(");
  2824. strcat (parsed_args[ndx], tmp);
  2825. strcat (parsed_args[ndx], ")+1");
  2826. }
  2827. /* The function nios2_swap_args swaps the pointers at indices index_1 and
  2828. index_2 in the array parsed_args[] - this is used for operand swapping
  2829. for comparison operations. */
  2830. static void
  2831. nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED,
  2832. int index_1, int index_2)
  2833. {
  2834. char *tmp;
  2835. gas_assert (index_1 < NIOS2_MAX_INSN_TOKENS
  2836. && index_2 < NIOS2_MAX_INSN_TOKENS);
  2837. tmp = parsed_args[index_1];
  2838. parsed_args[index_1] = parsed_args[index_2];
  2839. parsed_args[index_2] = tmp;
  2840. }
  2841. /* This function appends the string appnd to the array of strings in
  2842. parsed_args num times starting at index start in the array. */
  2843. static void
  2844. nios2_append_arg (char **parsed_args, const char *appnd, int num,
  2845. int start)
  2846. {
  2847. int i, count;
  2848. char *tmp;
  2849. gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
  2850. if (nios2_mode == NIOS2_MODE_TEST)
  2851. tmp = parsed_args[start];
  2852. else
  2853. tmp = NULL;
  2854. for (i = start, count = num; count > 0; ++i, --count)
  2855. parsed_args[i] = (char *) appnd;
  2856. gas_assert (i == (start + num));
  2857. parsed_args[i] = tmp;
  2858. parsed_args[i + 1] = NULL;
  2859. }
  2860. /* This function inserts the string insert num times in the array
  2861. parsed_args, starting at the index start. */
  2862. static void
  2863. nios2_insert_arg (char **parsed_args, const char *insert, int num,
  2864. int start)
  2865. {
  2866. int i, count;
  2867. gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
  2868. /* Move the existing arguments up to create space. */
  2869. for (i = NIOS2_MAX_INSN_TOKENS; i - num >= start; --i)
  2870. parsed_args[i] = parsed_args[i - num];
  2871. for (i = start, count = num; count > 0; ++i, --count)
  2872. parsed_args[i] = (char *) insert;
  2873. }
  2874. /* Cleanup function to free malloc'ed arg strings. */
  2875. static void
  2876. nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start)
  2877. {
  2878. if (parsed_args[start])
  2879. {
  2880. free (parsed_args[start]);
  2881. parsed_args[start] = NULL;
  2882. }
  2883. }
  2884. /* This function swaps the pseudo-op for a real op. */
  2885. static nios2_ps_insn_infoS*
  2886. nios2_translate_pseudo_insn (nios2_insn_infoS *insn)
  2887. {
  2888. nios2_ps_insn_infoS *ps_insn;
  2889. /* Find which real insn the pseudo-op transates to and
  2890. switch the insn_info ptr to point to it. */
  2891. ps_insn = nios2_ps_lookup (insn->insn_nios2_opcode->name);
  2892. if (ps_insn != NULL)
  2893. {
  2894. insn->insn_nios2_opcode = nios2_opcode_lookup (ps_insn->insn);
  2895. insn->insn_tokens[0] = insn->insn_nios2_opcode->name;
  2896. /* Modify the args so they work with the real insn. */
  2897. ps_insn->arg_modifer_func ((char **) insn->insn_tokens,
  2898. ps_insn->arg_modifier, ps_insn->num,
  2899. ps_insn->index);
  2900. }
  2901. else
  2902. /* we cannot recover from this. */
  2903. as_fatal (_("unrecognized pseudo-instruction %s"),
  2904. insn->insn_nios2_opcode->name);
  2905. return ps_insn;
  2906. }
  2907. /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
  2908. static void
  2909. nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn,
  2910. nios2_ps_insn_infoS *ps_insn)
  2911. {
  2912. if (ps_insn->arg_cleanup_func)
  2913. (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens,
  2914. ps_insn->num, ps_insn->index);
  2915. }
  2916. const nios2_ps_insn_infoS nios2_ps_insn_info_structs[] = {
  2917. /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
  2918. {"mov", "add", nios2_append_arg, "zero", 1, 3, NULL},
  2919. {"movi", "addi", nios2_insert_arg, "zero", 1, 2, NULL},
  2920. {"movhi", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
  2921. {"movui", "ori", nios2_insert_arg, "zero", 1, 2, NULL},
  2922. {"movia", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
  2923. {"nop", "add", nios2_append_arg, "zero", 3, 1, NULL},
  2924. {"bgt", "blt", nios2_swap_args, "", 1, 2, NULL},
  2925. {"bgtu", "bltu", nios2_swap_args, "", 1, 2, NULL},
  2926. {"ble", "bge", nios2_swap_args, "", 1, 2, NULL},
  2927. {"bleu", "bgeu", nios2_swap_args, "", 1, 2, NULL},
  2928. {"cmpgt", "cmplt", nios2_swap_args, "", 2, 3, NULL},
  2929. {"cmpgtu", "cmpltu", nios2_swap_args, "", 2, 3, NULL},
  2930. {"cmple", "cmpge", nios2_swap_args, "", 2, 3, NULL},
  2931. {"cmpleu", "cmpgeu", nios2_swap_args, "", 2, 3, NULL},
  2932. {"cmpgti", "cmpgei", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
  2933. {"cmpgtui", "cmpgeui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
  2934. {"cmplei", "cmplti", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
  2935. {"cmpleui", "cmpltui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
  2936. {"subi", "addi", nios2_negate_arg, "", 0, 3, nios2_free_arg},
  2937. {"nop.n", "mov.n", nios2_append_arg, "zero", 2, 1, NULL}
  2938. /* Add further pseudo-ops here. */
  2939. };
  2940. #define NIOS2_NUM_PSEUDO_INSNS \
  2941. ((sizeof(nios2_ps_insn_info_structs)/ \
  2942. sizeof(nios2_ps_insn_info_structs[0])))
  2943. const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS;
  2944. /** Assembler output support. */
  2945. /* Output a normal instruction. */
  2946. static void
  2947. output_insn (nios2_insn_infoS *insn)
  2948. {
  2949. char *f;
  2950. nios2_insn_relocS *reloc;
  2951. f = frag_more (insn->insn_nios2_opcode->size);
  2952. /* This allocates enough space for the instruction
  2953. and puts it in the current frag. */
  2954. md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
  2955. /* Emit debug info. */
  2956. dwarf2_emit_insn (insn->insn_nios2_opcode->size);
  2957. /* Create any fixups to be acted on later. */
  2958. for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
  2959. fix_new_exp (frag_now, f - frag_now->fr_literal,
  2960. insn->insn_nios2_opcode->size,
  2961. &reloc->reloc_expression, reloc->reloc_pcrel,
  2962. reloc->reloc_type);
  2963. }
  2964. /* Output an unconditional branch. */
  2965. static void
  2966. output_ubranch (nios2_insn_infoS *insn)
  2967. {
  2968. nios2_insn_relocS *reloc = insn->insn_reloc;
  2969. /* If the reloc is NULL, there was an error assembling the branch. */
  2970. if (reloc != NULL)
  2971. {
  2972. symbolS *symp = reloc->reloc_expression.X_add_symbol;
  2973. offsetT offset = reloc->reloc_expression.X_add_number;
  2974. char *f;
  2975. bfd_boolean is_cdx = (insn->insn_nios2_opcode->size == 2);
  2976. /* Tag dwarf2 debug info to the address at the start of the insn.
  2977. We must do it before frag_var() below closes off the frag. */
  2978. dwarf2_emit_insn (0);
  2979. /* We create a machine dependent frag which can grow
  2980. to accommodate the largest possible instruction sequence
  2981. this may generate. */
  2982. f = frag_var (rs_machine_dependent,
  2983. UBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
  2984. (is_cdx ? CDX_UBRANCH_SUBTYPE (0) : UBRANCH_SUBTYPE (0)),
  2985. symp, offset, NULL);
  2986. md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
  2987. /* We leave fixup generation to md_convert_frag. */
  2988. }
  2989. }
  2990. /* Output a conditional branch. */
  2991. static void
  2992. output_cbranch (nios2_insn_infoS *insn)
  2993. {
  2994. nios2_insn_relocS *reloc = insn->insn_reloc;
  2995. /* If the reloc is NULL, there was an error assembling the branch. */
  2996. if (reloc != NULL)
  2997. {
  2998. symbolS *symp = reloc->reloc_expression.X_add_symbol;
  2999. offsetT offset = reloc->reloc_expression.X_add_number;
  3000. char *f;
  3001. bfd_boolean is_cdx = (insn->insn_nios2_opcode->size == 2);
  3002. /* Tag dwarf2 debug info to the address at the start of the insn.
  3003. We must do it before frag_var() below closes off the frag. */
  3004. dwarf2_emit_insn (0);
  3005. /* We create a machine dependent frag which can grow
  3006. to accommodate the largest possible instruction sequence
  3007. this may generate. */
  3008. f = frag_var (rs_machine_dependent,
  3009. CBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
  3010. (is_cdx ? CDX_CBRANCH_SUBTYPE (0) : CBRANCH_SUBTYPE (0)),
  3011. symp, offset, NULL);
  3012. md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
  3013. /* We leave fixup generation to md_convert_frag. */
  3014. }
  3015. }
  3016. /* Output a call sequence. Since calls are not pc-relative for NIOS2,
  3017. but are page-relative, we cannot tell at any stage in assembly
  3018. whether a call will be out of range since a section may be linked
  3019. at any address. So if we are relaxing, we convert all call instructions
  3020. to long call sequences, and rely on the linker to relax them back to
  3021. short calls. */
  3022. static void
  3023. output_call (nios2_insn_infoS *insn)
  3024. {
  3025. /* This allocates enough space for the instruction
  3026. and puts it in the current frag. */
  3027. char *f = frag_more (12);
  3028. nios2_insn_relocS *reloc = insn->insn_reloc;
  3029. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  3030. switch (op->format)
  3031. {
  3032. case iw_j_type:
  3033. md_number_to_chars (f,
  3034. (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
  3035. | SET_IW_I_A (0)),
  3036. 4);
  3037. dwarf2_emit_insn (4);
  3038. fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
  3039. &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
  3040. md_number_to_chars (f + 4,
  3041. (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
  3042. | SET_IW_I_A (AT_REGNUM)),
  3043. 4);
  3044. dwarf2_emit_insn (4);
  3045. fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
  3046. &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
  3047. md_number_to_chars (f + 8, MATCH_R1_CALLR | SET_IW_R_A (AT_REGNUM), 4);
  3048. dwarf2_emit_insn (4);
  3049. break;
  3050. case iw_L26_type:
  3051. md_number_to_chars (f,
  3052. (MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM)
  3053. | SET_IW_F2I16_A (0)),
  3054. 4);
  3055. dwarf2_emit_insn (4);
  3056. fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
  3057. &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
  3058. md_number_to_chars (f + 4,
  3059. (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM)
  3060. | SET_IW_F2I16_A (AT_REGNUM)),
  3061. 4);
  3062. dwarf2_emit_insn (4);
  3063. fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
  3064. &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
  3065. md_number_to_chars (f + 8, MATCH_R2_CALLR | SET_IW_F3X6L5_A (AT_REGNUM),
  3066. 4);
  3067. dwarf2_emit_insn (4);
  3068. break;
  3069. default:
  3070. bad_opcode (op);
  3071. }
  3072. }
  3073. /* Output a movhi/addi pair for the movia pseudo-op. */
  3074. static void
  3075. output_movia (nios2_insn_infoS *insn)
  3076. {
  3077. /* This allocates enough space for the instruction
  3078. and puts it in the current frag. */
  3079. char *f = frag_more (8);
  3080. nios2_insn_relocS *reloc = insn->insn_reloc;
  3081. unsigned long reg, code;
  3082. const struct nios2_opcode *op = insn->insn_nios2_opcode;
  3083. /* If the reloc is NULL, there was an error assembling the movia. */
  3084. if (reloc != NULL)
  3085. {
  3086. switch (op->format)
  3087. {
  3088. case iw_i_type:
  3089. reg = GET_IW_I_B (insn->insn_code);
  3090. code = MATCH_R1_ADDI | SET_IW_I_A (reg) | SET_IW_I_B (reg);
  3091. break;
  3092. case iw_F2I16_type:
  3093. reg = GET_IW_F2I16_B (insn->insn_code);
  3094. code = MATCH_R2_ADDI | SET_IW_F2I16_A (reg) | SET_IW_F2I16_B (reg);
  3095. break;
  3096. default:
  3097. bad_opcode (op);
  3098. }
  3099. md_number_to_chars (f, insn->insn_code, 4);
  3100. dwarf2_emit_insn (4);
  3101. fix_new (frag_now, f - frag_now->fr_literal, 4,
  3102. reloc->reloc_expression.X_add_symbol,
  3103. reloc->reloc_expression.X_add_number, 0,
  3104. BFD_RELOC_NIOS2_HIADJ16);
  3105. md_number_to_chars (f + 4, code, 4);
  3106. dwarf2_emit_insn (4);
  3107. fix_new (frag_now, f + 4 - frag_now->fr_literal, 4,
  3108. reloc->reloc_expression.X_add_symbol,
  3109. reloc->reloc_expression.X_add_number, 0, BFD_RELOC_NIOS2_LO16);
  3110. }
  3111. }
  3112. /** External interfaces. */
  3113. /* Update the selected architecture based on ARCH, giving an error if
  3114. ARCH is an invalid value. */
  3115. static void
  3116. nios2_use_arch (const char *arch)
  3117. {
  3118. if (strcmp (arch, "nios2") == 0 || strcmp (arch, "r1") == 0)
  3119. {
  3120. nios2_architecture |= EF_NIOS2_ARCH_R1;
  3121. nios2_opcodes = (struct nios2_opcode *) nios2_r1_opcodes;
  3122. nios2_num_opcodes = nios2_num_r1_opcodes;
  3123. nop32 = nop_r1;
  3124. nop16 = NULL;
  3125. return;
  3126. }
  3127. else if (strcmp (arch, "r2") == 0)
  3128. {
  3129. nios2_architecture |= EF_NIOS2_ARCH_R2;
  3130. nios2_opcodes = (struct nios2_opcode *) nios2_r2_opcodes;
  3131. nios2_num_opcodes = nios2_num_r2_opcodes;
  3132. nop32 = nop_r2;
  3133. nop16 = nop_r2_cdx;
  3134. return;
  3135. }
  3136. as_bad (_("unknown architecture '%s'"), arch);
  3137. }
  3138. /* The following functions are called by machine-independent parts of
  3139. the assembler. */
  3140. int
  3141. md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
  3142. {
  3143. switch (c)
  3144. {
  3145. case 'r':
  3146. /* Hidden option for self-test mode. */
  3147. nios2_mode = NIOS2_MODE_TEST;
  3148. break;
  3149. case OPTION_RELAX_ALL:
  3150. nios2_as_options.relax = relax_all;
  3151. break;
  3152. case OPTION_NORELAX:
  3153. nios2_as_options.relax = relax_none;
  3154. break;
  3155. case OPTION_RELAX_SECTION:
  3156. nios2_as_options.relax = relax_section;
  3157. break;
  3158. case OPTION_EB:
  3159. target_big_endian = 1;
  3160. break;
  3161. case OPTION_EL:
  3162. target_big_endian = 0;
  3163. break;
  3164. case OPTION_MARCH:
  3165. nios2_use_arch (arg);
  3166. break;
  3167. default:
  3168. return 0;
  3169. break;
  3170. }
  3171. return 1;
  3172. }
  3173. /* Implement TARGET_FORMAT. We can choose to be big-endian or
  3174. little-endian at runtime based on a switch. */
  3175. const char *
  3176. nios2_target_format (void)
  3177. {
  3178. return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2";
  3179. }
  3180. /* Machine-dependent usage message. */
  3181. void
  3182. md_show_usage (FILE *stream)
  3183. {
  3184. fprintf (stream, " NIOS2 options:\n"
  3185. " -relax-all replace all branch and call "
  3186. "instructions with jmp and callr sequences\n"
  3187. " -relax-section replace identified out of range "
  3188. "branches with jmp sequences (default)\n"
  3189. " -no-relax do not replace any branches or calls\n"
  3190. " -EB force big-endian byte ordering\n"
  3191. " -EL force little-endian byte ordering\n"
  3192. " -march=ARCH enable instructions from architecture ARCH\n");
  3193. }
  3194. /* This function is called once, at assembler startup time.
  3195. It should set up all the tables, etc. that the MD part of the
  3196. assembler will need. */
  3197. void
  3198. md_begin (void)
  3199. {
  3200. int i;
  3201. const char *inserted;
  3202. switch (nios2_architecture)
  3203. {
  3204. default:
  3205. case EF_NIOS2_ARCH_R1:
  3206. bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r1);
  3207. break;
  3208. case EF_NIOS2_ARCH_R2:
  3209. if (target_big_endian)
  3210. as_fatal (_("Big-endian R2 is not supported."));
  3211. bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r2);
  3212. break;
  3213. }
  3214. /* Create and fill a hashtable for the Nios II opcodes, registers and
  3215. arguments. */
  3216. nios2_opcode_hash = hash_new ();
  3217. nios2_reg_hash = hash_new ();
  3218. nios2_ps_hash = hash_new ();
  3219. for (i = 0; i < nios2_num_opcodes; ++i)
  3220. {
  3221. inserted
  3222. = hash_insert (nios2_opcode_hash, nios2_opcodes[i].name,
  3223. (PTR) & nios2_opcodes[i]);
  3224. if (inserted != NULL)
  3225. {
  3226. fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
  3227. nios2_opcodes[i].name, inserted);
  3228. /* Probably a memory allocation problem? Give up now. */
  3229. as_fatal (_("Broken assembler. No assembly attempted."));
  3230. }
  3231. }
  3232. for (i = 0; i < nios2_num_regs; ++i)
  3233. {
  3234. inserted
  3235. = hash_insert (nios2_reg_hash, nios2_regs[i].name,
  3236. (PTR) & nios2_regs[i]);
  3237. if (inserted != NULL)
  3238. {
  3239. fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
  3240. nios2_regs[i].name, inserted);
  3241. /* Probably a memory allocation problem? Give up now. */
  3242. as_fatal (_("Broken assembler. No assembly attempted."));
  3243. }
  3244. }
  3245. for (i = 0; i < nios2_num_ps_insn_info_structs; ++i)
  3246. {
  3247. inserted
  3248. = hash_insert (nios2_ps_hash, nios2_ps_insn_info_structs[i].pseudo_insn,
  3249. (PTR) & nios2_ps_insn_info_structs[i]);
  3250. if (inserted != NULL)
  3251. {
  3252. fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
  3253. nios2_ps_insn_info_structs[i].pseudo_insn, inserted);
  3254. /* Probably a memory allocation problem? Give up now. */
  3255. as_fatal (_("Broken assembler. No assembly attempted."));
  3256. }
  3257. }
  3258. /* Assembler option defaults. */
  3259. nios2_as_options.noat = FALSE;
  3260. nios2_as_options.nobreak = FALSE;
  3261. /* Debug information is incompatible with relaxation. */
  3262. if (debug_type != DEBUG_UNSPECIFIED)
  3263. nios2_as_options.relax = relax_none;
  3264. /* Initialize the alignment data. */
  3265. nios2_current_align_seg = now_seg;
  3266. nios2_last_label = NULL;
  3267. nios2_current_align = 0;
  3268. nios2_min_align = 2;
  3269. }
  3270. /* Assembles a single line of Nios II assembly language. */
  3271. void
  3272. md_assemble (char *op_str)
  3273. {
  3274. char *argstr;
  3275. char *op_strdup = NULL;
  3276. unsigned long saved_pinfo = 0;
  3277. nios2_insn_infoS thisinsn;
  3278. nios2_insn_infoS *insn = &thisinsn;
  3279. /* Make sure we are aligned on an appropriate boundary. */
  3280. if (nios2_current_align < nios2_min_align)
  3281. nios2_align (nios2_min_align, NULL, nios2_last_label);
  3282. else if (nios2_current_align > nios2_min_align)
  3283. nios2_current_align = nios2_min_align;
  3284. nios2_last_label = NULL;
  3285. /* We don't want to clobber to op_str
  3286. because we want to be able to use it in messages. */
  3287. op_strdup = strdup (op_str);
  3288. insn->insn_tokens[0] = strtok (op_strdup, " ");
  3289. argstr = strtok (NULL, "");
  3290. /* Assemble the opcode. */
  3291. insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]);
  3292. insn->insn_reloc = NULL;
  3293. if (insn->insn_nios2_opcode != NULL)
  3294. {
  3295. nios2_ps_insn_infoS *ps_insn = NULL;
  3296. /* Note if we've seen a 16-bit instruction. */
  3297. if (insn->insn_nios2_opcode->size == 2)
  3298. nios2_min_align = 1;
  3299. /* Set the opcode for the instruction. */
  3300. insn->insn_code = insn->insn_nios2_opcode->match;
  3301. insn->constant_bits = 0;
  3302. /* Parse the arguments pointed to by argstr. */
  3303. if (nios2_mode == NIOS2_MODE_ASSEMBLE)
  3304. nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args,
  3305. (char **) &insn->insn_tokens[1]);
  3306. else
  3307. nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test,
  3308. (char **) &insn->insn_tokens[1]);
  3309. /* We need to preserve the MOVIA macro as this is clobbered by
  3310. translate_pseudo_insn. */
  3311. if (insn->insn_nios2_opcode->pinfo == NIOS2_INSN_MACRO_MOVIA)
  3312. saved_pinfo = NIOS2_INSN_MACRO_MOVIA;
  3313. /* If the instruction is an pseudo-instruction, we want to replace it
  3314. with its real equivalent, and then continue. */
  3315. if ((insn->insn_nios2_opcode->pinfo & NIOS2_INSN_MACRO)
  3316. == NIOS2_INSN_MACRO)
  3317. ps_insn = nios2_translate_pseudo_insn (insn);
  3318. /* Assemble the parsed arguments into the instruction word. */
  3319. nios2_assemble_args (insn);
  3320. /* Handle relaxation and other transformations. */
  3321. if (nios2_as_options.relax != relax_none
  3322. && !nios2_as_options.noat
  3323. && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_UBRANCH)
  3324. output_ubranch (insn);
  3325. else if (nios2_as_options.relax != relax_none
  3326. && !nios2_as_options.noat
  3327. && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CBRANCH)
  3328. output_cbranch (insn);
  3329. else if (nios2_as_options.relax == relax_all
  3330. && !nios2_as_options.noat
  3331. && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CALL
  3332. && insn->insn_reloc
  3333. && ((insn->insn_reloc->reloc_type
  3334. == BFD_RELOC_NIOS2_CALL26)
  3335. || (insn->insn_reloc->reloc_type
  3336. == BFD_RELOC_NIOS2_CALL26_NOAT)))
  3337. output_call (insn);
  3338. else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA)
  3339. output_movia (insn);
  3340. else
  3341. output_insn (insn);
  3342. if (ps_insn)
  3343. nios2_cleanup_pseudo_insn (insn, ps_insn);
  3344. }
  3345. else
  3346. /* Unrecognised instruction - error. */
  3347. as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
  3348. /* Don't leak memory. */
  3349. free (op_strdup);
  3350. }
  3351. /* Round up section size. */
  3352. valueT
  3353. md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size)
  3354. {
  3355. /* I think byte alignment is fine here. */
  3356. return size;
  3357. }
  3358. /* Implement TC_FORCE_RELOCATION. */
  3359. int
  3360. nios2_force_relocation (fixS *fixp)
  3361. {
  3362. if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  3363. || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
  3364. || fixp->fx_r_type == BFD_RELOC_NIOS2_ALIGN)
  3365. return 1;
  3366. return generic_force_reloc (fixp);
  3367. }
  3368. /* Implement tc_fix_adjustable. */
  3369. int
  3370. nios2_fix_adjustable (fixS *fixp)
  3371. {
  3372. if (fixp->fx_addsy == NULL)
  3373. return 1;
  3374. #ifdef OBJ_ELF
  3375. /* Prevent all adjustments to global symbols. */
  3376. if (OUTPUT_FLAVOR == bfd_target_elf_flavour
  3377. && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
  3378. return 0;
  3379. #endif
  3380. if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  3381. || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  3382. return 0;
  3383. /* Preserve relocations against symbols with function type. */
  3384. if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
  3385. return 0;
  3386. /* Don't allow symbols to be discarded on GOT related relocs. */
  3387. if (fixp->fx_r_type == BFD_RELOC_NIOS2_GOT16
  3388. || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL16
  3389. || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
  3390. || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
  3391. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
  3392. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
  3393. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
  3394. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
  3395. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
  3396. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPMOD
  3397. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
  3398. || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_TPREL
  3399. || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
  3400. || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
  3401. || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
  3402. || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
  3403. || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
  3404. )
  3405. return 0;
  3406. return 1;
  3407. }
  3408. /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
  3409. it is used to remove *ABS* references from the symbol table. */
  3410. int
  3411. nios2_frob_symbol (symbolS *symp)
  3412. {
  3413. if ((OUTPUT_FLAVOR == bfd_target_elf_flavour
  3414. && symp == section_symbol (absolute_section))
  3415. || !S_IS_DEFINED (symp))
  3416. return 1;
  3417. else
  3418. return 0;
  3419. }
  3420. /* The function tc_gen_reloc creates a relocation structure for the
  3421. fixup fixp, and returns a pointer to it. This structure is passed
  3422. to bfd_install_relocation so that it can be written to the object
  3423. file for linking. */
  3424. arelent *
  3425. tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
  3426. {
  3427. arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
  3428. reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
  3429. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  3430. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  3431. reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */
  3432. if (fixp->fx_pcrel)
  3433. {
  3434. switch (fixp->fx_r_type)
  3435. {
  3436. case BFD_RELOC_16:
  3437. fixp->fx_r_type = BFD_RELOC_16_PCREL;
  3438. break;
  3439. case BFD_RELOC_NIOS2_LO16:
  3440. fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO;
  3441. break;
  3442. case BFD_RELOC_NIOS2_HIADJ16:
  3443. fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA;
  3444. break;
  3445. default:
  3446. break;
  3447. }
  3448. }
  3449. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  3450. if (reloc->howto == NULL)
  3451. {
  3452. as_bad_where (fixp->fx_file, fixp->fx_line,
  3453. _("can't represent relocation type %s"),
  3454. bfd_get_reloc_code_name (fixp->fx_r_type));
  3455. /* Set howto to a garbage value so that we can keep going. */
  3456. reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
  3457. gas_assert (reloc->howto != NULL);
  3458. }
  3459. return reloc;
  3460. }
  3461. long
  3462. md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
  3463. {
  3464. return 0;
  3465. }
  3466. /* Called just before the assembler exits. */
  3467. void
  3468. md_end ()
  3469. {
  3470. /* FIXME - not yet implemented */
  3471. }
  3472. /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
  3473. Otherwise we have no need to default values of symbols. */
  3474. symbolS *
  3475. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  3476. {
  3477. #ifdef OBJ_ELF
  3478. if (name[0] == '_' && name[1] == 'G'
  3479. && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
  3480. {
  3481. if (!GOT_symbol)
  3482. {
  3483. if (symbol_find (name))
  3484. as_bad ("GOT already in the symbol table");
  3485. GOT_symbol = symbol_new (name, undefined_section,
  3486. (valueT) 0, &zero_address_frag);
  3487. }
  3488. return GOT_symbol;
  3489. }
  3490. #endif
  3491. return 0;
  3492. }
  3493. /* Implement tc_frob_label. */
  3494. void
  3495. nios2_frob_label (symbolS *lab)
  3496. {
  3497. /* Emit dwarf information. */
  3498. dwarf2_emit_label (lab);
  3499. /* Update the label's address with the current output pointer. */
  3500. symbol_set_frag (lab, frag_now);
  3501. S_SET_VALUE (lab, (valueT) frag_now_fix ());
  3502. /* Record this label for future adjustment after we find out what
  3503. kind of data it references, and the required alignment therewith. */
  3504. nios2_last_label = lab;
  3505. }
  3506. /* Implement md_cons_align. */
  3507. void
  3508. nios2_cons_align (int size)
  3509. {
  3510. int log_size = 0;
  3511. const char *pfill = NULL;
  3512. while ((size >>= 1) != 0)
  3513. ++log_size;
  3514. if (subseg_text_p (now_seg))
  3515. pfill = (const char *) nop32;
  3516. else
  3517. pfill = NULL;
  3518. if (nios2_auto_align_on)
  3519. nios2_align (log_size, pfill, NULL);
  3520. nios2_last_label = NULL;
  3521. }
  3522. /* Map 's' to SHF_NIOS2_GPREL. */
  3523. /* This is from the Alpha code tc-alpha.c. */
  3524. int
  3525. nios2_elf_section_letter (int letter, char **ptr_msg)
  3526. {
  3527. if (letter == 's')
  3528. return SHF_NIOS2_GPREL;
  3529. *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
  3530. return -1;
  3531. }
  3532. /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
  3533. /* This is from the Alpha code tc-alpha.c. */
  3534. flagword
  3535. nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED)
  3536. {
  3537. if (attr & SHF_NIOS2_GPREL)
  3538. flags |= SEC_SMALL_DATA;
  3539. return flags;
  3540. }
  3541. /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
  3542. bfd_reloc_code_real_type
  3543. nios2_cons (expressionS *exp, int size)
  3544. {
  3545. bfd_reloc_code_real_type nios2_tls_ldo_reloc = BFD_RELOC_NONE;
  3546. SKIP_WHITESPACE ();
  3547. if (input_line_pointer[0] == '%')
  3548. {
  3549. if (strprefix (input_line_pointer + 1, "tls_ldo"))
  3550. {
  3551. if (size != 4)
  3552. as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
  3553. size);
  3554. else
  3555. {
  3556. input_line_pointer += 8;
  3557. nios2_tls_ldo_reloc = BFD_RELOC_NIOS2_TLS_DTPREL;
  3558. }
  3559. }
  3560. if (nios2_tls_ldo_reloc != BFD_RELOC_NONE)
  3561. {
  3562. SKIP_WHITESPACE ();
  3563. if (input_line_pointer[0] != '(')
  3564. as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
  3565. else
  3566. {
  3567. int c;
  3568. char *end = ++input_line_pointer;
  3569. int npar = 0;
  3570. for (c = *end; !is_end_of_line[c]; end++, c = *end)
  3571. if (c == '(')
  3572. npar++;
  3573. else if (c == ')')
  3574. {
  3575. if (!npar)
  3576. break;
  3577. npar--;
  3578. }
  3579. if (c != ')')
  3580. as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
  3581. else
  3582. {
  3583. *end = '\0';
  3584. expression (exp);
  3585. *end = c;
  3586. if (input_line_pointer != end)
  3587. as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
  3588. else
  3589. {
  3590. input_line_pointer++;
  3591. SKIP_WHITESPACE ();
  3592. c = *input_line_pointer;
  3593. if (! is_end_of_line[c] && c != ',')
  3594. as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
  3595. }
  3596. }
  3597. }
  3598. }
  3599. }
  3600. if (nios2_tls_ldo_reloc == BFD_RELOC_NONE)
  3601. expression (exp);
  3602. return nios2_tls_ldo_reloc;
  3603. }
  3604. /* Implement HANDLE_ALIGN. */
  3605. void
  3606. nios2_handle_align (fragS *fragp)
  3607. {
  3608. /* If we are expecting to relax in the linker, then we must output a
  3609. relocation to tell the linker we are aligning code. */
  3610. if (nios2_as_options.relax == relax_all
  3611. && (fragp->fr_type == rs_align || fragp->fr_type == rs_align_code)
  3612. && fragp->fr_address + fragp->fr_fix > 0
  3613. && fragp->fr_offset > 1
  3614. && now_seg != bss_section)
  3615. fix_new (fragp, fragp->fr_fix, 0, &abs_symbol, fragp->fr_offset, 0,
  3616. BFD_RELOC_NIOS2_ALIGN);
  3617. }
  3618. /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
  3619. register number. */
  3620. int
  3621. nios2_regname_to_dw2regnum (char *regname)
  3622. {
  3623. struct nios2_reg *r = nios2_reg_lookup (regname);
  3624. if (r == NULL)
  3625. return -1;
  3626. return r->index;
  3627. }
  3628. /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
  3629. unwind information for this procedure. */
  3630. void
  3631. nios2_frame_initial_instructions (void)
  3632. {
  3633. cfi_add_CFA_def_cfa (27, 0);
  3634. }
  3635. #ifdef OBJ_ELF
  3636. /* Some special processing for a Nios II ELF file. */
  3637. void
  3638. nios2_elf_final_processing (void)
  3639. {
  3640. elf_elfheader (stdoutput)->e_flags = nios2_architecture;
  3641. }
  3642. #endif