2 * Copyright (C) 2006-2007 Michael Buesch <mb@bu3sch.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
26 extern int yyparse(void);
30 const char *infile_name;
31 const char *outfile_name;
41 unsigned int operand; /* For NORMAL */
42 struct label *label; /* For LABELREF */
52 /* Set to true, if this is a jump instruction.
53 * This is only used when assembling RET to check
54 * whether the previous instruction was a jump or not. */
58 struct out_operand operands[3];
60 /* The absolute address of this instruction.
61 * Only used in resolve_labels(). */
64 const char *labelname; /* only for OUT_LABEL */
65 /* Set to 1, if this is the %start instruction. */
68 struct list_head list;
71 struct assembler_context {
72 /* The architecture version (802.11 core revision) */
75 struct label *start_label;
78 struct statement *cur_stmt;
80 struct list_head output;
84 #define for_each_statement(ctx, s) \
85 list_for_each_entry(s, &infile.sl, list) { \
88 #define for_each_statement_end(ctx, s) \
89 } do { ctx->cur_stmt = NULL; } while (0)
91 #define _msg_helper(type, stmt, msg, x...) do { \
92 fprintf(stderr, "Assembler " type); \
94 fprintf(stderr, " (file \"%s\", line %u)", \
98 fprintf(stderr, ":\n " msg "\n" ,##x); \
101 #define asm_error(ctx, msg, x...) do { \
102 _msg_helper("ERROR", (ctx)->cur_stmt, msg ,##x); \
106 #define asm_warn(ctx, msg, x...) \
107 _msg_helper("warning", (ctx)->cur_stmt, msg ,##x)
109 #define asm_info(ctx, msg, x...) \
110 _msg_helper("info", (ctx)->cur_stmt, msg ,##x)
113 static void eval_directives(struct assembler_context *ctx)
118 int have_start_label = 0;
121 for_each_statement(ctx, s) {
122 if (s->type == STMT_ASMDIR) {
127 asm_error(ctx, "Multiple %%arch definitions");
128 ctx->arch = ad->u.arch;
129 if (ctx->arch != 5 && ctx->arch != 15) {
130 asm_error(ctx, "Architecture version %u unsupported",
136 if (have_start_label)
137 asm_error(ctx, "Multiple %%start definitions");
138 ctx->start_label = ad->u.start;
139 have_start_label = 1;
142 asm_error(ctx, "Unknown ASM directive");
145 } for_each_statement_end(ctx, s);
148 asm_error(ctx, "No %%arch defined");
149 if (!have_start_label)
150 asm_info(ctx, "Using start address 0");
153 static bool is_possible_imm(unsigned int imm)
157 /* Immediates are only possible up to 16bit (wordsize). */
160 if (imm & (1 << 15)) {
161 if ((imm & mask) != mask &&
165 if ((imm & mask) != 0)
172 static bool is_valid_imm(struct assembler_context *ctx,
176 unsigned int immediate_size;
178 /* This function checks if the immediate value is representable
179 * as a native immediate operand.
181 * For v5 architecture the immediate can be 10bit long.
182 * For v15 architecture the immediate can be 11bit long.
184 * The value is sign-extended, so we allow values
185 * of 0xFFFA, for example.
188 if (!is_possible_imm(imm))
192 if (ctx->arch == 5) {
193 immediate_size = 10; /* 10bit */
194 } else if (ctx->arch == 15) {
195 immediate_size = 11; /* 11bit */
197 asm_error(ctx, "Unknown immediate size for arch %u",
201 /* First create a mask with all possible bits for
202 * an immediate value unset. */
203 mask = (~0 << immediate_size) & 0xFFFF;
204 /* Is the sign bit of the immediate set? */
205 if (imm & (1 << (immediate_size - 1))) {
206 /* Yes, so all bits above that must also
207 * be set, otherwise we can't represent this
208 * value in an operand. */
209 if ((imm & mask) != mask)
212 /* All bits above the immediate's size must
221 /* This checks if the value is nonzero and a power of two. */
222 static bool is_power_of_two(unsigned int value)
224 return (value && ((value & (value - 1)) == 0));
227 /* This checks if all bits set in the mask are contiguous.
228 * Zero is also considered a contiguous mask. */
229 static bool is_contiguous_bitmask(unsigned int mask)
231 unsigned int low_zeros_mask;
236 /* Turn the lowest zeros of the mask into a bitmask.
237 * Example: 0b00011000 -> 0b00000111 */
238 low_zeros_mask = (mask - 1) & ~mask;
239 /* Adding the low_zeros_mask to the original mask
240 * basically is a bitwise OR operation.
241 * If the original mask was contiguous, we end up with a
242 * contiguous bitmask from bit 0 to the highest bit
243 * set in the original mask. Adding 1 will result in a single
244 * bit set, which is a power of two. */
245 is_contiguous = is_power_of_two(mask + low_zeros_mask + 1);
247 return is_contiguous;
250 static unsigned int generate_imm_operand(struct assembler_context *ctx,
251 const struct immediate *imm)
253 unsigned int val, tmp;
256 /* format: 0b11ii iiii iiii */
263 if (!is_valid_imm(ctx, tmp)) {
264 asm_warn(ctx, "IMMEDIATE 0x%X (%d) too long "
265 "(> 9 bits + sign). Did you intend to "
266 "use implicit sign extension?",
279 static unsigned int generate_reg_operand(struct assembler_context *ctx,
280 const struct registr *reg)
282 unsigned int val = 0;
286 /* format: 0b1011 11rr rrrr */
290 if (reg->nr & ~0x3F) //FIXME 128 regs for v15 arch possible?
291 asm_error(ctx, "GPR-nr too big");
295 /* format: 0b100. .... .... */
299 if (reg->nr & ~0x1FF)
300 asm_error(ctx, "SPR-nr too big");
304 /* format: 0b1000 0110 0rrr */
309 asm_error(ctx, "OFFR-nr too big");
313 asm_error(ctx, "generate_reg_operand() regtype");
319 static unsigned int generate_mem_operand(struct assembler_context *ctx,
320 const struct memory *mem)
322 unsigned int val = 0, off, reg;
326 /* format: 0b0mmm mmmm mmmm */
328 if (off & ~0x7FF) { //FIXME 4096 words for v15 arch possible?
329 asm_warn(ctx, "DIRECT memoffset 0x%X too long (> 11 bits)", off);
335 /* format: 0b101r rroo oooo */
339 //FIXME what about v15 arch?
341 asm_warn(ctx, "INDIRECT memoffset 0x%X too long (> 6 bits)", off);
345 asm_error(ctx, "OFFR-nr too big");
350 asm_error(ctx, "generate_mem_operand() memtype");
356 static void generate_operand(struct assembler_context *ctx,
357 const struct operand *oper,
358 struct out_operand *out)
360 out->type = OUTOPER_NORMAL;
362 switch (oper->type) {
364 out->u.operand = generate_imm_operand(ctx, oper->u.imm);
367 out->u.operand = generate_reg_operand(ctx, oper->u.reg);
370 out->u.operand = generate_mem_operand(ctx, oper->u.mem);
373 out->type = OUTOPER_LABELREF;
374 out->u.label = oper->u.label;
377 out->u.operand = oper->u.addr->addr;
380 out->u.operand = oper->u.raw;
383 asm_error(ctx, "generate_operand() operstate");
387 static struct code_output * do_assemble_insn(struct assembler_context *ctx,
388 struct instruction *insn,
395 struct code_output *out;
396 struct label *labelref = NULL;
397 struct operand *oper;
398 int have_spr_operand = 0;
399 int have_mem_operand = 0;
401 out = xmalloc(sizeof(*out));
402 INIT_LIST_HEAD(&out->list);
403 out->opcode = opcode;
406 if (ARRAY_SIZE(out->operands) > ARRAY_SIZE(ol->oper))
407 asm_error(ctx, "Internal operand array confusion");
409 for (i = 0; i < ARRAY_SIZE(out->operands); i++) {
414 /* If this is an INPUT operand (first or second), we must
415 * make sure that not both are accessing SPR or MEMORY.
416 * The device only supports one SPR or MEMORY operand in
417 * the input operands. */
418 if ((i == 0) || (i == 1)) {
419 if ((oper->type == OPER_REG) &&
420 (oper->u.reg->type == SPR)) {
421 if (have_spr_operand)
422 asm_error(ctx, "Multiple SPR input operands in one instruction");
423 have_spr_operand = 1;
425 if (oper->type == OPER_MEM) {
426 if (have_mem_operand)
427 asm_error(ctx, "Multiple MEMORY input operands in on instruction");
428 have_mem_operand = 1;
432 generate_operand(ctx, oper, &out->operands[i]);
436 asm_error(ctx, "Internal error: nr_oper at "
437 "lowlevel do_assemble_insn");
439 list_add_tail(&out->list, &ctx->output);
444 static unsigned int merge_ext_into_opcode(struct assembler_context *ctx,
446 struct instruction *insn)
450 unsigned int mask, shift;
454 mask = ol->oper[0]->u.raw;
456 asm_error(ctx, "opcode MASK extension too big (> 0xF)");
457 shift = ol->oper[1]->u.raw;
459 asm_error(ctx, "opcode SHIFT extension too big (> 0xF)");
460 opcode |= (mask << 4);
462 ol->oper[0] = ol->oper[2];
463 ol->oper[1] = ol->oper[3];
464 ol->oper[2] = ol->oper[4];
469 static unsigned int merge_external_jmp_into_opcode(struct assembler_context *ctx,
471 struct instruction *insn)
479 cond = ol->oper[0]->u.imm->imm;
481 asm_error(ctx, "External jump condition value too big (> 0xFF)");
483 ol->oper[0] = ol->oper[1];
484 ol->oper[1] = ol->oper[2];
485 ol->oper[2] = ol->oper[3];
490 static void assemble_instruction(struct assembler_context *ctx,
491 struct instruction *insn);
493 static void emulate_mov_insn(struct assembler_context *ctx,
494 struct instruction *insn)
496 struct instruction em_insn;
497 struct operlist em_ol;
498 struct operand em_op_shift;
499 struct operand em_op_mask;
500 struct operand em_op_x;
501 struct operand em_op_y;
502 struct immediate em_imm_x;
503 struct immediate em_imm_y;
505 struct operand *in, *out;
508 /* This is a pseudo-OP. We emulate it by OR or ORX */
510 in = insn->operands->oper[0];
511 out = insn->operands->oper[1];
516 em_op_x.type = OPER_IMM;
517 em_op_x.u.imm = &em_imm_x;
518 em_ol.oper[1] = &em_op_x;
521 if (in->type == OPER_IMM) {
522 tmp = in->u.imm->imm;
523 if (!is_possible_imm(tmp))
524 asm_error(ctx, "MOV operand 0x%X > 16bit", tmp);
525 if (!is_valid_imm(ctx, tmp)) {
526 /* Immediate too big for plain OR */
529 em_op_mask.type = OPER_RAW;
530 em_op_mask.u.raw = 0x7;
531 em_op_shift.type = OPER_RAW;
532 em_op_shift.u.raw = 0x8;
534 em_imm_x.imm = (tmp & 0xFF00) >> 8;
535 em_op_x.type = OPER_IMM;
536 em_op_x.u.imm = &em_imm_x;
538 em_imm_y.imm = (tmp & 0x00FF);
539 em_op_y.type = OPER_IMM;
540 em_op_y.u.imm = &em_imm_y;
542 em_ol.oper[0] = &em_op_mask;
543 em_ol.oper[1] = &em_op_shift;
544 em_ol.oper[2] = &em_op_x;
545 em_ol.oper[3] = &em_op_y;
550 em_insn.operands = &em_ol;
551 assemble_instruction(ctx, &em_insn); /* recurse */
554 static void emulate_jmp_insn(struct assembler_context *ctx,
555 struct instruction *insn)
557 struct instruction em_insn;
558 struct operlist em_ol;
559 struct operand em_op;
560 struct immediate em_imm;
562 /* This is a pseudo-OP. We emulate it by JE */
566 em_op.type = OPER_IMM;
567 em_op.u.imm = &em_imm;
568 em_ol.oper[0] = &em_op;
569 em_ol.oper[1] = &em_op;
570 em_ol.oper[2] = insn->operands->oper[0];
571 em_insn.operands = &em_ol;
572 assemble_instruction(ctx, &em_insn); /* recurse */
575 static void emulate_jand_insn(struct assembler_context *ctx,
576 struct instruction *insn,
579 struct code_output *out;
580 struct instruction em_insn;
581 struct operlist em_ol;
582 struct operand em_op_shift;
583 struct operand em_op_mask;
584 struct operand em_op_y;
585 struct immediate em_imm;
587 struct operand *oper0, *oper1, *oper2;
588 struct operand *imm_oper = NULL;
590 int first_bit, last_bit;
592 oper0 = insn->operands->oper[0];
593 oper1 = insn->operands->oper[1];
594 oper2 = insn->operands->oper[2];
596 if (oper0->type == OPER_IMM)
598 if (oper1->type == OPER_IMM)
600 if (oper0->type == OPER_IMM && oper1->type == OPER_IMM)
604 /* We have a single immediate operand.
605 * Check if it's representable by a normal JAND insn.
607 tmp = imm_oper->u.imm->imm;
608 if (!is_valid_imm(ctx, tmp)) {
609 /* Nope, this must be emulated by JZX/JNZX */
610 if (!is_contiguous_bitmask(tmp)) {
611 asm_error(ctx, "Long bitmask 0x%X is not contiguous",
615 first_bit = ffs(tmp);
616 last_bit = ffs(~(tmp >> (first_bit - 1))) - 1 + first_bit - 1;
621 em_insn.op = OP_JNZX;
622 em_op_shift.type = OPER_RAW;
623 em_op_shift.u.raw = first_bit - 1;
624 em_op_mask.type = OPER_RAW;
625 em_op_mask.u.raw = last_bit - first_bit;
628 em_op_y.type = OPER_IMM;
629 em_op_y.u.imm = &em_imm;
631 em_ol.oper[0] = &em_op_mask;
632 em_ol.oper[1] = &em_op_shift;
633 if (oper0->type != OPER_IMM)
634 em_ol.oper[2] = oper0;
636 em_ol.oper[2] = oper1;
637 em_ol.oper[3] = &em_op_y;
638 em_ol.oper[4] = oper2;
640 em_insn.operands = &em_ol;
642 assemble_instruction(ctx, &em_insn); /* recurse */
647 /* Do a normal JAND/JNAND instruction */
649 out = do_assemble_insn(ctx, insn, 0x040 | 0x1);
651 out = do_assemble_insn(ctx, insn, 0x040);
652 out->is_jump_insn = 1;
655 static void assemble_instruction(struct assembler_context *ctx,
656 struct instruction *insn)
658 struct code_output *out;
663 do_assemble_insn(ctx, insn, 0x1C0);
666 do_assemble_insn(ctx, insn, 0x1C2);
669 do_assemble_insn(ctx, insn, 0x1C1);
672 do_assemble_insn(ctx, insn, 0x1C3);
675 do_assemble_insn(ctx, insn, 0x1D0);
678 do_assemble_insn(ctx, insn, 0x1D2);
681 do_assemble_insn(ctx, insn, 0x1D1);
684 do_assemble_insn(ctx, insn, 0x1D3);
687 do_assemble_insn(ctx, insn, 0x130);
690 do_assemble_insn(ctx, insn, 0x160);
693 do_assemble_insn(ctx, insn, 0x140);
696 do_assemble_insn(ctx, insn, 0x170);
699 do_assemble_insn(ctx, insn, 0x120);
702 opcode = merge_ext_into_opcode(ctx, 0x200, insn);
703 do_assemble_insn(ctx, insn, opcode);
706 do_assemble_insn(ctx, insn, 0x110);
709 do_assemble_insn(ctx, insn, 0x1A0);
712 do_assemble_insn(ctx, insn, 0x1B0);
715 do_assemble_insn(ctx, insn, 0x150);
718 opcode = merge_ext_into_opcode(ctx, 0x300, insn);
719 do_assemble_insn(ctx, insn, opcode);
722 emulate_mov_insn(ctx, insn);
725 emulate_jmp_insn(ctx, insn);
728 emulate_jand_insn(ctx, insn, 0);
731 emulate_jand_insn(ctx, insn, 1);
734 out = do_assemble_insn(ctx, insn, 0x050);
735 out->is_jump_insn = 1;
738 out = do_assemble_insn(ctx, insn, 0x050 | 0x1);
739 out->is_jump_insn = 1;
742 out = do_assemble_insn(ctx, insn, 0x0D0);
743 out->is_jump_insn = 1;
746 out = do_assemble_insn(ctx, insn, 0x0D0 | 0x1);
747 out->is_jump_insn = 1;
750 out = do_assemble_insn(ctx, insn, 0x0D2);
751 out->is_jump_insn = 1;
754 out = do_assemble_insn(ctx, insn, 0x0D2 | 0x1);
755 out->is_jump_insn = 1;
758 out = do_assemble_insn(ctx, insn, 0x0D4);
759 out->is_jump_insn = 1;
762 out = do_assemble_insn(ctx, insn, 0x0D4 | 0x1);
763 out->is_jump_insn = 1;
766 out = do_assemble_insn(ctx, insn, 0x0DA);
767 out->is_jump_insn = 1;
770 out = do_assemble_insn(ctx, insn, 0x0DA | 0x1);
771 out->is_jump_insn = 1;
774 out = do_assemble_insn(ctx, insn, 0x0DC);
777 out = do_assemble_insn(ctx, insn, 0x0DC | 0x1);
778 out->is_jump_insn = 1;
781 opcode = merge_ext_into_opcode(ctx, 0x400, insn);
782 out = do_assemble_insn(ctx, insn, opcode);
783 out->is_jump_insn = 1;
786 opcode = merge_ext_into_opcode(ctx, 0x500, insn);
787 out = do_assemble_insn(ctx, insn, opcode);
788 out->is_jump_insn = 1;
791 opcode = merge_external_jmp_into_opcode(ctx, 0x700, insn);
792 out = do_assemble_insn(ctx, insn, opcode);
793 out->is_jump_insn = 1;
796 opcode = merge_external_jmp_into_opcode(ctx, 0x600, insn);
797 out = do_assemble_insn(ctx, insn, opcode);
798 out->is_jump_insn = 1;
801 do_assemble_insn(ctx, insn, 0x002);
804 if (!list_empty(&ctx->output)) {
805 /* Get the previous instruction and check whether it
806 * is a jump instruction. */
807 out = list_entry(ctx->output.prev, struct code_output, list);
808 if (out->is_jump_insn) {
809 asm_error(ctx, "RET instruction directly after "
810 "jump instruction. The hardware won't like this.");
813 do_assemble_insn(ctx, insn, 0x003);
819 do_assemble_insn(ctx, insn, 0x1E0);
822 do_assemble_insn(ctx, insn, 0x001);
825 do_assemble_insn(ctx, insn, insn->opcode);
828 asm_error(ctx, "Unknown op");
832 static void assemble_instructions(struct assembler_context *ctx)
835 struct instruction *insn;
836 struct code_output *out;
838 if (ctx->start_label) {
839 /* Generate a jump instruction at offset 0 to
840 * jump to the code start.
842 struct instruction sjmp;
846 oper.type = OPER_LABEL;
847 oper.u.label = ctx->start_label;
852 assemble_instruction(ctx, &sjmp);
853 out = list_entry(ctx->output.next, struct code_output, list);
854 out->is_start_insn = 1;
857 for_each_statement(ctx, s) {
862 assemble_instruction(ctx, insn);
865 out = xmalloc(sizeof(*out));
866 INIT_LIST_HEAD(&out->list);
867 out->type = OUT_LABEL;
868 out->labelname = s->u.label->name;
870 list_add_tail(&out->list, &ctx->output);
875 } for_each_statement_end(ctx, s);
878 /* Resolve a label reference to the address it points to. */
879 static int get_labeladdress(struct assembler_context *ctx,
880 struct code_output *this_insn,
881 struct label *labelref)
883 struct code_output *c;
887 switch (labelref->direction) {
888 case LABELREF_ABSOLUTE:
889 list_for_each_entry(c, &ctx->output, list) {
890 if (c->type != OUT_LABEL)
892 if (strcmp(c->labelname, labelref->name) != 0)
895 asm_error(ctx, "Ambiguous label reference \"%s\"",
899 address = c->address;
902 case LABELREF_RELATIVE_BACK:
903 for (c = list_entry(this_insn->list.prev, typeof(*c), list);
904 &c->list != &ctx->output;
905 c = list_entry(c->list.prev, typeof(*c), list)) {
906 if (c->type != OUT_LABEL)
908 if (strcmp(c->labelname, labelref->name) == 0) {
910 address = c->address;
915 case LABELREF_RELATIVE_FORWARD:
916 for (c = list_entry(this_insn->list.next, typeof(*c), list);
917 &c->list != &ctx->output;
918 c = list_entry(c->list.next, typeof(*c), list)) {
919 if (c->type != OUT_LABEL)
921 if (strcmp(c->labelname, labelref->name) == 0) {
923 address = c->address;
933 static void resolve_labels(struct assembler_context *ctx)
935 struct code_output *c;
938 unsigned int current_address;
940 /* Calculate the absolute addresses for each instruction. */
941 recalculate_addresses:
943 list_for_each_entry(c, &ctx->output, list) {
946 c->address = current_address;
950 c->address = current_address;
955 /* Resolve the symbolic label references. */
956 list_for_each_entry(c, &ctx->output, list) {
959 if (c->is_start_insn) {
960 /* If the first %start-jump jumps to 001, we can
961 * optimize it away, as it's unneeded.
964 if (c->operands[i].type != OUTOPER_LABELREF)
965 asm_error(ctx, "Internal error, %%start insn oper 2 not labelref");
966 if (c->operands[i].u.label->direction != LABELREF_ABSOLUTE)
967 asm_error(ctx, "%%start label reference not absolute");
968 addr = get_labeladdress(ctx, c, c->operands[i].u.label);
972 list_del(&c->list); /* Kill it */
973 goto recalculate_addresses;
977 for (i = 0; i < ARRAY_SIZE(c->operands); i++) {
978 if (c->operands[i].type != OUTOPER_LABELREF)
980 addr = get_labeladdress(ctx, c, c->operands[i].u.label);
983 c->operands[i].u.operand = addr;
985 /* Is not a jump target.
986 * Make it be an immediate */
988 c->operands[i].u.operand |= 0xC00;
989 else if (ctx->arch == 15)
990 c->operands[i].u.operand |= 0xC00 << 1;
992 asm_error(ctx, "Internal error: label res imm");
1003 asm_error(ctx, "Label \"%s\" does not exist",
1004 c->operands[i].u.label->name);
1007 static void emit_code(struct assembler_context *ctx)
1012 struct code_output *c;
1014 unsigned char outbuf[8];
1015 unsigned int insn_count = 0;
1016 struct fw_header hdr;
1018 fn_len = strlen(outfile_name) + 20;
1019 fn = xmalloc(fn_len);
1020 snprintf(fn, fn_len, "%s.ucode", outfile_name);
1021 fd = fopen(fn, "w+");
1023 fprintf(stderr, "Could not open microcode output file \"%s\"\n", fn);
1027 if (IS_VERBOSE_DEBUG)
1028 fprintf(stderr, "\nCode:\n");
1030 list_for_each_entry(c, &ctx->output, list) {
1040 memset(&hdr, 0, sizeof(hdr));
1041 hdr.type = FW_TYPE_UCODE;
1042 hdr.ver = FW_HDR_VER;
1043 hdr.size = cpu_to_be32(8 * insn_count);
1044 if (fwrite(&hdr, sizeof(hdr), 1, fd) != 1) {
1045 fprintf(stderr, "Could not write microcode outfile\n");
1049 if (insn_count > NUM_INSN_LIMIT)
1050 asm_warn(ctx, "Generating more than %d instructions. This "
1051 "will overflow the device microcode memory.",
1054 list_for_each_entry(c, &ctx->output, list) {
1057 if (IS_VERBOSE_DEBUG) {
1058 fprintf(stderr, "%03X %03X,%03X,%03X\n",
1060 c->operands[0].u.operand,
1061 c->operands[1].u.operand,
1062 c->operands[2].u.operand);
1066 if (ctx->arch == 5) {
1067 /* Instruction binary format is: xxyyyzzz0000oooX
1070 * Xxx is the first operand
1071 * yyy is the second operand
1072 * zzz is the third operand
1074 code |= ((uint64_t)c->operands[2].u.operand);
1075 code |= ((uint64_t)c->operands[1].u.operand) << 12;
1076 code |= ((uint64_t)c->operands[0].u.operand) << 24;
1077 code |= ((uint64_t)c->opcode) << 36;
1078 code = ((code & (uint64_t)0xFFFFFFFF00000000ULL) >> 32) |
1079 ((code & (uint64_t)0x00000000FFFFFFFFULL) << 32);
1080 } else if (ctx->arch == 15) {
1081 code |= ((uint64_t)c->operands[2].u.operand);
1082 code |= ((uint64_t)c->operands[1].u.operand) << 13;
1083 code |= ((uint64_t)c->operands[0].u.operand) << 26;
1084 code |= ((uint64_t)c->opcode) << 39;
1085 code = ((code & (uint64_t)0xFFFFFFFF00000000ULL) >> 32) |
1086 ((code & (uint64_t)0x00000000FFFFFFFFULL) << 32);
1088 asm_error(ctx, "No emit format for arch %u",
1091 outbuf[0] = (code & (uint64_t)0xFF00000000000000ULL) >> 56;
1092 outbuf[1] = (code & (uint64_t)0x00FF000000000000ULL) >> 48;
1093 outbuf[2] = (code & (uint64_t)0x0000FF0000000000ULL) >> 40;
1094 outbuf[3] = (code & (uint64_t)0x000000FF00000000ULL) >> 32;
1095 outbuf[4] = (code & (uint64_t)0x00000000FF000000ULL) >> 24;
1096 outbuf[5] = (code & (uint64_t)0x0000000000FF0000ULL) >> 16;
1097 outbuf[6] = (code & (uint64_t)0x000000000000FF00ULL) >> 8;
1098 outbuf[7] = (code & (uint64_t)0x00000000000000FFULL) >> 0;
1100 if (fwrite(&outbuf, ARRAY_SIZE(outbuf), 1, fd) != 1) {
1101 fprintf(stderr, "Could not write microcode outfile\n");
1113 static void assemble(void)
1115 struct assembler_context ctx;
1117 memset(&ctx, 0, sizeof(ctx));
1118 INIT_LIST_HEAD(&ctx.output);
1120 eval_directives(&ctx);
1121 assemble_instructions(&ctx);
1122 resolve_labels(&ctx);
1126 static void initialize(void)
1128 INIT_LIST_HEAD(&infile.sl);
1129 INIT_LIST_HEAD(&infile.ivals);
1131 if (IS_INSANE_DEBUG)
1135 #endif /* YYDEBUG */
1138 int main(int argc, char **argv)
1142 err = parse_args(argc, argv);
1149 err = open_input_file();
1155 assemble_initvals();
1159 /* Lazyman simply leaks all allocated memory. */