diff options
author | somov <somov@yandex-team.ru> | 2022-02-10 16:45:47 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:47 +0300 |
commit | a5950576e397b1909261050b8c7da16db58f10b1 (patch) | |
tree | 7ba7677f6a4c3e19e2cefab34d16df2c8963b4d4 /contrib/tools/yasm/modules/arch/lc3b | |
parent | 81eddc8c0b55990194e112b02d127b87d54164a9 (diff) | |
download | ydb-a5950576e397b1909261050b8c7da16db58f10b1.tar.gz |
Restoring authorship annotation for <somov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/yasm/modules/arch/lc3b')
-rw-r--r-- | contrib/tools/yasm/modules/arch/lc3b/lc3barch.c | 424 | ||||
-rw-r--r-- | contrib/tools/yasm/modules/arch/lc3b/lc3barch.h | 140 | ||||
-rw-r--r-- | contrib/tools/yasm/modules/arch/lc3b/lc3bbc.c | 498 |
3 files changed, 531 insertions, 531 deletions
diff --git a/contrib/tools/yasm/modules/arch/lc3b/lc3barch.c b/contrib/tools/yasm/modules/arch/lc3b/lc3barch.c index 051452862f..e9b1ce9dde 100644 --- a/contrib/tools/yasm/modules/arch/lc3b/lc3barch.c +++ b/contrib/tools/yasm/modules/arch/lc3b/lc3barch.c @@ -1,212 +1,212 @@ -/* - * LC-3b architecture description - * - * Copyright (C) 2003-2007 Peter Johnson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include <util.h> - -#include <libyasm.h> - -#include "lc3barch.h" - - -yasm_arch_module yasm_lc3b_LTX_arch; - - -static /*@only@*/ yasm_arch * -lc3b_create(const char *machine, const char *parser, - /*@out@*/ yasm_arch_create_error *error) -{ - yasm_arch_base *arch; - - *error = YASM_ARCH_CREATE_OK; - - if (yasm__strcasecmp(machine, "lc3b") != 0) { - *error = YASM_ARCH_CREATE_BAD_MACHINE; - return NULL; - } - - if (yasm__strcasecmp(parser, "nasm") != 0) { - *error = YASM_ARCH_CREATE_BAD_PARSER; - return NULL; - } - - arch = yasm_xmalloc(sizeof(yasm_arch_base)); - arch->module = &yasm_lc3b_LTX_arch; - return (yasm_arch *)arch; -} - -static void -lc3b_destroy(/*@only@*/ yasm_arch *arch) -{ - yasm_xfree(arch); -} - -static const char * -lc3b_get_machine(/*@unused@*/ const yasm_arch *arch) -{ - return "lc3b"; -} - -static unsigned int -lc3b_get_address_size(/*@unused@*/ const yasm_arch *arch) -{ - return 16; -} - -static int -lc3b_set_var(yasm_arch *arch, const char *var, unsigned long val) -{ - return 1; -} - -static const unsigned char ** -lc3b_get_fill(const yasm_arch *arch) -{ - /* NOP pattern is all 0's per LC-3b Assembler 3.50 output */ - static const unsigned char *fill[16] = { - NULL, /* unused */ - NULL, /* 1 - illegal; all opcodes are 2 bytes long */ - (const unsigned char *) - "\x00\x00", /* 4 */ - NULL, /* 3 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00", /* 4 */ - NULL, /* 5 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00", /* 6 */ - NULL, /* 7 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 8 */ - "\x00\x00", - NULL, /* 9 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 10 */ - "\x00\x00\x00\x00", - NULL, /* 11 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 12 */ - "\x00\x00\x00\x00\x00\x00", - NULL, /* 13 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 14 */ - "\x00\x00\x00\x00\x00\x00\x00\x00", - NULL /* 15 - illegal */ - }; - return fill; -} - -static unsigned int -lc3b_get_reg_size(/*@unused@*/ yasm_arch *arch, /*@unused@*/ uintptr_t reg) -{ - return 16; -} - -static uintptr_t -lc3b_reggroup_get_reg(/*@unused@*/ yasm_arch *arch, - /*@unused@*/ uintptr_t reggroup, - /*@unused@*/ unsigned long regindex) -{ - return 0; -} - -static void -lc3b_reg_print(/*@unused@*/ yasm_arch *arch, uintptr_t reg, FILE *f) -{ - fprintf(f, "r%u", (unsigned int)(reg&7)); -} - -static int -lc3b_floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt, - unsigned char *buf, size_t destsize, size_t valsize, - size_t shift, int warn) -{ - yasm_error_set(YASM_ERROR_FLOATING_POINT, - N_("LC-3b does not support floating point")); - return 1; -} - -static yasm_effaddr * -lc3b_ea_create_expr(yasm_arch *arch, yasm_expr *e) -{ - yasm_effaddr *ea = yasm_xmalloc(sizeof(yasm_effaddr)); - yasm_value_initialize(&ea->disp, e, 0); - ea->need_nonzero_len = 0; - ea->need_disp = 1; - ea->nosplit = 0; - ea->strong = 0; - ea->segreg = 0; - ea->pc_rel = 0; - ea->not_pc_rel = 0; - return ea; -} - -void -yasm_lc3b__ea_destroy(/*@only@*/ yasm_effaddr *ea) -{ - yasm_value_delete(&ea->disp); - yasm_xfree(ea); -} - -static void -lc3b_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level) -{ - fprintf(f, "%*sDisp:\n", indent_level, ""); - yasm_value_print(&ea->disp, f, indent_level+1); -} - -/* Define lc3b machines -- see arch.h for details */ -static yasm_arch_machine lc3b_machines[] = { - { "LC-3b", "lc3b" }, - { NULL, NULL } -}; - -/* Define arch structure -- see arch.h for details */ -yasm_arch_module yasm_lc3b_LTX_arch = { - "LC-3b", - "lc3b", - NULL, - lc3b_create, - lc3b_destroy, - lc3b_get_machine, - lc3b_get_address_size, - lc3b_set_var, - yasm_lc3b__parse_check_insnprefix, - yasm_lc3b__parse_check_regtmod, - lc3b_get_fill, - lc3b_floatnum_tobytes, - yasm_lc3b__intnum_tobytes, - lc3b_get_reg_size, - lc3b_reggroup_get_reg, - lc3b_reg_print, - NULL, /*yasm_lc3b__segreg_print*/ - lc3b_ea_create_expr, - yasm_lc3b__ea_destroy, - lc3b_ea_print, - yasm_lc3b__create_empty_insn, - lc3b_machines, - "lc3b", - 16, - 2 -}; +/* + * LC-3b architecture description + * + * Copyright (C) 2003-2007 Peter Johnson + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <util.h> + +#include <libyasm.h> + +#include "lc3barch.h" + + +yasm_arch_module yasm_lc3b_LTX_arch; + + +static /*@only@*/ yasm_arch * +lc3b_create(const char *machine, const char *parser, + /*@out@*/ yasm_arch_create_error *error) +{ + yasm_arch_base *arch; + + *error = YASM_ARCH_CREATE_OK; + + if (yasm__strcasecmp(machine, "lc3b") != 0) { + *error = YASM_ARCH_CREATE_BAD_MACHINE; + return NULL; + } + + if (yasm__strcasecmp(parser, "nasm") != 0) { + *error = YASM_ARCH_CREATE_BAD_PARSER; + return NULL; + } + + arch = yasm_xmalloc(sizeof(yasm_arch_base)); + arch->module = &yasm_lc3b_LTX_arch; + return (yasm_arch *)arch; +} + +static void +lc3b_destroy(/*@only@*/ yasm_arch *arch) +{ + yasm_xfree(arch); +} + +static const char * +lc3b_get_machine(/*@unused@*/ const yasm_arch *arch) +{ + return "lc3b"; +} + +static unsigned int +lc3b_get_address_size(/*@unused@*/ const yasm_arch *arch) +{ + return 16; +} + +static int +lc3b_set_var(yasm_arch *arch, const char *var, unsigned long val) +{ + return 1; +} + +static const unsigned char ** +lc3b_get_fill(const yasm_arch *arch) +{ + /* NOP pattern is all 0's per LC-3b Assembler 3.50 output */ + static const unsigned char *fill[16] = { + NULL, /* unused */ + NULL, /* 1 - illegal; all opcodes are 2 bytes long */ + (const unsigned char *) + "\x00\x00", /* 4 */ + NULL, /* 3 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00", /* 4 */ + NULL, /* 5 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00", /* 6 */ + NULL, /* 7 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 8 */ + "\x00\x00", + NULL, /* 9 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 10 */ + "\x00\x00\x00\x00", + NULL, /* 11 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 12 */ + "\x00\x00\x00\x00\x00\x00", + NULL, /* 13 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 14 */ + "\x00\x00\x00\x00\x00\x00\x00\x00", + NULL /* 15 - illegal */ + }; + return fill; +} + +static unsigned int +lc3b_get_reg_size(/*@unused@*/ yasm_arch *arch, /*@unused@*/ uintptr_t reg) +{ + return 16; +} + +static uintptr_t +lc3b_reggroup_get_reg(/*@unused@*/ yasm_arch *arch, + /*@unused@*/ uintptr_t reggroup, + /*@unused@*/ unsigned long regindex) +{ + return 0; +} + +static void +lc3b_reg_print(/*@unused@*/ yasm_arch *arch, uintptr_t reg, FILE *f) +{ + fprintf(f, "r%u", (unsigned int)(reg&7)); +} + +static int +lc3b_floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt, + unsigned char *buf, size_t destsize, size_t valsize, + size_t shift, int warn) +{ + yasm_error_set(YASM_ERROR_FLOATING_POINT, + N_("LC-3b does not support floating point")); + return 1; +} + +static yasm_effaddr * +lc3b_ea_create_expr(yasm_arch *arch, yasm_expr *e) +{ + yasm_effaddr *ea = yasm_xmalloc(sizeof(yasm_effaddr)); + yasm_value_initialize(&ea->disp, e, 0); + ea->need_nonzero_len = 0; + ea->need_disp = 1; + ea->nosplit = 0; + ea->strong = 0; + ea->segreg = 0; + ea->pc_rel = 0; + ea->not_pc_rel = 0; + return ea; +} + +void +yasm_lc3b__ea_destroy(/*@only@*/ yasm_effaddr *ea) +{ + yasm_value_delete(&ea->disp); + yasm_xfree(ea); +} + +static void +lc3b_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level) +{ + fprintf(f, "%*sDisp:\n", indent_level, ""); + yasm_value_print(&ea->disp, f, indent_level+1); +} + +/* Define lc3b machines -- see arch.h for details */ +static yasm_arch_machine lc3b_machines[] = { + { "LC-3b", "lc3b" }, + { NULL, NULL } +}; + +/* Define arch structure -- see arch.h for details */ +yasm_arch_module yasm_lc3b_LTX_arch = { + "LC-3b", + "lc3b", + NULL, + lc3b_create, + lc3b_destroy, + lc3b_get_machine, + lc3b_get_address_size, + lc3b_set_var, + yasm_lc3b__parse_check_insnprefix, + yasm_lc3b__parse_check_regtmod, + lc3b_get_fill, + lc3b_floatnum_tobytes, + yasm_lc3b__intnum_tobytes, + lc3b_get_reg_size, + lc3b_reggroup_get_reg, + lc3b_reg_print, + NULL, /*yasm_lc3b__segreg_print*/ + lc3b_ea_create_expr, + yasm_lc3b__ea_destroy, + lc3b_ea_print, + yasm_lc3b__create_empty_insn, + lc3b_machines, + "lc3b", + 16, + 2 +}; diff --git a/contrib/tools/yasm/modules/arch/lc3b/lc3barch.h b/contrib/tools/yasm/modules/arch/lc3b/lc3barch.h index 9ded8cbef7..745dffc335 100644 --- a/contrib/tools/yasm/modules/arch/lc3b/lc3barch.h +++ b/contrib/tools/yasm/modules/arch/lc3b/lc3barch.h @@ -1,70 +1,70 @@ -/* - * LC-3b Architecture header file - * - * Copyright (C) 2003-2007 Peter Johnson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef YASM_LC3BARCH_H -#define YASM_LC3BARCH_H - -/* Types of immediate. All immediates are stored in the LSBs of the insn. */ -typedef enum lc3b_imm_type { - LC3B_IMM_NONE = 0, /* no immediate */ - LC3B_IMM_4, /* 4-bit */ - LC3B_IMM_5, /* 5-bit */ - LC3B_IMM_6_WORD, /* 6-bit, word-multiple (byte>>1) */ - LC3B_IMM_6_BYTE, /* 6-bit, byte-multiple */ - LC3B_IMM_8, /* 8-bit, word-multiple (byte>>1) */ - LC3B_IMM_9, /* 9-bit, signed, word-multiple (byte>>1) */ - LC3B_IMM_9_PC /* 9-bit, signed, word-multiple, PC relative */ -} lc3b_imm_type; - -/* Bytecode types */ - -typedef struct lc3b_insn { - yasm_value imm; /* immediate or relative value */ - lc3b_imm_type imm_type; /* size of the immediate */ - - unsigned int opcode; /* opcode */ -} lc3b_insn; - -void yasm_lc3b__bc_transform_insn(yasm_bytecode *bc, lc3b_insn *insn); - -yasm_arch_insnprefix yasm_lc3b__parse_check_insnprefix - (yasm_arch *arch, const char *id, size_t id_len, unsigned long line, - /*@out@*/ /*@only@*/ yasm_bytecode **bc, /*@out@*/ uintptr_t *prefix); -yasm_arch_regtmod yasm_lc3b__parse_check_regtmod - (yasm_arch *arch, const char *id, size_t id_len, - /*@out@*/ uintptr_t *data); - -int yasm_lc3b__intnum_tobytes - (yasm_arch *arch, const yasm_intnum *intn, unsigned char *buf, - size_t destsize, size_t valsize, int shift, const yasm_bytecode *bc, - int warn); - -/*@only@*/ yasm_bytecode *yasm_lc3b__create_empty_insn(yasm_arch *arch, - unsigned long line); - -void yasm_lc3b__ea_destroy(/*@only@*/ yasm_effaddr *ea); - -#endif +/* + * LC-3b Architecture header file + * + * Copyright (C) 2003-2007 Peter Johnson + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef YASM_LC3BARCH_H +#define YASM_LC3BARCH_H + +/* Types of immediate. All immediates are stored in the LSBs of the insn. */ +typedef enum lc3b_imm_type { + LC3B_IMM_NONE = 0, /* no immediate */ + LC3B_IMM_4, /* 4-bit */ + LC3B_IMM_5, /* 5-bit */ + LC3B_IMM_6_WORD, /* 6-bit, word-multiple (byte>>1) */ + LC3B_IMM_6_BYTE, /* 6-bit, byte-multiple */ + LC3B_IMM_8, /* 8-bit, word-multiple (byte>>1) */ + LC3B_IMM_9, /* 9-bit, signed, word-multiple (byte>>1) */ + LC3B_IMM_9_PC /* 9-bit, signed, word-multiple, PC relative */ +} lc3b_imm_type; + +/* Bytecode types */ + +typedef struct lc3b_insn { + yasm_value imm; /* immediate or relative value */ + lc3b_imm_type imm_type; /* size of the immediate */ + + unsigned int opcode; /* opcode */ +} lc3b_insn; + +void yasm_lc3b__bc_transform_insn(yasm_bytecode *bc, lc3b_insn *insn); + +yasm_arch_insnprefix yasm_lc3b__parse_check_insnprefix + (yasm_arch *arch, const char *id, size_t id_len, unsigned long line, + /*@out@*/ /*@only@*/ yasm_bytecode **bc, /*@out@*/ uintptr_t *prefix); +yasm_arch_regtmod yasm_lc3b__parse_check_regtmod + (yasm_arch *arch, const char *id, size_t id_len, + /*@out@*/ uintptr_t *data); + +int yasm_lc3b__intnum_tobytes + (yasm_arch *arch, const yasm_intnum *intn, unsigned char *buf, + size_t destsize, size_t valsize, int shift, const yasm_bytecode *bc, + int warn); + +/*@only@*/ yasm_bytecode *yasm_lc3b__create_empty_insn(yasm_arch *arch, + unsigned long line); + +void yasm_lc3b__ea_destroy(/*@only@*/ yasm_effaddr *ea); + +#endif diff --git a/contrib/tools/yasm/modules/arch/lc3b/lc3bbc.c b/contrib/tools/yasm/modules/arch/lc3b/lc3bbc.c index d077c7c5fb..fb8a46d100 100644 --- a/contrib/tools/yasm/modules/arch/lc3b/lc3bbc.c +++ b/contrib/tools/yasm/modules/arch/lc3b/lc3bbc.c @@ -1,249 +1,249 @@ -/* - * LC-3b bytecode utility functions - * - * Copyright (C) 2003-2007 Peter Johnson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include <util.h> - -#include <libyasm.h> - -#include "lc3barch.h" - - -/* Bytecode callback function prototypes */ - -static void lc3b_bc_insn_destroy(void *contents); -static void lc3b_bc_insn_print(const void *contents, FILE *f, - int indent_level); -static int lc3b_bc_insn_calc_len(yasm_bytecode *bc, - yasm_bc_add_span_func add_span, - void *add_span_data); -static int lc3b_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); -static int lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, - unsigned char *bufstart, - void *d, yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); - -/* Bytecode callback structures */ - -static const yasm_bytecode_callback lc3b_bc_callback_insn = { - lc3b_bc_insn_destroy, - lc3b_bc_insn_print, - yasm_bc_finalize_common, - NULL, - lc3b_bc_insn_calc_len, - lc3b_bc_insn_expand, - lc3b_bc_insn_tobytes, - 0 -}; - - -void -yasm_lc3b__bc_transform_insn(yasm_bytecode *bc, lc3b_insn *insn) -{ - yasm_bc_transform(bc, &lc3b_bc_callback_insn, insn); -} - -static void -lc3b_bc_insn_destroy(void *contents) -{ - lc3b_insn *insn = (lc3b_insn *)contents; - yasm_value_delete(&insn->imm); - yasm_xfree(contents); -} - -static void -lc3b_bc_insn_print(const void *contents, FILE *f, int indent_level) -{ - const lc3b_insn *insn = (const lc3b_insn *)contents; - - fprintf(f, "%*s_Instruction_\n", indent_level, ""); - fprintf(f, "%*sImmediate Value:", indent_level, ""); - if (!insn->imm.abs) - fprintf(f, " (nil)\n"); - else { - indent_level++; - fprintf(f, "\n"); - yasm_value_print(&insn->imm, f, indent_level); - fprintf(f, "%*sType=", indent_level, ""); - switch (insn->imm_type) { - case LC3B_IMM_NONE: - fprintf(f, "NONE-SHOULDN'T HAPPEN"); - break; - case LC3B_IMM_4: - fprintf(f, "4-bit"); - break; - case LC3B_IMM_5: - fprintf(f, "5-bit"); - break; - case LC3B_IMM_6_WORD: - fprintf(f, "6-bit, word-multiple"); - break; - case LC3B_IMM_6_BYTE: - fprintf(f, "6-bit, byte-multiple"); - break; - case LC3B_IMM_8: - fprintf(f, "8-bit, word-multiple"); - break; - case LC3B_IMM_9: - fprintf(f, "9-bit, signed, word-multiple"); - break; - case LC3B_IMM_9_PC: - fprintf(f, "9-bit, signed, word-multiple, PC-relative"); - break; - } - indent_level--; - } - /* FIXME - fprintf(f, "\n%*sOrigin=", indent_level, ""); - if (insn->origin) { - fprintf(f, "\n"); - yasm_symrec_print(insn->origin, f, indent_level+1); - } else - fprintf(f, "(nil)\n"); - */ - fprintf(f, "%*sOpcode: %04x\n", indent_level, "", - (unsigned int)insn->opcode); -} - -static int -lc3b_bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) -{ - lc3b_insn *insn = (lc3b_insn *)bc->contents; - yasm_bytecode *target_prevbc; - - /* Fixed size instruction length */ - bc->len += 2; - - /* Only need to worry about out-of-range to PC-relative */ - if (insn->imm_type != LC3B_IMM_9_PC) - return 0; - - if (insn->imm.rel - && (!yasm_symrec_get_label(insn->imm.rel, &target_prevbc) - || target_prevbc->section != bc->section)) { - /* External or out of segment, so we can't check distance. */ - return 0; - } - - /* 9-bit signed, word-multiple displacement */ - add_span(add_span_data, bc, 1, &insn->imm, -512+(long)bc->len, - 511+(long)bc->len); - return 0; -} - -static int -lc3b_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) -{ - yasm_error_set(YASM_ERROR_VALUE, N_("jump target out of range")); - return -1; -} - -static int -lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, - unsigned char *bufstart, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) -{ - lc3b_insn *insn = (lc3b_insn *)bc->contents; - /*@only@*/ yasm_intnum *delta; - unsigned long buf_off = (unsigned long)(*bufp - bufstart); - - /* Output opcode */ - YASM_SAVE_16_L(*bufp, insn->opcode); - - /* Insert immediate into opcode. */ - switch (insn->imm_type) { - case LC3B_IMM_NONE: - break; - case LC3B_IMM_4: - insn->imm.size = 4; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_5: - insn->imm.size = 5; - insn->imm.sign = 1; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_6_WORD: - insn->imm.size = 6; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_6_BYTE: - insn->imm.size = 6; - insn->imm.sign = 1; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_8: - insn->imm.size = 8; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_9_PC: - /* Adjust relative displacement to end of bytecode */ - delta = yasm_intnum_create_int(-1); - if (!insn->imm.abs) - insn->imm.abs = yasm_expr_create_ident(yasm_expr_int(delta), - bc->line); - else - insn->imm.abs = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(insn->imm.abs), - yasm_expr_int(delta), bc->line); - - insn->imm.size = 9; - insn->imm.sign = 1; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - case LC3B_IMM_9: - insn->imm.size = 9; - if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) - return 1; - break; - default: - yasm_internal_error(N_("Unrecognized immediate type")); - } - - *bufp += 2; /* all instructions are 2 bytes in size */ - return 0; -} - -int -yasm_lc3b__intnum_tobytes(yasm_arch *arch, const yasm_intnum *intn, - unsigned char *buf, size_t destsize, size_t valsize, - int shift, const yasm_bytecode *bc, int warn) -{ - /* Write value out. */ - yasm_intnum_get_sized(intn, buf, destsize, valsize, shift, 0, warn); - return 0; -} +/* + * LC-3b bytecode utility functions + * + * Copyright (C) 2003-2007 Peter Johnson + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <util.h> + +#include <libyasm.h> + +#include "lc3barch.h" + + +/* Bytecode callback function prototypes */ + +static void lc3b_bc_insn_destroy(void *contents); +static void lc3b_bc_insn_print(const void *contents, FILE *f, + int indent_level); +static int lc3b_bc_insn_calc_len(yasm_bytecode *bc, + yasm_bc_add_span_func add_span, + void *add_span_data); +static int lc3b_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); +static int lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, + unsigned char *bufstart, + void *d, yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); + +/* Bytecode callback structures */ + +static const yasm_bytecode_callback lc3b_bc_callback_insn = { + lc3b_bc_insn_destroy, + lc3b_bc_insn_print, + yasm_bc_finalize_common, + NULL, + lc3b_bc_insn_calc_len, + lc3b_bc_insn_expand, + lc3b_bc_insn_tobytes, + 0 +}; + + +void +yasm_lc3b__bc_transform_insn(yasm_bytecode *bc, lc3b_insn *insn) +{ + yasm_bc_transform(bc, &lc3b_bc_callback_insn, insn); +} + +static void +lc3b_bc_insn_destroy(void *contents) +{ + lc3b_insn *insn = (lc3b_insn *)contents; + yasm_value_delete(&insn->imm); + yasm_xfree(contents); +} + +static void +lc3b_bc_insn_print(const void *contents, FILE *f, int indent_level) +{ + const lc3b_insn *insn = (const lc3b_insn *)contents; + + fprintf(f, "%*s_Instruction_\n", indent_level, ""); + fprintf(f, "%*sImmediate Value:", indent_level, ""); + if (!insn->imm.abs) + fprintf(f, " (nil)\n"); + else { + indent_level++; + fprintf(f, "\n"); + yasm_value_print(&insn->imm, f, indent_level); + fprintf(f, "%*sType=", indent_level, ""); + switch (insn->imm_type) { + case LC3B_IMM_NONE: + fprintf(f, "NONE-SHOULDN'T HAPPEN"); + break; + case LC3B_IMM_4: + fprintf(f, "4-bit"); + break; + case LC3B_IMM_5: + fprintf(f, "5-bit"); + break; + case LC3B_IMM_6_WORD: + fprintf(f, "6-bit, word-multiple"); + break; + case LC3B_IMM_6_BYTE: + fprintf(f, "6-bit, byte-multiple"); + break; + case LC3B_IMM_8: + fprintf(f, "8-bit, word-multiple"); + break; + case LC3B_IMM_9: + fprintf(f, "9-bit, signed, word-multiple"); + break; + case LC3B_IMM_9_PC: + fprintf(f, "9-bit, signed, word-multiple, PC-relative"); + break; + } + indent_level--; + } + /* FIXME + fprintf(f, "\n%*sOrigin=", indent_level, ""); + if (insn->origin) { + fprintf(f, "\n"); + yasm_symrec_print(insn->origin, f, indent_level+1); + } else + fprintf(f, "(nil)\n"); + */ + fprintf(f, "%*sOpcode: %04x\n", indent_level, "", + (unsigned int)insn->opcode); +} + +static int +lc3b_bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, + void *add_span_data) +{ + lc3b_insn *insn = (lc3b_insn *)bc->contents; + yasm_bytecode *target_prevbc; + + /* Fixed size instruction length */ + bc->len += 2; + + /* Only need to worry about out-of-range to PC-relative */ + if (insn->imm_type != LC3B_IMM_9_PC) + return 0; + + if (insn->imm.rel + && (!yasm_symrec_get_label(insn->imm.rel, &target_prevbc) + || target_prevbc->section != bc->section)) { + /* External or out of segment, so we can't check distance. */ + return 0; + } + + /* 9-bit signed, word-multiple displacement */ + add_span(add_span_data, bc, 1, &insn->imm, -512+(long)bc->len, + 511+(long)bc->len); + return 0; +} + +static int +lc3b_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, long new_val, + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) +{ + yasm_error_set(YASM_ERROR_VALUE, N_("jump target out of range")); + return -1; +} + +static int +lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, + unsigned char *bufstart, void *d, + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) +{ + lc3b_insn *insn = (lc3b_insn *)bc->contents; + /*@only@*/ yasm_intnum *delta; + unsigned long buf_off = (unsigned long)(*bufp - bufstart); + + /* Output opcode */ + YASM_SAVE_16_L(*bufp, insn->opcode); + + /* Insert immediate into opcode. */ + switch (insn->imm_type) { + case LC3B_IMM_NONE: + break; + case LC3B_IMM_4: + insn->imm.size = 4; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_5: + insn->imm.size = 5; + insn->imm.sign = 1; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_6_WORD: + insn->imm.size = 6; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_6_BYTE: + insn->imm.size = 6; + insn->imm.sign = 1; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_8: + insn->imm.size = 8; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_9_PC: + /* Adjust relative displacement to end of bytecode */ + delta = yasm_intnum_create_int(-1); + if (!insn->imm.abs) + insn->imm.abs = yasm_expr_create_ident(yasm_expr_int(delta), + bc->line); + else + insn->imm.abs = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(insn->imm.abs), + yasm_expr_int(delta), bc->line); + + insn->imm.size = 9; + insn->imm.sign = 1; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + case LC3B_IMM_9: + insn->imm.size = 9; + if (output_value(&insn->imm, *bufp, 2, buf_off, bc, 1, d)) + return 1; + break; + default: + yasm_internal_error(N_("Unrecognized immediate type")); + } + + *bufp += 2; /* all instructions are 2 bytes in size */ + return 0; +} + +int +yasm_lc3b__intnum_tobytes(yasm_arch *arch, const yasm_intnum *intn, + unsigned char *buf, size_t destsize, size_t valsize, + int shift, const yasm_bytecode *bc, int warn) +{ + /* Write value out. */ + yasm_intnum_get_sized(intn, buf, destsize, valsize, shift, 0, warn); + return 0; +} |