cgen.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 691 行 · 第 1/2 页
C
691 行
/* GAS interface for targets using CGEN: Cpu tools GENerator. Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.This file is part of GAS, the GNU Assembler.GAS is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GAS is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GAS; see the file COPYING. If not, write to the Free SoftwareFoundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include <setjmp.h>#include "ansidecl.h"#include "libiberty.h"#include "bfd.h"#include "symcat.h"#include "cgen-desc.h"#include "as.h"#include "subsegs.h"#include "cgen.h"#include "dwarf2dbg.h"/* Opcode table descriptor, must be set by md_begin. */CGEN_CPU_DESC gas_cgen_cpu_desc;/* Callback to insert a register into the symbol table. A target may choose to let GAS parse the registers. ??? Not currently used. */voidcgen_asm_record_register (name, number) char *name; int number;{ /* Use symbol_create here instead of symbol_new so we don't try to output registers into the object file's symbol table. */ symbol_table_insert (symbol_create (name, reg_section, number, &zero_address_frag));}/* We need to keep a list of fixups. We can't simply generate them as we go, because that would require us to first create the frag, and that would screw up references to ``.''. This is used by cpu's with simple operands. It keeps knowledge of what an `expressionS' is and what a `fixup' is out of CGEN which for the time being is preferable. OPINDEX is the index in the operand table. OPINFO is something the caller chooses to help in reloc determination. */struct fixup { int opindex; int opinfo; expressionS exp;};static struct fixup fixups[GAS_CGEN_MAX_FIXUPS];static int num_fixups;/* Prepare to parse an instruction. ??? May wish to make this static and delete calls in md_assemble. */voidgas_cgen_init_parse (){ num_fixups = 0;}/* Queue a fixup. */static voidqueue_fixup (opindex, opinfo, expP) int opindex; int opinfo; expressionS * expP;{ /* We need to generate a fixup for this expression. */ if (num_fixups >= GAS_CGEN_MAX_FIXUPS) as_fatal (_("too many fixups")); fixups[num_fixups].exp = *expP; fixups[num_fixups].opindex = opindex; fixups[num_fixups].opinfo = opinfo; ++ num_fixups;}/* The following three functions allow a backup of the fixup chain to be made, and to have this backup be swapped with the current chain. This allows certain ports, eg the m32r, to swap two instructions and swap their fixups at the same time. *//* ??? I think with cgen_asm_finish_insn (or something else) there is no more need for this. */static struct fixup saved_fixups[GAS_CGEN_MAX_FIXUPS];static int saved_num_fixups;voidgas_cgen_save_fixups (){ saved_num_fixups = num_fixups; memcpy (saved_fixups, fixups, sizeof (fixups[0]) * num_fixups); num_fixups = 0;}voidgas_cgen_restore_fixups (){ num_fixups = saved_num_fixups; memcpy (fixups, saved_fixups, sizeof (fixups[0]) * num_fixups); saved_num_fixups = 0;}voidgas_cgen_swap_fixups (){ int tmp; struct fixup tmp_fixup; if (num_fixups == 0) { gas_cgen_restore_fixups (); } else if (saved_num_fixups == 0) { gas_cgen_save_fixups (); } else { tmp = saved_num_fixups; saved_num_fixups = num_fixups; num_fixups = tmp; for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;) { tmp_fixup = saved_fixups [tmp]; saved_fixups [tmp] = fixups [tmp]; fixups [tmp] = tmp_fixup; } }}/* Default routine to record a fixup. This is a cover function to fix_new. It exists because we record INSN with the fixup. FRAG and WHERE are their respective arguments to fix_new_exp. LENGTH is in bits. OPINFO is something the caller chooses to help in reloc determination. At this point we do not use a bfd_reloc_code_real_type for operands residing in the insn, but instead just use the operand index. This lets us easily handle fixups for any operand type. We pick a BFD reloc type in md_apply_fix. */fixS *gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset) fragS * frag; int where; const CGEN_INSN * insn; int length; const CGEN_OPERAND * operand; int opinfo; symbolS * symbol; offsetT offset;{ fixS *fixP; /* It may seem strange to use operand->attrs and not insn->attrs here, but it is the operand that has a pc relative relocation. */ fixP = fix_new (frag, where, length / 8, symbol, offset, CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR), (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + (int) operand->type)); fixP->fx_cgen.insn = insn; fixP->fx_cgen.opinfo = opinfo; return fixP;}/* Default routine to record a fixup given an expression. This is a cover function to fix_new_exp. It exists because we record INSN with the fixup. FRAG and WHERE are their respective arguments to fix_new_exp. LENGTH is in bits. OPINFO is something the caller chooses to help in reloc determination. At this point we do not use a bfd_reloc_code_real_type for operands residing in the insn, but instead just use the operand index. This lets us easily handle fixups for any operand type. We pick a BFD reloc type in md_apply_fix. */fixS *gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp) fragS * frag; int where; const CGEN_INSN * insn; int length; const CGEN_OPERAND * operand; int opinfo; expressionS * exp;{ fixS *fixP; /* It may seem strange to use operand->attrs and not insn->attrs here, but it is the operand that has a pc relative relocation. */ fixP = fix_new_exp (frag, where, length / 8, exp, CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR), (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + (int) operand->type)); fixP->fx_cgen.insn = insn; fixP->fx_cgen.opinfo = opinfo; return fixP;}/* Used for communication between the next two procedures. */static jmp_buf expr_jmp_buf;static int expr_jmp_buf_p;/* Callback for cgen interface. Parse the expression at *STRP. The result is an error message or NULL for success (in which case *STRP is advanced past the parsed text). WANT is an indication of what the caller is looking for. If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match a table entry with the insn, reset the queued fixups counter. An enum cgen_parse_operand_result is stored in RESULTP. OPINDEX is the operand's table entry index. OPINFO is something the caller chooses to help in reloc determination. The resulting value is stored in VALUEP. */const char *gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP) CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; enum cgen_parse_operand_type want; const char **strP; int opindex; int opinfo; enum cgen_parse_operand_result *resultP; bfd_vma *valueP;{#ifdef __STDC__ /* These are volatile to survive the setjmp. */ char * volatile hold; enum cgen_parse_operand_result * volatile resultP_1;#else static char *hold; static enum cgen_parse_operand_result *resultP_1;#endif const char *errmsg = NULL; expressionS exp; if (want == CGEN_PARSE_OPERAND_INIT) { gas_cgen_init_parse (); return NULL; } resultP_1 = resultP; hold = input_line_pointer; input_line_pointer = (char *) *strP; /* We rely on md_operand to longjmp back to us. This is done via gas_cgen_md_operand. */ if (setjmp (expr_jmp_buf) != 0) { expr_jmp_buf_p = 0; input_line_pointer = (char *) hold; *resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR; return "illegal operand"; } expr_jmp_buf_p = 1; expression (&exp); expr_jmp_buf_p = 0; *strP = input_line_pointer; input_line_pointer = hold; /* FIXME: Need to check `want'. */ switch (exp.X_op) { case O_illegal: errmsg = _("illegal operand"); *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR; break; case O_absent: errmsg = _("missing operand"); *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR; break; case O_constant: *valueP = exp.X_add_number; *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER; break; case O_register: *valueP = exp.X_add_number; *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER; break; default: queue_fixup (opindex, opinfo, &exp); *valueP = 0; *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED; break; } return errmsg;}/* md_operand handler to catch unrecognized expressions and halt the parsing process so the next entry can be tried. ??? This could be done differently by adding code to `expression'. */voidgas_cgen_md_operand (expressionP) expressionS *expressionP ATTRIBUTE_UNUSED;{ /* Don't longjmp if we're not called from within cgen_parse_operand(). */ if (expr_jmp_buf_p) longjmp (expr_jmp_buf, 1);}/* Finish assembling instruction INSN. BUF contains what we've built up so far. LENGTH is the size of the insn in bits. RELAX_P is non-zero if relaxable insns should be emitted as such. Otherwise they're emitted in non-relaxable forms.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?