i386.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /* i386.c -- Assemble code for the Intel 80386
  2. Copyright (C) 1989, Free Software Foundation.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. Intel 80386 machine specific gas.
  17. Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
  18. Bugs & suggestions are completely welcome. This is free software.
  19. Please help us make it better.
  20. */
  21. #include <stdio.h>
  22. #include <varargs.h>
  23. #include <ctype.h>
  24. #include "as.h"
  25. #include "read.h"
  26. #include "flonum.h"
  27. #include "obstack.h"
  28. #include "frags.h"
  29. #include "struc-symbol.h"
  30. #include "expr.h"
  31. #include "symbols.h"
  32. #include "hash.h"
  33. #include "md.h"
  34. #include "i386.h"
  35. #include "i386-opcode.h"
  36. long omagic = OMAGIC;
  37. char FLT_CHARS[] = "fFdDxX";
  38. char EXP_CHARS[] = "eE";
  39. char line_comment_chars[] = "#";
  40. char comment_chars[] = "#/";
  41. /* tables for lexical analysis */
  42. static char opcode_chars[256];
  43. static char register_chars[256];
  44. static char operand_chars[256];
  45. static char space_chars[256];
  46. static char identifier_chars[256];
  47. static char digit_chars[256];
  48. /* lexical macros */
  49. #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
  50. #define is_operand_char(x) (operand_chars[(unsigned char) x])
  51. #define is_register_char(x) (register_chars[(unsigned char) x])
  52. #define is_space_char(x) (space_chars[(unsigned char) x])
  53. #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
  54. #define is_digit_char(x) (digit_chars[(unsigned char) x])
  55. /* put here all non-digit non-letter charcters that may occur in an operand */
  56. static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:";
  57. static char *ordinal_names[] = { "first", "second", "third" }; /* for printfs */
  58. /* md_assemble() always leaves the strings it's passed unaltered. To
  59. effect this we maintain a stack of saved characters that we've smashed
  60. with '\0's (indicating end of strings for various sub-fields of the
  61. assembler instruction). */
  62. static char save_stack[32];
  63. static char *save_stack_p; /* stack pointer */
  64. #define END_STRING_AND_SAVE(s) *save_stack_p++ = *s; *s = '\0'
  65. #define RESTORE_END_STRING(s) *s = *--save_stack_p
  66. /* The instruction we're assembling. */
  67. static i386_insn i;
  68. /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
  69. static expressionS disp_expressions[2], im_expressions[2];
  70. /* pointers to ebp & esp entries in reg_hash hash table */
  71. static reg_entry *ebp, *esp;
  72. static int this_operand; /* current operand we are working on */
  73. /*
  74. Interface to relax_segment.
  75. There are 2 relax states for 386 jump insns: one for conditional & one
  76. for unconditional jumps. This is because the these two types of jumps
  77. add different sizes to frags when we're figuring out what sort of jump
  78. to choose to reach a given label. */
  79. /* types */
  80. #define COND_JUMP 1 /* conditional jump */
  81. #define UNCOND_JUMP 2 /* unconditional jump */
  82. /* sizes */
  83. #define BYTE 0
  84. #define WORD 1
  85. #define DWORD 2
  86. #define UNKNOWN_SIZE 3
  87. #define ENCODE_RELAX_STATE(type,size) ((type<<2) | (size))
  88. #define SIZE_FROM_RELAX_STATE(s) \
  89. ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
  90. relax_typeS md_relax_table[] = {
  91. /*
  92. The fields are:
  93. 1) most positive reach of this state,
  94. 2) most negative reach of this state,
  95. 3) how many bytes this mode will add to the size of the current frag
  96. 4) which index into the table to try if we can't fit into this one.
  97. */
  98. {1, 1, 0, 0},
  99. {1, 1, 0, 0},
  100. {1, 1, 0, 0},
  101. {1, 1, 0, 0},
  102. /* For now we don't use word displacement jumps: they may be
  103. untrustworthy. */
  104. {127+1, -128+1, 0, ENCODE_RELAX_STATE(COND_JUMP,DWORD) },
  105. /* word conditionals add 3 bytes to frag:
  106. 2 opcode prefix; 1 displacement bytes */
  107. {32767+2, -32768+2, 3, ENCODE_RELAX_STATE(COND_JUMP,DWORD) },
  108. /* dword conditionals adds 4 bytes to frag:
  109. 1 opcode prefix; 3 displacement bytes */
  110. {0, 0, 4, 0},
  111. {1, 1, 0, 0},
  112. {127+1, -128+1, 0, ENCODE_RELAX_STATE(UNCOND_JUMP,DWORD) },
  113. /* word jmp adds 2 bytes to frag:
  114. 1 opcode prefix; 1 displacement bytes */
  115. {32767+2, -32768+2, 2, ENCODE_RELAX_STATE(UNCOND_JUMP,DWORD) },
  116. /* dword jmp adds 3 bytes to frag:
  117. 0 opcode prefix; 3 displacement bytes */
  118. {0, 0, 3, 0},
  119. {1, 1, 0, 0},
  120. };
  121. void float_cons (), cons ();
  122. /* Ignore certain directives generated by gcc. This probably should
  123. not be here. */
  124. void dummy ()
  125. {
  126. while (*input_line_pointer && *input_line_pointer != '\n')
  127. input_line_pointer++;
  128. }
  129. pseudo_typeS md_pseudo_table[] = {
  130. { "ffloat", float_cons, 'f' },
  131. { "dfloat", float_cons, 'd' },
  132. { "tfloat", float_cons, 'x' },
  133. { "value", cons, 2 },
  134. { "ident", dummy, 0 }, /* ignore these directives */
  135. { "def", dummy, 0 },
  136. { "version", dummy, 0 },
  137. { "ln", dummy, 0 },
  138. { 0, 0, 0 }
  139. };
  140. /* for interface with expression () */
  141. extern char * input_line_pointer;
  142. char * index ();
  143. char * output_invalid ();
  144. reg_entry * parse_register ();
  145. /* obstack for constructing various things in md_begin */
  146. struct obstack o;
  147. /* hash table for opcode lookup */
  148. static struct hash_control *op_hash = (struct hash_control *) 0;
  149. /* hash table for register lookup */
  150. static struct hash_control *reg_hash = (struct hash_control *) 0;
  151. /* hash table for prefix lookup */
  152. static struct hash_control *prefix_hash = (struct hash_control *) 0;
  153. void md_begin ()
  154. {
  155. char * hash_err;
  156. obstack_begin (&o,4096);
  157. /* initialize op_hash hash table */
  158. op_hash = hash_new(); /* xmalloc handles error */
  159. {
  160. register template *optab;
  161. register templates *core_optab;
  162. char *prev_name;
  163. optab = i386_optab; /* setup for loop */
  164. prev_name = optab->name;
  165. obstack_grow (&o, optab, sizeof(template));
  166. core_optab = (templates *) xmalloc (sizeof (templates));
  167. for (optab++; optab < i386_optab_end; optab++) {
  168. if (! strcmp (optab->name, prev_name)) {
  169. /* same name as before --> append to current template list */
  170. obstack_grow (&o, optab, sizeof(template));
  171. } else {
  172. /* different name --> ship out current template list;
  173. add to hash table; & begin anew */
  174. /* Note: end must be set before start! since obstack_next_free changes
  175. upon opstack_finish */
  176. core_optab->end = (template *) obstack_next_free(&o);
  177. core_optab->start = (template *) obstack_finish(&o);
  178. hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
  179. if (hash_err && *hash_err) {
  180. hash_error:
  181. as_fatal("Internal Error: Can't hash %s: %s",prev_name, hash_err);
  182. }
  183. prev_name = optab->name;
  184. core_optab = (templates *) xmalloc (sizeof(templates));
  185. obstack_grow (&o, optab, sizeof(template));
  186. }
  187. }
  188. }
  189. /* initialize reg_hash hash table */
  190. reg_hash = hash_new();
  191. {
  192. register reg_entry *regtab;
  193. for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++) {
  194. hash_err = hash_insert (reg_hash, regtab->reg_name, regtab);
  195. if (hash_err && *hash_err) goto hash_error;
  196. }
  197. }
  198. esp = (reg_entry *) hash_find (reg_hash, "esp");
  199. ebp = (reg_entry *) hash_find (reg_hash, "ebp");
  200. /* initialize reg_hash hash table */
  201. prefix_hash = hash_new();
  202. {
  203. register prefix_entry *prefixtab;
  204. for (prefixtab = i386_prefixtab;
  205. prefixtab < i386_prefixtab_end; prefixtab++) {
  206. hash_err = hash_insert (prefix_hash, prefixtab->prefix_name, prefixtab);
  207. if (hash_err && *hash_err) goto hash_error;
  208. }
  209. }
  210. /* fill in lexical tables: opcode_chars, operand_chars, space_chars */
  211. {
  212. register unsigned int c;
  213. bzero (opcode_chars, sizeof(opcode_chars));
  214. bzero (operand_chars, sizeof(operand_chars));
  215. bzero (space_chars, sizeof(space_chars));
  216. bzero (identifier_chars, sizeof(identifier_chars));
  217. bzero (digit_chars, sizeof(digit_chars));
  218. for (c = 0; c < 256; c++) {
  219. if (islower(c) || isdigit(c)) {
  220. opcode_chars[c] = c;
  221. register_chars[c] = c;
  222. } else if (isupper(c)) {
  223. opcode_chars[c] = tolower(c);
  224. register_chars[c] = opcode_chars[c];
  225. } else if (c == PREFIX_SEPERATOR) {
  226. opcode_chars[c] = c;
  227. } else if (c == ')' || c == '(') {
  228. register_chars[c] = c;
  229. }
  230. if (isupper(c) || islower(c) || isdigit(c))
  231. operand_chars[c] = c;
  232. else if (memchr(operand_special_chars, c, sizeof(operand_special_chars)))
  233. operand_chars[c] = c;
  234. if (isdigit(c) || c == '-') digit_chars[c] = c;
  235. if (isalpha(c) || c == '_' || c == '.' || isdigit(c))
  236. identifier_chars[c] = c;
  237. if (c == ' ' || c == '\t') space_chars[c] = c;
  238. }
  239. }
  240. }
  241. void md_end() {} /* not much to do here. */
  242. #ifdef DEBUG386
  243. /* debugging routines for md_assemble */
  244. static void pi (), pte (), pt (), pe (), ps ();
  245. static void pi (line, x)
  246. char * line;
  247. i386_insn *x;
  248. {
  249. register template *p;
  250. int i;
  251. fprintf (stdout, "%s: template ", line);
  252. pte (&x->tm);
  253. fprintf (stdout, " modrm: mode %x reg %x reg/mem %x",
  254. x->rm.mode, x->rm.reg, x->rm.regmem);
  255. fprintf (stdout, " base %x index %x scale %x\n",
  256. x->bi.base, x->bi.index, x->bi.scale);
  257. for (i = 0; i < x->operands; i++) {
  258. fprintf (stdout, " #%d: ", i+1);
  259. pt (x->types[i]);
  260. fprintf (stdout, "\n");
  261. if (x->types[i] & Reg) fprintf (stdout, "%s\n", x->regs[i]->reg_name);
  262. if (x->types[i] & Imm) pe (x->imms[i]);
  263. if (x->types[i] & (Disp|Abs)) pe (x->disps[i]);
  264. }
  265. }
  266. static void pte (t)
  267. template *t;
  268. {
  269. int i;
  270. fprintf (stdout, " %d operands ", t->operands);
  271. fprintf (stdout, "opcode %x ",
  272. t->base_opcode);
  273. if (t->extension_opcode != None)
  274. fprintf (stdout, "ext %x ", t->extension_opcode);
  275. if (t->opcode_modifier&D)
  276. fprintf (stdout, "D");
  277. if (t->opcode_modifier&W)
  278. fprintf (stdout, "W");
  279. fprintf (stdout, "\n");
  280. for (i = 0; i < t->operands; i++) {
  281. fprintf (stdout, " #%d type ", i+1);
  282. pt (t->operand_types[i]);
  283. fprintf (stdout, "\n");
  284. }
  285. }
  286. char *seg_names[] = {
  287. "SEG_ABSOLUTE", "SEG_TEXT", "SEG_DATA", "SEG_BSS", "SEG_UNKNOWN",
  288. "SEG_NONE", "SEG_PASS1", "SEG_GOOF", "SEG_BIG", "SEG_DIFFERENCE" };
  289. static void pe (e)
  290. expressionS *e;
  291. {
  292. fprintf (stdout, " segment %s\n", seg_names[(int) e->X_seg]);
  293. fprintf (stdout, " add_number %d (%x)\n",
  294. e->X_add_number, e->X_add_number);
  295. if (e->X_add_symbol) {
  296. fprintf (stdout, " add_symbol ");
  297. ps (e->X_add_symbol);
  298. fprintf (stdout, "\n");
  299. }
  300. if (e->X_subtract_symbol) {
  301. fprintf (stdout, " sub_symbol ");
  302. ps (e->X_subtract_symbol);
  303. fprintf (stdout, "\n");
  304. }
  305. }
  306. #define SYMBOL_TYPE(t) \
  307. (((t&N_TYPE) == N_UNDF) ? "UNDEFINED" : \
  308. (((t&N_TYPE) == N_ABS) ? "ABSOLUTE" : \
  309. (((t&N_TYPE) == N_TEXT) ? "TEXT" : \
  310. (((t&N_TYPE) == N_DATA) ? "DATA" : \
  311. (((t&N_TYPE) == N_BSS) ? "BSS" : "Bad n_type!")))))
  312. static void ps (s)
  313. symbolS *s;
  314. {
  315. fprintf (stdout, "%s type %s%s",
  316. s->sy_nlist.n_un.n_name,
  317. (s->sy_nlist.n_type&N_EXT) ? "EXTERNAL " : "",
  318. SYMBOL_TYPE (s->sy_nlist.n_type));
  319. }
  320. struct type_name {
  321. uint mask;
  322. char *tname;
  323. } type_names[] = {
  324. { Reg8, "r8" }, { Reg16, "r16" }, { Reg32, "r32" }, { Imm8, "i8" },
  325. { Imm8S, "i8s" },
  326. { Imm16, "i16" }, { Imm32, "i32" }, { Mem8, "Mem8"}, { Mem16, "Mem16"},
  327. { Mem32, "Mem32"}, { BaseIndex, "BaseIndex" },
  328. { Abs8, "Abs8" }, { Abs16, "Abs16" }, { Abs32, "Abs32" },
  329. { Disp8, "d8" }, { Disp16, "d16" },
  330. { Disp32, "d32" }, { SReg2, "SReg2" }, { SReg3, "SReg3" }, { Acc, "Acc" },
  331. { InOutPortReg, "InOutPortReg" }, { ShiftCount, "ShiftCount" },
  332. { Imm1, "i1" }, { Control, "control reg" }, {Test, "test reg"},
  333. { FloatReg, "FReg"}, {FloatAcc, "FAcc"},
  334. { JumpAbsolute, "Jump Absolute"},
  335. { 0, "" }
  336. };
  337. static void pt (t)
  338. uint t;
  339. {
  340. register struct type_name *ty;
  341. if (t == Unknown) {
  342. fprintf (stdout, "Unknown");
  343. } else {
  344. for (ty = type_names; ty->mask; ty++)
  345. if (t & ty->mask) fprintf (stdout, "%s, ", ty->tname);
  346. }
  347. fflush (stdout);
  348. }
  349. #endif /* DEBUG386 */
  350. /*
  351. This is the guts of the machine-dependent assembler. LINE points to a
  352. machine dependent instruction. This funciton is supposed to emit
  353. the frags/bytes it assembles to.
  354. */
  355. void md_assemble (line)
  356. char *line;
  357. {
  358. /* Holds temlate once we've found it. */
  359. register template * t;
  360. /* Possible templates for current insn */
  361. templates *current_templates = (templates *) 0;
  362. /* Initialize globals. */
  363. bzero (&i, sizeof(i));
  364. bzero (disp_expressions, sizeof(disp_expressions));
  365. bzero (im_expressions, sizeof(im_expressions));
  366. save_stack_p = save_stack; /* reset stack pointer */
  367. /* Fist parse an opcode & call i386_operand for the operands.
  368. We assume that the scrubber has arranged it so that line[0] is the valid
  369. start of a (possibly prefixed) opcode. */
  370. {
  371. register char *l = line; /* Fast place to put LINE. */
  372. /* TRUE if operand is pending after ','. */
  373. uint expecting_operand = 0;
  374. /* TRUE if we found a prefix only acceptable with string insns. */
  375. uint expecting_string_instruction = 0;
  376. /* Non-zero if operand parens not balenced. */
  377. uint paren_not_balenced;
  378. char * token_start = l;
  379. while (! is_space_char(*l) && *l != END_OF_INSN) {
  380. if (! is_opcode_char(*l)) {
  381. as_warn ("invalid character %s in opcode", output_invalid(*l));
  382. return;
  383. } else if (*l != PREFIX_SEPERATOR) {
  384. *l = opcode_chars[(unsigned char) *l]; /* fold case of opcodes */
  385. l++;
  386. } else { /* this opcode's got a prefix */
  387. register int q;
  388. register prefix_entry * prefix;
  389. if (l == token_start) {
  390. as_warn ("expecting prefix; got nothing");
  391. return;
  392. }
  393. END_STRING_AND_SAVE (l);
  394. prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
  395. if (! prefix) {
  396. as_warn ("no such opcode prefix ('%s')", token_start);
  397. return;
  398. }
  399. RESTORE_END_STRING (l);
  400. /* check for repeated prefix */
  401. for (q = 0; q < i.prefixes; q++)
  402. if (i.prefix[q] == prefix->prefix_code) {
  403. as_warn ("same prefix used twice; you don't really want this!");
  404. return;
  405. }
  406. if (i.prefixes == MAX_PREFIXES) {
  407. as_warn ("too many opcode prefixes");
  408. return;
  409. }
  410. i.prefix[i.prefixes++] = prefix->prefix_code;
  411. if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
  412. expecting_string_instruction = TRUE;
  413. /* skip past PREFIX_SEPERATOR and reset token_start */
  414. token_start = ++l;
  415. }
  416. }
  417. END_STRING_AND_SAVE (l);
  418. if (token_start == l) {
  419. as_warn ("expecting opcode; got nothing");
  420. return;
  421. }
  422. /* Lookup insn in hash; try intel & att naming conventions if appropriate;
  423. that is: we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
  424. current_templates = (templates *) hash_find (op_hash, token_start);
  425. if (! current_templates) {
  426. int last_index = strlen(token_start) - 1;
  427. char last_char = token_start[last_index];
  428. switch (last_char) {
  429. case DWORD_OPCODE_SUFFIX:
  430. case WORD_OPCODE_SUFFIX:
  431. case BYTE_OPCODE_SUFFIX:
  432. token_start[last_index] = '\0';
  433. current_templates = (templates *) hash_find (op_hash, token_start);
  434. token_start[last_index] = last_char;
  435. i.suffix = last_char;
  436. }
  437. if (!current_templates) {
  438. as_warn ("no such 386 instruction: `%s'", token_start); return;
  439. }
  440. }
  441. RESTORE_END_STRING (l);
  442. /* check for rep/repne without a string instruction */
  443. if (expecting_string_instruction &&
  444. ! IS_STRING_INSTRUCTION (current_templates->
  445. start->base_opcode)) {
  446. as_warn ("expecting string instruction after rep/repne");
  447. return;
  448. }
  449. /* There may be operands to parse. */
  450. if (*l != END_OF_INSN &&
  451. /* For string instructions, we ignore any operands if given. This
  452. kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
  453. the operands are always going to be the same, and are not really
  454. encoded in machine code. */
  455. ! IS_STRING_INSTRUCTION (current_templates->
  456. start->base_opcode)) {
  457. /* parse operands */
  458. do {
  459. /* skip optional white space before operand */
  460. while (! is_operand_char(*l) && *l != END_OF_INSN) {
  461. if (! is_space_char(*l)) {
  462. as_warn ("invalid character %s before %s operand",
  463. output_invalid(*l),
  464. ordinal_names[i.operands]);
  465. return;
  466. }
  467. l++;
  468. }
  469. token_start = l; /* after white space */
  470. paren_not_balenced = 0;
  471. while (paren_not_balenced || *l != ',') {
  472. if (*l == END_OF_INSN) {
  473. if (paren_not_balenced) {
  474. as_warn ("unbalenced parenthesis in %s operand.",
  475. ordinal_names[i.operands]);
  476. return;
  477. } else break; /* we are done */
  478. } else if (! is_operand_char(*l)) {
  479. as_warn ("invalid character %s in %s operand",
  480. output_invalid(*l),
  481. ordinal_names[i.operands]);
  482. return;
  483. }
  484. if (*l == '(') ++paren_not_balenced;
  485. if (*l == ')') --paren_not_balenced;
  486. l++;
  487. }
  488. if (l != token_start) { /* yes, we've read in another operand */
  489. uint operand_ok;
  490. this_operand = i.operands++;
  491. if (i.operands > MAX_OPERANDS) {
  492. as_warn ("spurious operands; (%d operands/instruction max)",
  493. MAX_OPERANDS);
  494. return;
  495. }
  496. /* now parse operand adding info to 'i' as we go along */
  497. END_STRING_AND_SAVE (l);
  498. operand_ok = i386_operand (token_start);
  499. RESTORE_END_STRING (l); /* restore old contents */
  500. if (!operand_ok) return;
  501. } else {
  502. if (expecting_operand) {
  503. expecting_operand_after_comma:
  504. as_warn ("expecting operand after ','; got nothing");
  505. return;
  506. }
  507. if (*l == ',') {
  508. as_warn ("expecting operand before ','; got nothing");
  509. return;
  510. }
  511. }
  512. /* now *l must be either ',' or END_OF_INSN */
  513. if (*l == ',') {
  514. if (*++l == END_OF_INSN) { /* just skip it, if it's \n complain */
  515. goto expecting_operand_after_comma;
  516. }
  517. expecting_operand = TRUE;
  518. }
  519. } while (*l != END_OF_INSN); /* until we get end of insn */
  520. }
  521. }
  522. /* Now we've parsed the opcode into a set of templates, and have the
  523. operands at hand.
  524. Next, we find a template that matches the given insn,
  525. making sure the overlap of the given operands types is consistent
  526. with the template operand types. */
  527. #define MATCH(overlap,given_type) \
  528. (overlap && \
  529. (overlap & (JumpAbsolute|BaseIndex|Mem8)) \
  530. == (given_type & (JumpAbsolute|BaseIndex|Mem8)))
  531. /* If m0 and m1 are register matches they must be consistent
  532. with the expected operand types t0 and t1.
  533. That is, if both m0 & m1 are register matches
  534. i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
  535. then, either 1. or 2. must be true:
  536. 1. the expected operand type register overlap is null:
  537. (t0 & t1 & Reg) == 0
  538. AND
  539. the given register overlap is null:
  540. (m0 & m1 & Reg) == 0
  541. 2. the expected operand type register overlap == the given
  542. operand type overlap: (t0 & t1 & m0 & m1 & Reg).
  543. */
  544. #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
  545. ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
  546. ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
  547. ((t0 & t1) & (m0 & m1) & (Reg)) \
  548. ) : 1)
  549. {
  550. register uint overlap0, overlap1;
  551. expressionS * exp;
  552. uint overlap2;
  553. uint found_reverse_match;
  554. overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
  555. for (t = current_templates->start;
  556. t < current_templates->end;
  557. t++) {
  558. /* must have right number of operands */
  559. if (i.operands != t->operands) continue;
  560. else if (!t->operands) break; /* 0 operands always matches */
  561. overlap0 = i.types[0] & t->operand_types[0];
  562. switch (t->operands) {
  563. case 1:
  564. if (! MATCH (overlap0,i.types[0])) continue;
  565. break;
  566. case 2: case 3:
  567. overlap1 = i.types[1] & t->operand_types[1];
  568. if (! MATCH (overlap0,i.types[0]) ||
  569. ! MATCH (overlap1,i.types[1]) ||
  570. ! CONSISTENT_REGISTER_MATCH(overlap0, overlap1,
  571. t->operand_types[0],
  572. t->operand_types[1])) {
  573. /* check if other direction is valid ... */
  574. if (! (t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
  575. continue;
  576. /* try reversing direction of operands */
  577. overlap0 = i.types[0] & t->operand_types[1];
  578. overlap1 = i.types[1] & t->operand_types[0];
  579. if (! MATCH (overlap0,i.types[0]) ||
  580. ! MATCH (overlap1,i.types[1]) ||
  581. ! CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  582. t->operand_types[0],
  583. t->operand_types[1])) {
  584. /* does not match either direction */
  585. continue;
  586. }
  587. /* found a reverse match here -- slip through */
  588. /* found_reverse_match holds which of D or FloatD we've found */
  589. found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
  590. } /* endif: not forward match */
  591. /* found either forward/reverse 2 operand match here */
  592. if (t->operands == 3) {
  593. overlap2 = i.types[2] & t->operand_types[2];
  594. if (! MATCH (overlap2,i.types[2]) ||
  595. ! CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
  596. t->operand_types[0],
  597. t->operand_types[2]) ||
  598. ! CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
  599. t->operand_types[1],
  600. t->operand_types[2]))
  601. continue;
  602. }
  603. /* found either forward/reverse 2 or 3 operand match here:
  604. slip through to break */
  605. }
  606. break; /* we've found a match; break out of loop */
  607. } /* for (t = ... */
  608. if (t == current_templates->end) { /* we found no match */
  609. as_warn ("operands given don't match any known 386 instruction");
  610. return;
  611. }
  612. /* Copy the template we found (we may change it!). */
  613. bcopy (t, &i.tm, sizeof (template));
  614. t = &i.tm; /* alter new copy of template */
  615. /* If there's no opcode suffix we try to invent one based on register
  616. operands. */
  617. if (! i.suffix && i.reg_operands) {
  618. /* We take i.suffix from the LAST register operand specified. This
  619. assumes that the last register operands is the destination register
  620. operand. */
  621. int o;
  622. for (o = 0; o < MAX_OPERANDS; o++)
  623. if (i.types[o] & Reg) {
  624. i.suffix = (i.types[o] == Reg8) ? BYTE_OPCODE_SUFFIX :
  625. (i.types[o] == Reg16) ? WORD_OPCODE_SUFFIX :
  626. DWORD_OPCODE_SUFFIX;
  627. }
  628. }
  629. /* Make still unresolved immediate matches conform to size of immediate
  630. given in i.suffix. Note: overlap2 cannot be an immediate!
  631. We assume this. */
  632. if ((overlap0 & (Imm8|Imm8S|Imm16|Imm32))
  633. && overlap0 != Imm8 && overlap0 != Imm8S
  634. && overlap0 != Imm16 && overlap0 != Imm32) {
  635. if (! i.suffix) {
  636. as_warn ("no opcode suffix given; can't determine immediate size");
  637. return;
  638. }
  639. overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8|Imm8S) :
  640. (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  641. }
  642. if ((overlap1 & (Imm8|Imm8S|Imm16|Imm32))
  643. && overlap1 != Imm8 && overlap1 != Imm8S
  644. && overlap1 != Imm16 && overlap1 != Imm32) {
  645. if (! i.suffix) {
  646. as_warn ("no opcode suffix given; can't determine immediate size");
  647. return;
  648. }
  649. overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8|Imm8S) :
  650. (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  651. }
  652. i.types[0] = overlap0;
  653. i.types[1] = overlap1;
  654. i.types[2] = overlap2;
  655. if (overlap0 & ImplicitRegister) i.reg_operands--;
  656. if (overlap1 & ImplicitRegister) i.reg_operands--;
  657. if (overlap2 & ImplicitRegister) i.reg_operands--;
  658. if (overlap0 & Imm1) i.imm_operands = 0; /* kludge for shift insns */
  659. if (found_reverse_match) {
  660. uint save;
  661. save = t->operand_types[0];
  662. t->operand_types[0] = t->operand_types[1];
  663. t->operand_types[1] = save;
  664. }
  665. /* Finalize opcode. First, we change the opcode based on the operand
  666. size given by i.suffix: we never have to change things for byte insns,
  667. or when no opcode suffix is need to size the operands. */
  668. if (! i.suffix && (t->opcode_modifier & W)) {
  669. as_warn ("no opcode suffix given and no register operands; can't size instruction");
  670. return;
  671. }
  672. if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX) {
  673. /* Select between byte and word/dword operations. */
  674. if (t->opcode_modifier & W)
  675. t->base_opcode |= W;
  676. /* Now select between word & dword operations via the
  677. operand size prefix. */
  678. if (i.suffix == WORD_OPCODE_SUFFIX) {
  679. if (i.prefixes == MAX_PREFIXES) {
  680. as_warn ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
  681. MAX_PREFIXES);
  682. return;
  683. }
  684. i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
  685. }
  686. }
  687. /* For insns with operands there are more diddles to do to the opcode. */
  688. if (i.operands) {
  689. /* If we found a reverse match we must alter the opcode direction bit
  690. found_reverse_match holds bit to set (different for int &
  691. float insns). */
  692. if (found_reverse_match) {
  693. t->base_opcode |= found_reverse_match;
  694. }
  695. /*
  696. The imul $imm, %reg instruction is converted into
  697. imul $imm, %reg, %reg. */
  698. if (t->opcode_modifier & imulKludge) {
  699. i.regs[2] = i.regs[1]; /* Pretend we saw the 3 operand case. */
  700. i.reg_operands = 2;
  701. }
  702. /* Certain instructions expect the destination to be in the i.rm.reg
  703. field. This is by far the exceptional case. For these instructions,
  704. if the source operand is a register, we must reverse the i.rm.reg
  705. and i.rm.regmem fields. We accomplish this by faking that the
  706. two register operands were given in the reverse order. */
  707. if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2) {
  708. uint first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
  709. uint second_reg_operand = first_reg_operand + 1;
  710. reg_entry *tmp = i.regs[first_reg_operand];
  711. i.regs[first_reg_operand] = i.regs[second_reg_operand];
  712. i.regs[second_reg_operand] = tmp;
  713. }
  714. if (t->opcode_modifier & ShortForm) {
  715. /* The register or float register operand is in operand 0 or 1. */
  716. uint o = (i.types[0] & (Reg|FloatReg)) ? 0 : 1;
  717. /* Register goes in low 3 bits of opcode. */
  718. t->base_opcode |= i.regs[o]->reg_num;
  719. } else if (t->opcode_modifier & ShortFormW) {
  720. /* Short form with 0x8 width bit. Register is always dest. operand */
  721. t->base_opcode |= i.regs[1]->reg_num;
  722. if (i.suffix == WORD_OPCODE_SUFFIX ||
  723. i.suffix == DWORD_OPCODE_SUFFIX)
  724. t->base_opcode |= 0x8;
  725. } else if (t->opcode_modifier & Seg2ShortForm) {
  726. if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1) {
  727. as_warn ("you can't 'pop cs' on the 386.");
  728. return;
  729. }
  730. t->base_opcode |= (i.regs[0]->reg_num << 3);
  731. } else if (t->opcode_modifier & Seg3ShortForm) {
  732. /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
  733. 'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
  734. So, only if i.regs[0]->reg_num == 5 (%gs) do we need
  735. to change the opcode. */
  736. if (i.regs[0]->reg_num == 5)
  737. t->base_opcode |= 0x08;
  738. } else if (t->opcode_modifier & Modrm) {
  739. /* The opcode is completed (modulo t->extension_opcode which must
  740. be put into the modrm byte.
  741. Now, we make the modrm & index base bytes based on all the info
  742. we've collected. */
  743. /* i.reg_operands MUST be the number of real register operands;
  744. implicit registers do not count. */
  745. if (i.reg_operands == 2) {
  746. uint source, dest;
  747. source = (i.types[0] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 0 : 1;
  748. dest = source + 1;
  749. i.rm.mode = 3;
  750. /* We must be careful to make sure that all segment/control/test/
  751. debug registers go into the i.rm.reg field (despite the whether
  752. they are source or destination operands). */
  753. if (i.regs[dest]->reg_type & (SReg2|SReg3|Control|Debug|Test)) {
  754. i.rm.reg = i.regs[dest]->reg_num;
  755. i.rm.regmem = i.regs[source]->reg_num;
  756. } else {
  757. i.rm.reg = i.regs[source]->reg_num;
  758. i.rm.regmem = i.regs[dest]->reg_num;
  759. }
  760. } else { /* if it's not 2 reg operands... */
  761. if (i.mem_operands) {
  762. uint fake_zero_displacement = FALSE;
  763. uint o = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
  764. /* Encode memory operand into modrm byte and base index byte. */
  765. if (i.base_reg == esp && ! i.index_reg) {
  766. /* <disp>(%esp) becomes two byte modrm with no index register. */
  767. i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  768. i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  769. i.bi.base = ESP_REG_NUM;
  770. i.bi.index = NO_INDEX_REGISTER;
  771. i.bi.scale = 0; /* Must be zero! */
  772. } else if (i.base_reg == ebp && !i.index_reg) {
  773. if (! (i.types[o] & Disp)) {
  774. /* Must fake a zero byte displacement.
  775. There is no direct way to code '(%ebp)' directly. */
  776. fake_zero_displacement = TRUE;
  777. /* fake_zero_displacement code does not set this. */
  778. i.types[o] |= Disp8;
  779. }
  780. i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  781. i.rm.regmem = EBP_REG_NUM;
  782. } else if (! i.base_reg && (i.types[o] & BaseIndex)) {
  783. /* There are three cases here.
  784. Case 1: '<32bit disp>(,1)' -- indirect absolute.
  785. (Same as cases 2 & 3 with NO index register)
  786. Case 2: <32bit disp> (,<index>) -- no base register with disp
  787. Case 3: (, <index>) --- no base register;
  788. no disp (must add 32bit 0 disp). */
  789. i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  790. i.rm.mode = 0; /* 32bit mode */
  791. i.bi.base = NO_BASE_REGISTER;
  792. i.types[o] &= ~Disp;
  793. i.types[o] |= Disp32; /* Must be 32bit! */
  794. if (i.index_reg) { /* case 2 or case 3 */
  795. i.bi.index = i.index_reg->reg_num;
  796. i.bi.scale = i.log2_scale_factor;
  797. if (i.disp_operands == 0)
  798. fake_zero_displacement = TRUE; /* case 3 */
  799. } else {
  800. i.bi.index = NO_INDEX_REGISTER;
  801. i.bi.scale = 0;
  802. }
  803. } else if (i.disp_operands && !i.base_reg && !i.index_reg) {
  804. /* Operand is just <32bit disp> */
  805. i.rm.regmem = EBP_REG_NUM;
  806. i.rm.mode = 0;
  807. i.types[o] &= ~Disp;
  808. i.types[o] |= Disp32;
  809. } else {
  810. /* It's not a special case; rev'em up. */
  811. i.rm.regmem = i.base_reg->reg_num;
  812. i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  813. if (i.index_reg) {
  814. i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  815. i.bi.base = i.base_reg->reg_num;
  816. i.bi.index = i.index_reg->reg_num;
  817. i.bi.scale = i.log2_scale_factor;
  818. if (i.base_reg == ebp && i.disp_operands == 0) { /* pace */
  819. fake_zero_displacement = TRUE;
  820. i.types[o] |= Disp8;
  821. i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  822. }
  823. }
  824. }
  825. if (fake_zero_displacement) {
  826. /* Fakes a zero displacement assuming that i.types[o] holds
  827. the correct displacement size. */
  828. exp = &disp_expressions[i.disp_operands++];
  829. i.disps[o] = exp;
  830. exp->X_seg = SEG_ABSOLUTE;
  831. exp->X_add_number = 0;
  832. exp->X_add_symbol = (symbolS *) 0;
  833. exp->X_subtract_symbol = (symbolS *) 0;
  834. }
  835. /* Select the correct segment for the memory operand. */
  836. if (i.seg) {
  837. uint seg_index;
  838. seg_entry * default_seg;
  839. if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING) {
  840. seg_index = (i.rm.mode<<3) | i.bi.base;
  841. default_seg = two_byte_segment_defaults [seg_index];
  842. } else {
  843. seg_index = (i.rm.mode<<3) | i.rm.regmem;
  844. default_seg = one_byte_segment_defaults [seg_index];
  845. }
  846. /* If the specified segment is not the default, use an
  847. opcode prefix to select it */
  848. if (i.seg != default_seg) {
  849. if (i.prefixes == MAX_PREFIXES) {
  850. as_warn ("%d prefixes given and %s segment override gives too many prefixes",
  851. MAX_PREFIXES, i.seg->seg_name);
  852. return;
  853. }
  854. i.prefix[i.prefixes++] = i.seg->seg_prefix;
  855. }
  856. }
  857. }
  858. /* Fill in i.rm.reg or i.rm.regmem field with register operand
  859. (if any) based on t->extension_opcode. Again, we must be careful
  860. to make sure that segment/control/debug/test registers are coded
  861. into the i.rm.reg field. */
  862. if (i.reg_operands) {
  863. uint o =
  864. (i.types[0] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 0 :
  865. (i.types[1] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 1 : 2;
  866. /* If there is an extension opcode to put here, the register number
  867. must be put into the regmem field. */
  868. if (t->extension_opcode != None)
  869. i.rm.regmem = i.regs[o]->reg_num;
  870. else i.rm.reg = i.regs[o]->reg_num;
  871. /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
  872. we must set it to 3 to indicate this is a register operand
  873. int the regmem field */
  874. if (! i.mem_operands) i.rm.mode = 3;
  875. }
  876. /* Fill in i.rm.reg field with extension opcode (if any). */
  877. if (t->extension_opcode != None)
  878. i.rm.reg = t->extension_opcode;
  879. }
  880. }
  881. }
  882. }
  883. /* Handle conversion of 'int $3' --> special int3 insn. */
  884. if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3) {
  885. t->base_opcode = INT3_OPCODE;
  886. i.imm_operands = 0;
  887. }
  888. /* We are ready to output the insn. */
  889. {
  890. register char * p;
  891. /* Output jumps. */
  892. if (t->opcode_modifier & Jump) {
  893. int n = i.disps[0]->X_add_number;
  894. switch (i.disps[0]->X_seg) {
  895. case SEG_ABSOLUTE:
  896. if (FITS_IN_SIGNED_BYTE (n)) {
  897. p = frag_more (2);
  898. p[0] = t->base_opcode;
  899. p[1] = n;
  900. #if 0 /* leave out 16 bit jumps - pace */
  901. } else if (FITS_IN_SIGNED_WORD (n)) {
  902. p = frag_more (4);
  903. p[0] = WORD_PREFIX_OPCODE;
  904. p[1] = t->base_opcode;
  905. md_number_to_chars (&p[2], n, 2);
  906. #endif
  907. } else { /* It's an absolute dword displacement. */
  908. if (t->base_opcode == JUMP_PC_RELATIVE) { /* pace */
  909. /* unconditional jump */
  910. p = frag_more (5);
  911. p[0] = 0xe9;
  912. md_number_to_chars (&p[1], n, 4);
  913. } else {
  914. /* conditional jump */
  915. p = frag_more (6);
  916. p[0] = TWO_BYTE_OPCODE_ESCAPE;
  917. p[1] = t->base_opcode + 0x10;
  918. md_number_to_chars (&p[2], n, 4);
  919. }
  920. }
  921. break;
  922. default:
  923. /* It's a symbol; end frag & setup for relax.
  924. Make sure there are 6 chars left in the current frag; if not
  925. we'll have to start a new one. */
  926. /* I caught it failing with obstack_room == 6,
  927. so I changed to <= pace */
  928. if (obstack_room (&frags) <= 6) frag_new (0);
  929. p = frag_more (1);
  930. p[0] = t->base_opcode;
  931. frag_var (rs_machine_dependent,
  932. 6, /* 2 opcode/prefix + 4 displacement */
  933. 1,
  934. ((uchar) *p == JUMP_PC_RELATIVE
  935. ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
  936. : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
  937. i.disps[0]->X_add_symbol,
  938. n, p);
  939. break;
  940. }
  941. } else if (t->opcode_modifier & (JumpByte|JumpDword)) {
  942. int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
  943. int n = i.disps[0]->X_add_number;
  944. if (FITS_IN_UNSIGNED_BYTE(t->base_opcode)) {
  945. FRAG_APPEND_1_CHAR (t->base_opcode);
  946. } else {
  947. p = frag_more (2); /* opcode can be at most two bytes */
  948. /* put out high byte first: can't use md_number_to_chars! */
  949. *p++ = (t->base_opcode >> 8) & 0xff;
  950. *p = t->base_opcode & 0xff;
  951. }
  952. p = frag_more (size);
  953. switch (i.disps[0]->X_seg) {
  954. case SEG_ABSOLUTE:
  955. md_number_to_chars (p, n, size);
  956. if (size == 1 && ! FITS_IN_SIGNED_BYTE (n)) {
  957. as_warn ("loop/jecx only takes byte displacement; %d shortened to %d",
  958. n, *p);
  959. }
  960. break;
  961. default:
  962. fix_new (frag_now, p - frag_now->fr_literal, size,
  963. i.disps[0]->X_add_symbol, i.disps[0]->X_subtract_symbol,
  964. i.disps[0]->X_add_number, 1);
  965. break;
  966. }
  967. } else if (t->opcode_modifier & JumpInterSegment) {
  968. p = frag_more (1 + 2 + 4); /* 1 opcode; 2 segment; 4 offset */
  969. p[0] = t->base_opcode;
  970. if (i.imms[1]->X_seg == SEG_ABSOLUTE)
  971. md_number_to_chars (p + 1, i.imms[1]->X_add_number, 4);
  972. else
  973. fix_new (frag_now, p + 1 - frag_now->fr_literal, 4,
  974. i.imms[1]->X_add_symbol,
  975. i.imms[1]->X_subtract_symbol,
  976. i.imms[1]->X_add_number, 0);
  977. if (i.imms[0]->X_seg != SEG_ABSOLUTE)
  978. as_warn ("can't handle non absolute segment in long call/jmp");
  979. md_number_to_chars (p + 5, i.imms[0]->X_add_number, 2);
  980. } else {
  981. /* Output normal instructions here. */
  982. register char *q;
  983. /* First the prefix bytes. */
  984. for (q = i.prefix; q < i.prefix + i.prefixes; q++) {
  985. p = frag_more (1);
  986. md_number_to_chars (p, (uint) *q, 1);
  987. }
  988. /* Now the opcode; be careful about word order here! */
  989. if (FITS_IN_UNSIGNED_BYTE(t->base_opcode)) {
  990. FRAG_APPEND_1_CHAR (t->base_opcode);
  991. } else if (FITS_IN_UNSIGNED_WORD(t->base_opcode)) {
  992. p = frag_more (2);
  993. /* put out high byte first: can't use md_number_to_chars! */
  994. *p++ = (t->base_opcode >> 8) & 0xff;
  995. *p = t->base_opcode & 0xff;
  996. } else { /* opcode is either 3 or 4 bytes */
  997. if (t->base_opcode & 0xff000000) {
  998. p = frag_more (4);
  999. *p++ = (t->base_opcode >> 24) & 0xff;
  1000. } else p = frag_more (3);
  1001. *p++ = (t->base_opcode >> 16) & 0xff;
  1002. *p++ = (t->base_opcode >> 8) & 0xff;
  1003. *p = (t->base_opcode ) & 0xff;
  1004. }
  1005. /* Now the modrm byte and base index byte (if present). */
  1006. if (t->opcode_modifier & Modrm) {
  1007. p = frag_more (1);
  1008. md_number_to_chars (p, i.rm, 1);
  1009. /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
  1010. ==> need second modrm byte. */
  1011. if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3) {
  1012. p = frag_more (1);
  1013. md_number_to_chars (p, i.bi, 1);
  1014. }
  1015. }
  1016. if (i.disp_operands) {
  1017. register int n;
  1018. for (n = 0; n < i.operands; n++) {
  1019. if (i.disps[n]) {
  1020. if (i.disps[n]->X_seg == SEG_ABSOLUTE) {
  1021. if (i.types[n] & (Disp8|Abs8)) {
  1022. p = frag_more (1);
  1023. md_number_to_chars (p, i.disps[n]->X_add_number, 1);
  1024. } else if (i.types[n] & (Disp16|Abs16)) {
  1025. p = frag_more (2);
  1026. md_number_to_chars (p, i.disps[n]->X_add_number, 2);
  1027. } else { /* Disp32|Abs32 */
  1028. p = frag_more (4);
  1029. md_number_to_chars (p, i.disps[n]->X_add_number, 4);
  1030. }
  1031. } else { /* not SEG_ABSOLUTE */
  1032. /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
  1033. p = frag_more (4);
  1034. fix_new (frag_now, p - frag_now->fr_literal, 4,
  1035. i.disps[n]->X_add_symbol, i.disps[n]->X_subtract_symbol,
  1036. i.disps[n]->X_add_number, 0);
  1037. }
  1038. }
  1039. }
  1040. } /* end displacement output */
  1041. /* output immediate */
  1042. if (i.imm_operands) {
  1043. register int n;
  1044. for (n = 0; n < i.operands; n++) {
  1045. if (i.imms[n]) {
  1046. if (i.imms[n]->X_seg == SEG_ABSOLUTE) {
  1047. if (i.types[n] & (Imm8|Imm8S)) {
  1048. p = frag_more (1);
  1049. md_number_to_chars (p, i.imms[n]->X_add_number, 1);
  1050. } else if (i.types[n] & Imm16) {
  1051. p = frag_more (2);
  1052. md_number_to_chars (p, i.imms[n]->X_add_number, 2);
  1053. } else {
  1054. p = frag_more (4);
  1055. md_number_to_chars (p, i.imms[n]->X_add_number, 4);
  1056. }
  1057. } else { /* not SEG_ABSOLUTE */
  1058. /* need a 32-bit fixup (don't support 8bit non-absolute ims) */
  1059. /* try to support other sizes ... */
  1060. int size;
  1061. if (i.types[n] & (Imm8|Imm8S))
  1062. size = 1;
  1063. else if (i.types[n] & Imm16)
  1064. size = 2;
  1065. else
  1066. size = 4;
  1067. p = frag_more (size);
  1068. fix_new (frag_now, p - frag_now->fr_literal, size,
  1069. i.imms[n]->X_add_symbol, i.imms[n]->X_subtract_symbol,
  1070. i.imms[n]->X_add_number, 0);
  1071. }
  1072. }
  1073. }
  1074. } /* end immediate output */
  1075. }
  1076. #ifdef DEBUG386
  1077. if (flagseen ['D']) {
  1078. pi (line, &i);
  1079. }
  1080. #endif /* DEBUG386 */
  1081. }
  1082. return;
  1083. }
  1084. /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
  1085. on error. */
  1086. int i386_operand (operand_string)
  1087. char *operand_string;
  1088. {
  1089. register char *op_string = operand_string;
  1090. /* Address of '\0' at end of operand_string. */
  1091. char * end_of_operand_string = operand_string + strlen(operand_string);
  1092. /* Start and end of displacement string expression (if found). */
  1093. char * displacement_string_start = 0;
  1094. char * displacement_string_end;
  1095. /* We check for an absolute prefix (differentiating,
  1096. for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
  1097. if (*op_string == ABSOLUTE_PREFIX) {
  1098. op_string++;
  1099. i.types[this_operand] |= JumpAbsolute;
  1100. }
  1101. /* Check if operand is a register. */
  1102. if (*op_string == REGISTER_PREFIX) {
  1103. register reg_entry * r;
  1104. if (! (r = parse_register (op_string))) {
  1105. as_warn ("bad register name ('%s')", op_string);
  1106. return 0;
  1107. }
  1108. /* Check for segment override, rather than segment register by
  1109. searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
  1110. if ((r->reg_type & (SReg2|SReg3)) && op_string[3] == ':') {
  1111. switch (r->reg_num) {
  1112. case 0:
  1113. i.seg = &es; break;
  1114. case 1:
  1115. i.seg = &cs; break;
  1116. case 2:
  1117. i.seg = &ss; break;
  1118. case 3:
  1119. i.seg = &ds; break;
  1120. case 4:
  1121. i.seg = &fs; break;
  1122. case 5:
  1123. i.seg = &gs; break;
  1124. }
  1125. op_string += 4; /* skip % <x> s : */
  1126. operand_string = op_string; /* Pretend given string starts here. */
  1127. if (!is_digit_char(*op_string) && !is_identifier_char(*op_string)
  1128. && *op_string != '(' && *op_string != ABSOLUTE_PREFIX) {
  1129. as_warn ("bad memory operand after segment override");
  1130. return 0;
  1131. }
  1132. /* Handle case of %es:*foo. */
  1133. if (*op_string == ABSOLUTE_PREFIX) {
  1134. op_string++;
  1135. i.types[this_operand] |= JumpAbsolute;
  1136. }
  1137. goto do_memory_reference;
  1138. }
  1139. i.types[this_operand] |= r->reg_type;
  1140. i.regs[this_operand] = r;
  1141. i.reg_operands++;
  1142. } else if (*op_string == IMMEDIATE_PREFIX) { /* ... or an immediate */
  1143. char * save_input_line_pointer;
  1144. register expressionS *exp;
  1145. segT exp_seg;
  1146. if (i.imm_operands == MAX_IMMEDIATE_OPERANDS) {
  1147. as_warn ("only 1 or 2 immediate operands are allowed");
  1148. return 0;
  1149. }
  1150. exp = &im_expressions[i.imm_operands++];
  1151. i.imms [this_operand] = exp;
  1152. save_input_line_pointer = input_line_pointer;
  1153. input_line_pointer = ++op_string; /* must advance op_string! */
  1154. exp_seg = expression (exp);
  1155. input_line_pointer = save_input_line_pointer;
  1156. switch (exp_seg) {
  1157. case SEG_NONE: /* missing or bad expr becomes absolute 0 */
  1158. as_warn ("missing or invalid immediate expression '%s' taken as 0",
  1159. operand_string);
  1160. exp->X_seg = SEG_ABSOLUTE;
  1161. exp->X_add_number = 0;
  1162. exp->X_add_symbol = (symbolS *) 0;
  1163. exp->X_subtract_symbol = (symbolS *) 0;
  1164. i.types[this_operand] |= Imm;
  1165. break;
  1166. case SEG_ABSOLUTE:
  1167. i.types[this_operand] |= SMALLEST_IMM_TYPE (exp->X_add_number);
  1168. break;
  1169. case SEG_TEXT: case SEG_DATA: case SEG_BSS: case SEG_UNKNOWN:
  1170. i.types[this_operand] |= Imm32; /* this is an address ==> 32bit */
  1171. break;
  1172. default:
  1173. seg_unimplemented:
  1174. as_warn ("Unimplemented segment type %d in parse_operand", exp_seg);
  1175. return 0;
  1176. }
  1177. /* shorten this type of this operand if the instruction wants
  1178. * fewer bits than are present in the immediate. The bit field
  1179. * code can put out 'andb $0xffffff, %al', for example. pace
  1180. * also 'movw $foo,(%eax)'
  1181. */
  1182. switch (i.suffix) {
  1183. case WORD_OPCODE_SUFFIX:
  1184. i.types[this_operand] |= Imm16;
  1185. break;
  1186. case BYTE_OPCODE_SUFFIX:
  1187. i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
  1188. break;
  1189. }
  1190. } else if (is_digit_char(*op_string) || is_identifier_char(*op_string)
  1191. || *op_string == '(') {
  1192. /* This is a memory reference of some sort. */
  1193. register char * base_string;
  1194. uint found_base_index_form;
  1195. do_memory_reference:
  1196. if (i.mem_operands == MAX_MEMORY_OPERANDS) {
  1197. as_warn ("more than 1 memory reference in instruction");
  1198. return 0;
  1199. }
  1200. i.mem_operands++;
  1201. /* Determine type of memory operand from opcode_suffix;
  1202. no opcode suffix implies general memory references. */
  1203. switch (i.suffix) {
  1204. case BYTE_OPCODE_SUFFIX:
  1205. i.types[this_operand] |= Mem8;
  1206. break;
  1207. case WORD_OPCODE_SUFFIX:
  1208. i.types[this_operand] |= Mem16;
  1209. break;
  1210. case DWORD_OPCODE_SUFFIX:
  1211. default:
  1212. i.types[this_operand] |= Mem32;
  1213. }
  1214. /* Check for base index form. We detect the base index form by
  1215. looking for an ')' at the end of the operand, searching
  1216. for the '(' matching it, and finding a REGISTER_PREFIX or ','
  1217. after it. */
  1218. base_string = end_of_operand_string - 1;
  1219. found_base_index_form = FALSE;
  1220. if (*base_string == ')') {
  1221. uint parens_balenced = 1;
  1222. /* We've already checked that the number of left & right ()'s are equal,
  1223. so this loop will not be infinite. */
  1224. do {
  1225. base_string--;
  1226. if (*base_string == ')') parens_balenced++;
  1227. if (*base_string == '(') parens_balenced--;
  1228. } while (parens_balenced);
  1229. base_string++; /* Skip past '('. */
  1230. if (*base_string == REGISTER_PREFIX || *base_string == ',')
  1231. found_base_index_form = TRUE;
  1232. }
  1233. /* If we can't parse a base index register expression, we've found
  1234. a pure displacement expression. We set up displacement_string_start
  1235. and displacement_string_end for the code below. */
  1236. if (! found_base_index_form) {
  1237. displacement_string_start = op_string;
  1238. displacement_string_end = end_of_operand_string;
  1239. } else {
  1240. char *base_reg_name, *index_reg_name, *num_string;
  1241. int num;
  1242. i.types[this_operand] |= BaseIndex;
  1243. /* If there is a displacement set-up for it to be parsed later. */
  1244. if (base_string != op_string + 1) {
  1245. displacement_string_start = op_string;
  1246. displacement_string_end = base_string - 1;
  1247. }
  1248. /* Find base register (if any). */
  1249. if (*base_string != ',') {
  1250. base_reg_name = base_string++;
  1251. /* skip past register name & parse it */
  1252. while (isalpha(*base_string)) base_string++;
  1253. if (base_string == base_reg_name+1) {
  1254. as_warn ("can't find base register name after '(%c'",
  1255. REGISTER_PREFIX);
  1256. return 0;
  1257. }
  1258. END_STRING_AND_SAVE (base_string);
  1259. if (! (i.base_reg = parse_register (base_reg_name))) {
  1260. as_warn ("bad base register name ('%s')", base_reg_name);
  1261. return 0;
  1262. }
  1263. RESTORE_END_STRING (base_string);
  1264. }
  1265. /* Now check seperator; must be ',' ==> index reg
  1266. OR num ==> no index reg. just scale factor
  1267. OR ')' ==> end. (scale factor = 1) */
  1268. if (*base_string != ',' && *base_string != ')') {
  1269. as_warn ("expecting ',' or ')' after base register in `%s'",
  1270. operand_string);
  1271. return 0;
  1272. }
  1273. /* There may index reg here; and there may be a scale factor. */
  1274. if (*base_string == ',' && *(base_string+1) == REGISTER_PREFIX) {
  1275. index_reg_name = ++base_string;
  1276. while (isalpha(*++base_string));
  1277. END_STRING_AND_SAVE (base_string);
  1278. if (! (i.index_reg = parse_register(index_reg_name))) {
  1279. as_warn ("bad index register name ('%s')", index_reg_name);
  1280. return 0;
  1281. }
  1282. RESTORE_END_STRING (base_string);
  1283. }
  1284. /* Check for scale factor. */
  1285. if (*base_string == ',' && isdigit(*(base_string+1))) {
  1286. num_string = ++base_string;
  1287. while (is_digit_char(*base_string)) base_string++;
  1288. if (base_string == num_string) {
  1289. as_warn ("can't find a scale factor after ','");
  1290. return 0;
  1291. }
  1292. END_STRING_AND_SAVE (base_string);
  1293. /* We've got a scale factor. */
  1294. if (! sscanf (num_string, "%d", &num)) {
  1295. as_warn ("can't parse scale factor from '%s'", num_string);
  1296. return 0;
  1297. }
  1298. RESTORE_END_STRING (base_string);
  1299. switch (num) { /* must be 1 digit scale */
  1300. case 1: i.log2_scale_factor = 0; break;
  1301. case 2: i.log2_scale_factor = 1; break;
  1302. case 4: i.log2_scale_factor = 2; break;
  1303. case 8: i.log2_scale_factor = 3; break;
  1304. default:
  1305. as_warn ("expecting scale factor of 1, 2, 4, 8; got %d", num);
  1306. return 0;
  1307. }
  1308. } else {
  1309. if (! i.index_reg && *base_string == ',') {
  1310. as_warn ("expecting index register or scale factor after ','; got '%c'",
  1311. *(base_string+1));
  1312. return 0;
  1313. }
  1314. }
  1315. }
  1316. /* If there's an expression begining the operand, parse it,
  1317. assuming displacement_string_start and displacement_string_end
  1318. are meaningful. */
  1319. if (displacement_string_start) {
  1320. register expressionS * exp;
  1321. segT exp_seg;
  1322. char * save_input_line_pointer;
  1323. exp = &disp_expressions[i.disp_operands];
  1324. i.disps [this_operand] = exp;
  1325. i.disp_operands++;
  1326. save_input_line_pointer = input_line_pointer;
  1327. input_line_pointer = displacement_string_start;
  1328. END_STRING_AND_SAVE (displacement_string_end);
  1329. exp_seg = expression (exp);
  1330. RESTORE_END_STRING (displacement_string_end);
  1331. input_line_pointer = save_input_line_pointer;
  1332. switch (exp_seg) {
  1333. case SEG_NONE:
  1334. /* missing expr becomes absolute 0 */
  1335. as_warn ("missing or invalid displacement '%s' taken as 0",
  1336. operand_string);
  1337. i.types[this_operand] |= (Disp|Abs);
  1338. exp->X_seg = SEG_ABSOLUTE;
  1339. exp->X_add_number = 0;
  1340. exp->X_add_symbol = (symbolS *) 0;
  1341. exp->X_subtract_symbol = (symbolS *) 0;
  1342. break;
  1343. case SEG_ABSOLUTE:
  1344. i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
  1345. break;
  1346. case SEG_TEXT: case SEG_DATA: case SEG_BSS:
  1347. case SEG_UNKNOWN: /* must be 32 bit displacement (i.e. address) */
  1348. i.types[this_operand] |= Disp32;
  1349. break;
  1350. default:
  1351. goto seg_unimplemented;
  1352. }
  1353. }
  1354. /* Make sure the memory operand we've been dealt is valid. */
  1355. if (i.base_reg && i.index_reg &&
  1356. ! (i.base_reg->reg_type & i.index_reg->reg_type & Reg)) {
  1357. as_warn ("register size mismatch in (base,index,scale) expression");
  1358. return 0;
  1359. }
  1360. if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
  1361. (i.index_reg && (i.index_reg->reg_type & Reg32) == 0)) {
  1362. as_warn ("base/index register must be 32 bit register");
  1363. return 0;
  1364. }
  1365. if (i.index_reg && i.index_reg == esp) {
  1366. as_warn ("%s may not be used as an index register", esp->reg_name);
  1367. return 0;
  1368. }
  1369. } else { /* it's not a memory operand; argh! */
  1370. as_warn ("invalid char %s begining %s operand '%s'",
  1371. output_invalid(*op_string), ordinal_names[this_operand],
  1372. op_string);
  1373. return 0;
  1374. }
  1375. return 1; /* normal return */
  1376. }
  1377. /*
  1378. * md_estimate_size_before_relax()
  1379. *
  1380. * Called just before relax().
  1381. * Any symbol that is now undefined will not become defined.
  1382. * Return the correct fr_subtype in the frag.
  1383. * Return the initial "guess for fr_var" to caller.
  1384. * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  1385. * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  1386. * Although it may not be explicit in the frag, pretend fr_var starts with a
  1387. * 0 value.
  1388. */
  1389. int
  1390. md_estimate_size_before_relax (fragP, segment_type)
  1391. register fragS * fragP;
  1392. register int segment_type; /* N_DATA or N_TEXT. */
  1393. {
  1394. register uchar * opcode;
  1395. register int old_fr_fix;
  1396. old_fr_fix = fragP -> fr_fix;
  1397. opcode = (uchar *) fragP -> fr_opcode;
  1398. /* We've already got fragP->fr_subtype right; all we have to do is check
  1399. for un-relaxable symbols. */
  1400. if ((fragP -> fr_symbol -> sy_type & N_TYPE) != segment_type) {
  1401. /* symbol is undefined in this segment */
  1402. switch (opcode[0]) {
  1403. case JUMP_PC_RELATIVE: /* make jmp (0xeb) a dword displacement jump */
  1404. opcode[0] = 0xe9; /* dword disp jmp */
  1405. fragP -> fr_fix += 4;
  1406. fix_new (fragP, old_fr_fix, 4,
  1407. fragP -> fr_symbol,
  1408. (symbolS *) 0,
  1409. fragP -> fr_offset, 1);
  1410. break;
  1411. default:
  1412. /* This changes the byte-displacement jump 0x7N -->
  1413. the dword-displacement jump 0x0f8N */
  1414. opcode[1] = opcode[0] + 0x10;
  1415. opcode[0] = TWO_BYTE_OPCODE_ESCAPE; /* two-byte escape */
  1416. fragP -> fr_fix += 1 + 4; /* we've added an opcode byte */
  1417. fix_new (fragP, old_fr_fix + 1, 4,
  1418. fragP -> fr_symbol,
  1419. (symbolS *) 0,
  1420. fragP -> fr_offset, 1);
  1421. break;
  1422. }
  1423. frag_wane (fragP);
  1424. }
  1425. return (fragP -> fr_var + fragP -> fr_fix - old_fr_fix);
  1426. } /* md_estimate_size_before_relax() */
  1427. /*
  1428. * md_convert_frag();
  1429. *
  1430. * Called after relax() is finished.
  1431. * In: Address of frag.
  1432. * fr_type == rs_machine_dependent.
  1433. * fr_subtype is what the address relaxed to.
  1434. *
  1435. * Out: Any fixSs and constants are set up.
  1436. * Caller will turn frag into a ".space 0".
  1437. */
  1438. void
  1439. md_convert_frag (fragP)
  1440. register fragS * fragP;
  1441. {
  1442. register uchar * opcode;
  1443. uchar * where_to_put_displacement;
  1444. uint target_address, opcode_address;
  1445. uint extension;
  1446. int displacement_from_opcode_start;
  1447. opcode = (uchar *) fragP -> fr_opcode;
  1448. /* Address we want to reach in file space. */
  1449. target_address = fragP->fr_symbol->sy_value + fragP->fr_offset;
  1450. /* Address opcode resides at in file space. */
  1451. opcode_address = fragP->fr_address + fragP->fr_fix;
  1452. /* Displacement from opcode start to fill into instruction. */
  1453. displacement_from_opcode_start = target_address - opcode_address;
  1454. switch (fragP->fr_subtype) {
  1455. case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
  1456. case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
  1457. /* don't have to change opcode */
  1458. extension = 1; /* 1 opcode + 1 displacement */
  1459. where_to_put_displacement = &opcode[1];
  1460. break;
  1461. case ENCODE_RELAX_STATE (COND_JUMP, WORD):
  1462. opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
  1463. opcode[2] = opcode[0] + 0x10;
  1464. opcode[0] = WORD_PREFIX_OPCODE;
  1465. extension = 4; /* 3 opcode + 2 displacement */
  1466. where_to_put_displacement = &opcode[3];
  1467. break;
  1468. case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
  1469. opcode[1] = 0xe9;
  1470. opcode[0] = WORD_PREFIX_OPCODE;
  1471. extension = 3; /* 2 opcode + 2 displacement */
  1472. where_to_put_displacement = &opcode[2];
  1473. break;
  1474. case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
  1475. opcode[1] = opcode[0] + 0x10;
  1476. opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
  1477. extension = 5; /* 2 opcode + 4 displacement */
  1478. where_to_put_displacement = &opcode[2];
  1479. break;
  1480. case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
  1481. opcode[0] = 0xe9;
  1482. extension = 4; /* 1 opcode + 4 displacement */
  1483. where_to_put_displacement = &opcode[1];
  1484. break;
  1485. default:
  1486. BAD_CASE(fragP -> fr_subtype);
  1487. break;
  1488. }
  1489. /* now put displacement after opcode */
  1490. md_number_to_chars (where_to_put_displacement,
  1491. displacement_from_opcode_start - extension,
  1492. SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
  1493. fragP -> fr_fix += extension;
  1494. }
  1495. int md_short_jump_size = 2; /* size of byte displacement jmp */
  1496. int md_long_jump_size = 5; /* size of dword displacement jmp */
  1497. void md_create_short_jump(ptr, from_addr, to_addr)
  1498. char *ptr;
  1499. long from_addr, to_addr;
  1500. {
  1501. long offset;
  1502. offset = to_addr - (from_addr + 2);
  1503. md_number_to_chars (ptr, (long) 0xeb, 1); /* opcode for byte-disp jump */
  1504. md_number_to_chars (ptr + 1, offset, 1);
  1505. }
  1506. void md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1507. char *ptr;
  1508. long from_addr, to_addr;
  1509. fragS *frag;
  1510. symbolS *to_symbol;
  1511. {
  1512. long offset;
  1513. if (flagseen['m']) {
  1514. offset = to_addr - to_symbol->sy_value;
  1515. md_number_to_chars (ptr, 0xe9, 1); /* opcode for long jmp */
  1516. md_number_to_chars (ptr + 1, offset, 4);
  1517. fix_new (frag, (ptr+1) - frag->fr_literal, 4,
  1518. to_symbol, (symbolS *) 0, (long int) 0, 0);
  1519. } else {
  1520. offset = to_addr - (from_addr + 5);
  1521. md_number_to_chars(ptr, (long) 0xe9, 1);
  1522. md_number_to_chars(ptr + 1, offset, 4);
  1523. }
  1524. }
  1525. int
  1526. md_parse_option(argP,cntP,vecP)
  1527. char **argP;
  1528. int *cntP;
  1529. char ***vecP;
  1530. {
  1531. return 1;
  1532. }
  1533. void /* Knows about order of bytes in address. */
  1534. md_number_to_chars (con, value, nbytes)
  1535. char con []; /* Return 'nbytes' of chars here. */
  1536. long int value; /* The value of the bits. */
  1537. int nbytes; /* Number of bytes in the output. */
  1538. {
  1539. register char * p = con;
  1540. switch (nbytes) {
  1541. case 1:
  1542. p[0] = value & 0xff;
  1543. break;
  1544. case 2:
  1545. p[0] = value & 0xff;
  1546. p[1] = (value >> 8) & 0xff;
  1547. break;
  1548. case 4:
  1549. p[0] = value & 0xff;
  1550. p[1] = (value>>8) & 0xff;
  1551. p[2] = (value>>16) & 0xff;
  1552. p[3] = (value>>24) & 0xff;
  1553. break;
  1554. default:
  1555. BAD_CASE (nbytes);
  1556. }
  1557. }
  1558. void /* Knows about order of bytes in address. */
  1559. md_number_to_disp (con, value, nbytes)
  1560. char con []; /* Return 'nbytes' of chars here. */
  1561. long int value; /* The value of the bits. */
  1562. int nbytes; /* Number of bytes in the output. */
  1563. {
  1564. char * alloca ();
  1565. char * answer = alloca (nbytes);
  1566. register char * p = answer;
  1567. switch (nbytes) {
  1568. case 1:
  1569. *p = value;
  1570. break;
  1571. case 2:
  1572. *p++ = value;
  1573. *p = (value>>8);
  1574. break;
  1575. case 4:
  1576. *p++ = value;
  1577. *p++ = (value>>8);
  1578. *p++ = (value>>16);
  1579. *p = (value>>24);
  1580. break;
  1581. default:
  1582. BAD_CASE (nbytes);
  1583. }
  1584. bcopy (answer, con, nbytes);
  1585. }
  1586. void /* Knows about order of bytes in address. */
  1587. md_number_to_imm (con, value, nbytes)
  1588. char con []; /* Return 'nbytes' of chars here. */
  1589. long int value; /* The value of the bits. */
  1590. int nbytes; /* Number of bytes in the output. */
  1591. {
  1592. char * alloca ();
  1593. char * answer = alloca (nbytes);
  1594. register char * p = answer;
  1595. switch (nbytes) {
  1596. case 1:
  1597. *p = value;
  1598. break;
  1599. case 2:
  1600. *p++ = value;
  1601. *p = (value>>8);
  1602. break;
  1603. case 4:
  1604. *p++ = value;
  1605. *p++ = (value>>8);
  1606. *p++ = (value>>16);
  1607. *p = (value>>24);
  1608. break;
  1609. default:
  1610. BAD_CASE (nbytes);
  1611. }
  1612. bcopy (answer, con, nbytes);
  1613. }
  1614. void /* Knows about order of bytes in address. */
  1615. md_number_to_field (con, value, nbytes)
  1616. char con []; /* Return 'nbytes' of chars here. */
  1617. long int value; /* The value of the bits. */
  1618. int nbytes; /* Number of bytes in the output. */
  1619. {
  1620. char * alloca ();
  1621. char * answer = alloca (nbytes);
  1622. register char * p = answer;
  1623. switch (nbytes) {
  1624. case 1:
  1625. *p = value;
  1626. break;
  1627. case 2:
  1628. *p++ = value;
  1629. *p = (value>>8);
  1630. break;
  1631. case 4:
  1632. *p++ = value;
  1633. *p++ = (value>>8);
  1634. *p++ = (value>>16);
  1635. *p = (value>>24);
  1636. break;
  1637. default:
  1638. BAD_CASE (nbytes);
  1639. }
  1640. bcopy (answer, con, nbytes);
  1641. }
  1642. long int /* Knows about the byte order in a word. */
  1643. md_chars_to_number (con, nbytes)
  1644. unsigned char con[]; /* Low order byte 1st. */
  1645. int nbytes; /* Number of bytes in the input. */
  1646. {
  1647. long int retval;
  1648. for (retval=0, con+=nbytes-1; nbytes--; con--)
  1649. {
  1650. retval <<= BITS_PER_CHAR;
  1651. retval |= *con;
  1652. }
  1653. return retval;
  1654. }
  1655. void md_ri_to_chars(ri_p, ri)
  1656. struct relocation_info *ri_p, ri;
  1657. {
  1658. unsigned char the_bytes[8];
  1659. /* this is easy */
  1660. md_number_to_chars(the_bytes, ri.r_address, sizeof(ri.r_address));
  1661. /* now the fun stuff */
  1662. the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
  1663. the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
  1664. the_bytes[4] = ri.r_symbolnum & 0x0ff;
  1665. the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06) |
  1666. ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
  1667. /* now put it back where you found it */
  1668. bcopy (the_bytes, (char *)ri_p, sizeof(struct relocation_info));
  1669. }
  1670. #define MAX_LITTLENUMS 6
  1671. /* Turn the string pointed to by litP into a floating point constant of type
  1672. type, and emit the appropriate bytes. The number of LITTLENUMS emitted
  1673. is stored in *sizeP . An error message is returned, or NULL on OK.
  1674. */
  1675. char *
  1676. md_atof(type,litP,sizeP)
  1677. char type;
  1678. char *litP;
  1679. int *sizeP;
  1680. {
  1681. int prec;
  1682. LITTLENUM_TYPE words[MAX_LITTLENUMS];
  1683. LITTLENUM_TYPE *wordP;
  1684. char *t;
  1685. char *atof_i386();
  1686. switch(type) {
  1687. case 'f':
  1688. case 'F':
  1689. prec = 2;
  1690. break;
  1691. case 'd':
  1692. case 'D':
  1693. prec = 4;
  1694. break;
  1695. case 'x':
  1696. case 'X':
  1697. prec = 5;
  1698. break;
  1699. default:
  1700. *sizeP=0;
  1701. return "Bad call to md_atof ()";
  1702. }
  1703. t = atof_i386 (input_line_pointer,type,words);
  1704. if(t)
  1705. input_line_pointer=t;
  1706. *sizeP = prec * sizeof(LITTLENUM_TYPE);
  1707. /* this loops outputs the LITTLENUMs in REVERSE order; in accord with
  1708. the bigendian 386 */
  1709. for(wordP = words + prec - 1;prec--;) {
  1710. md_number_to_chars (litP, (long) (*wordP--), sizeof(LITTLENUM_TYPE));
  1711. litP += sizeof(LITTLENUM_TYPE);
  1712. }
  1713. return ""; /* Someone should teach Dean about null pointers */
  1714. }
  1715. char output_invalid_buf[8];
  1716. char * output_invalid (c)
  1717. char c;
  1718. {
  1719. if (isprint(c)) sprintf (output_invalid_buf, "'%c'", c);
  1720. else sprintf (output_invalid_buf, "(0x%x)", c);
  1721. return output_invalid_buf;
  1722. }
  1723. reg_entry *parse_register (reg_string)
  1724. char *reg_string; /* reg_string starts *before* REGISTER_PREFIX */
  1725. {
  1726. register char *s = reg_string;
  1727. register char *p;
  1728. char reg_name_given[MAX_REG_NAME_SIZE];
  1729. s++; /* skip REGISTER_PREFIX */
  1730. for (p = reg_name_given; is_register_char (*s); p++, s++) {
  1731. *p = register_chars [*s];
  1732. if (p >= reg_name_given + MAX_REG_NAME_SIZE)
  1733. return (reg_entry *) 0;
  1734. }
  1735. *p = '\0';
  1736. return (reg_entry *) hash_find (reg_hash, reg_name_given);
  1737. }