├── CNAME
├── LICENSE
├── Makefile
├── README.md
├── arch
├── arm
│ ├── BranchExcSys.c
│ ├── BranchExcSys.h
│ ├── DataProcessingFloatingPoint.c
│ ├── DataProcessingFloatingPoint.h
│ ├── DataProcessingImmediate.c
│ ├── DataProcessingImmediate.h
│ ├── DataProcessingRegister.c
│ ├── DataProcessingRegister.h
│ ├── LoadsAndStores.c
│ ├── LoadsAndStores.h
│ ├── adefs.h
│ ├── armadillo.c
│ ├── armadillo.h
│ ├── bits.c
│ ├── bits.h
│ ├── common.h
│ ├── instruction.c
│ ├── instruction.h
│ ├── strext.c
│ ├── strext.h
│ ├── utils.c
│ └── utils.h
├── m68k
│ ├── dis68k.c
│ └── m68k.h
├── mips
│ ├── mips-rsp.c
│ └── mips.h
├── nv
│ ├── aprintf.c
│ ├── astr.c
│ ├── colors.c
│ ├── colors.h
│ ├── core-as.c
│ ├── core-dis.c
│ ├── core.c
│ ├── ctx.c
│ ├── dis-intern.h
│ ├── dis.h
│ ├── easm.c
│ ├── easm.h
│ ├── easm_print.c
│ ├── easm_xfrm.c
│ ├── envyas.c
│ ├── envyas.h
│ ├── envydis.c
│ ├── falcon.c
│ ├── g80.c
│ ├── gf100.c
│ ├── gk110.c
│ ├── gm107.c
│ ├── hash.c
│ ├── hwsq.c
│ ├── macro.c
│ ├── mask.c
│ ├── mask.h
│ ├── nv.c
│ ├── nv.h
│ ├── nvidia.txt
│ ├── symtab.c
│ ├── symtab.h
│ ├── util.h
│ ├── var.h
│ ├── vardata.c
│ ├── varinfo.c
│ ├── vcomp.c
│ ├── vp1.c
│ ├── vuc.c
│ └── xtensa.c
├── pdp11
│ ├── pdp11.c
│ └── pdp11.h
├── ppc
│ ├── ppc_disasm.c
│ └── ppc_disasm.h
├── riscv
│ ├── riscv-disas.c
│ ├── riscv-disas.h
│ └── rv32i.c
├── sh4
│ ├── sh4.c
│ ├── sh4.h
│ ├── sh4_opcodes.h
│ ├── sh4asm_txt_emit.h
│ └── sh4dis.h
└── x86
│ ├── alloc.c
│ ├── alloc.h
│ ├── bytesex.h
│ ├── common.c
│ ├── compiler.h
│ ├── config
│ ├── config.h
│ ├── unconfig.h
│ └── unknown.h
│ ├── directiv.h
│ ├── disasm.c
│ ├── disasm.h
│ ├── disp8.c
│ ├── disp8.h
│ ├── error.c
│ ├── error.h
│ ├── hashtbl.h
│ ├── iflag.c
│ ├── iflag.h
│ ├── iflaggen.h
│ ├── ilog2.h
│ ├── insns.h
│ ├── insnsa.c
│ ├── insnsb.c
│ ├── insnsd.c
│ ├── insnsi.h
│ ├── insnsn.c
│ ├── labels.h
│ ├── nasm.h
│ ├── nasmint.h
│ ├── nasmlib.h
│ ├── nctype.c
│ ├── nctype.h
│ ├── opflags.h
│ ├── perfhash.h
│ ├── pptok.h
│ ├── preproc.h
│ ├── readnum.c
│ ├── regdis.c
│ ├── regdis.h
│ ├── regflags.c
│ ├── regs.c
│ ├── regs.h
│ ├── regvals.c
│ ├── srcfile.h
│ ├── string.c
│ ├── strlist.h
│ ├── sync.c
│ ├── sync.h
│ ├── tables.h
│ ├── tokens.h
│ ├── ver.c
│ ├── ver.h
│ ├── version.h
│ ├── warnings.c
│ └── warnings.h
├── be.c
├── dasm
├── dasm.c
└── dasm.h
├── editor.c
├── editor.h
├── hex
├── hex.c
└── hex.h
├── img
└── be.png
├── index.html
├── licenses
├── armadillo.txt
├── hx.txt
├── nasm.txt
├── nv.txt
├── riscv.txt
├── sh4.txt
└── vdappc.txt
└── term
├── buffer.c
├── buffer.h
├── terminal.c
└── terminal.h
/CNAME:
--------------------------------------------------------------------------------
1 | be.5ht.co
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2022—2024 Groupoid Infinity
2 |
3 | Permission to use, copy, modify, and/or distribute this software for any
4 | purpose with or without fee is hereby granted, provided that the above
5 | copyright notice and this permission notice appear in all copies.
6 |
7 | YOU CANNOT USE THIS SOFTWARE IN ANY (PROVABLE BY MONEY TRACE)
8 | PROCESS CHAIN OF EXTERMINATING UKRAINIANS BY ANY MEANS OF FASCIST
9 | ACTIONS AGAINST OUR TERRITORIAL INTEGRITY, CULTURAL DIVERSITY BY
10 | APPLYING MILITARY INVASIONS, ECONOMICAL WARS, HUMANITARIAN DISASTERS,
11 | ARTFICIAL HOLODOMORS, GENOCIDE, RAPING, LOOTING, ROBBERIES, SPREADING
12 | FAKE INFORMATION, AND OTHER CONTEMPORARY WEAPONS OF WAR AT SCALE
13 | OR IN INVIDIVUAL MANNER.
14 |
15 | YOU CANNOT USE THIS SOFTWARE BY ANY MEANS IN INTEREST OF LEGAL
16 | ENTITIES OR INDIVIDUALS WHO IS SUPPORTING NOW OR WAS SUPPORTING
17 | BACK THEN FASCISM, RUSCISM, COMMUNISM, CHAUVINISM, HUMILIATION,
18 | AND OTHER SUPPRESSIVE IDEOLOGIES IN DIFFERENT EXPRESSIONS.
19 |
20 | STOP KILLING UKRAINIANS,
21 | THE COUNTER RENDERS TENS OF MILLIONS.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
24 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
25 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
26 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | objects := be.o editor.o \
2 | hex/hex.o dasm/dasm.o term/buffer.o term/terminal.o \
3 | arch/x86/insnsd.o arch/x86/insnsa.o arch/x86/insnsb.o arch/x86/insnsn.o arch/x86/disasm.o \
4 | arch/x86/regdis.o arch/x86/regs.o arch/x86/regflags.o arch/x86/regvals.o \
5 | arch/x86/iflag.o arch/x86/sync.o arch/x86/disp8.o arch/x86/nctype.o arch/x86/readnum.o \
6 | arch/x86/common.o arch/x86/alloc.o arch/x86/string.o \
7 | arch/x86/warnings.o arch/x86/error.o arch/x86/ver.o \
8 | arch/arm/BranchExcSys.o arch/arm/DataProcessingImmediate.o arch/arm/LoadsAndStores.o \
9 | arch/arm/bits.o arch/arm/strext.o \
10 | arch/arm/DataProcessingFloatingPoint.o arch/arm/DataProcessingRegister.o \
11 | arch/arm/armadillo.o arch/arm/instruction.o arch/arm/utils.o \
12 | arch/riscv/riscv-disas.o \
13 | arch/ppc/ppc_disasm.o \
14 | arch/m68k/dis68k.o \
15 | arch/sh4/sh4.o \
16 | arch/nv/core-as.o arch/nv/ctx.o arch/nv/falcon.o arch/nv/gk110.o arch/nv/macro.o arch/nv/vp1.o arch/nv/hash.o \
17 | arch/nv/envyas.o arch/nv/g80.o arch/nv/gm107.o arch/nv/nv.o arch/nv/vuc.o arch/nv/xtensa.o arch/nv/astr.o arch/nv/colors.o \
18 | arch/nv/core-dis.o arch/nv/core.o arch/nv/envydis.o arch/nv/gf100.o arch/nv/hwsq.o arch/nv/vcomp.o arch/nv/mask.o \
19 | arch/nv/varinfo.o arch/nv/vardata.o arch/nv/symtab.o arch/nv/easm.o arch/nv/aprintf.o arch/nv/easm_xfrm.o arch/nv/easm_print.o \
20 | arch/mips/mips-rsp.o \
21 | arch/pdp11/pdp11.o
22 | .PHONY: all
23 | all: be
24 | %.o: %.c
25 | $(CC) -c $(CFLAGS) -o $@ $<
26 | be: $(objects)
27 | $(CC) -o $@ $^ $(LDFLAGS)
28 | .PHONY: install
29 | install:
30 | install -d $(DESTDIR)$(PREFIX)/bin
31 | install -m0755 be $(DESTDIR)$(PREFIX)/bin
32 | .PHONY: clean
33 | clean:
34 | $(RM) $(objects) $(objects:.o=.d) be
35 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Binary Editor
2 | =============
3 |
4 | InfoSec Binary Hex Editor with Retro Disassemblers.
5 |
6 | Features
7 | --------
8 |
9 | * zero deps
10 | * zero builder
11 | * ANSI C99
12 | * termio
13 | * NetBSD, macOS, Linux
14 |
15 | Architectures
16 | -------------
17 |
18 | * EM64T: SSE5, KNI, AVX-512, AES, AMX, TSX, MPX, FMA, VNNI, GFNI, SGX, CET, PKU
19 | * AArch64: v8.5, NEON, SVE, SME
20 | * RISC-V: RV32, RV64, RV128
21 | * PowerPC: POWER, Cell/BE (Sony PS3)
22 | * MIPS: MIPS32, MIPS64 (Nintendo 64, Sony PS2)
23 | * SuperH: SH-4 (Sega Dreamcast)
24 | * M68K: M68000 (Motorola CD-920)
25 | * PDP-11: EIS, FIS, CIS (DEC PDP-11/70)
26 |
27 |
28 |
29 |
30 | Credits
31 | -------
32 |
33 | * Kevin Pors, hx
34 | * Wouter de Waal, dis68k
35 | * Snickerbockers, sh4asm
36 | * Frank Wille, vdappc
37 | * Michael Clark, riscv-disassembler
38 | * Justin Sherman, armadillo
39 | * NASM Authors, nasm
40 | * Marcelina Kościelnicka, envytools
41 | * Namdak Tonpa, dasm-pdp11, dasm-mips
42 |
--------------------------------------------------------------------------------
/arch/arm/BranchExcSys.h:
--------------------------------------------------------------------------------
1 | #ifndef _BRANCHEXCSYS_H_
2 | #define _BRANCHEXCSYS_H_
3 |
4 | int BranchExcSysDisassemble(struct instruction *, struct ad_insn *);
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/arch/arm/DataProcessingFloatingPoint.h:
--------------------------------------------------------------------------------
1 | #ifndef _DATAPROCESSINGFLOATINGPOINT_H_
2 | #define _DATAPROCESSINGFLOATINGPOINT_H_
3 |
4 | int DataProcessingFloatingPointDisassemble(struct instruction *i,
5 | struct ad_insn *);
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/arch/arm/DataProcessingImmediate.h:
--------------------------------------------------------------------------------
1 | #ifndef _DATAPROCESSINGIMMEDIATE_H_
2 | #define _DATAPROCESSINGIMMEDIATE_H_
3 |
4 | int DataProcessingImmediateDisassemble(struct instruction *, struct ad_insn *);
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/arch/arm/DataProcessingRegister.h:
--------------------------------------------------------------------------------
1 | #ifndef _DATAPROCESSINGREGISTER_H_
2 | #define _DATAPROCESSINGREGISTER_H_
3 |
4 | int DataProcessingRegisterDisassemble(struct instruction *, struct ad_insn *);
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/arch/arm/LoadsAndStores.h:
--------------------------------------------------------------------------------
1 | #ifndef _LOADSANDSTORES_H_
2 | #define _LOADSANDSTORES_H_
3 |
4 | #define NO_ALLOCATE 0
5 | #define POST_INDEXED 1
6 | #define OFFSET 2
7 | #define PRE_INDEXED 3
8 |
9 | #define UNSIGNED_IMMEDIATE -1
10 |
11 | #define UNSCALED_IMMEDIATE 0
12 | #define IMMEDIATE_POST_INDEXED 1
13 | #define UNPRIVILEGED 2
14 | #define IMMEDIATE_PRE_INDEXED 3
15 |
16 | static const char *unscaled_instr_tbl[] = {
17 | "sturb",
18 | "ldurb",
19 | "ldursb",
20 | "ldursb",
21 | "stur",
22 | "ldur",
23 | "stur",
24 | "ldur",
25 | "sturh",
26 | "ldurh",
27 | "ldursh",
28 | "ldursh",
29 | "stur",
30 | "ldur",
31 | NULL,
32 | NULL,
33 | "stur",
34 | "ldur",
35 | "ldursw",
36 | NULL,
37 | "stur",
38 | "ldur",
39 | NULL,
40 | NULL,
41 | "stur",
42 | "ldur",
43 | "prfm",
44 | NULL,
45 | "stur",
46 | "ldur" };
47 |
48 | static const char *pre_post_unsigned_register_idx_instr_tbl[] = {
49 | "strb",
50 | "ldrb",
51 | "ldrsb",
52 | "ldrsb",
53 | "str",
54 | "ldr",
55 | "str",
56 | "ldr",
57 | "strh",
58 | "ldrh",
59 | "ldrsh",
60 | "ldrsh",
61 | "str",
62 | "ldr",
63 | NULL,
64 | NULL,
65 | "str",
66 | "ldr",
67 | "ldrsw",
68 | NULL,
69 | "str",
70 | "ldr",
71 | NULL,
72 | NULL,
73 | "str",
74 | "ldr",
75 | NULL,
76 | NULL,
77 | "str",
78 | "ldr" };
79 |
80 | static const char *unprivileged_instr_tbl[] = {
81 | "sttrb",
82 | "ldtrb",
83 | "ldtrsb",
84 | "ldtrsb",
85 | NULL,
86 | NULL,
87 | NULL,
88 | NULL,
89 | "sttrh",
90 | "ldtrh",
91 | "ldtrsh",
92 | "ldtrsh",
93 | NULL,
94 | NULL,
95 | NULL,
96 | NULL,
97 | "sttr",
98 | "ldtr",
99 | "ldtrsw",
100 | NULL,
101 | NULL,
102 | NULL,
103 | NULL,
104 | NULL,
105 | "sttr",
106 | "ldtr",
107 | NULL };
108 |
109 | int LoadsAndStoresDisassemble(struct instruction *, struct ad_insn *);
110 |
111 | #endif
112 |
--------------------------------------------------------------------------------
/arch/arm/armadillo.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "adefs.h"
6 | #include "bits.h"
7 | #include "common.h"
8 | #include "instruction.h"
9 | #include "strext.h"
10 | #include "armadillo.h"
11 |
12 | #include "BranchExcSys.h"
13 | #include "DataProcessingImmediate.h"
14 | #include "DataProcessingFloatingPoint.h"
15 | #include "DataProcessingRegister.h"
16 | #include "LoadsAndStores.h"
17 |
18 | unsigned int * opcode = NULL;
19 | struct ad_insn *insn = NULL;
20 |
21 | char *decodeARM(unsigned long int start, char *outbuf, int *lendis, unsigned long int offset0)
22 | {
23 | *lendis = 4;
24 | opcode = (unsigned int *)start;
25 | ArmadilloDisassemble(*opcode, (unsigned long)opcode, &insn);
26 | memcpy(outbuf,insn->decoded,strlen(insn->decoded)+1);
27 | ArmadilloDone(&insn);
28 | return outbuf;
29 | }
30 |
31 | static int _ArmadilloDisassemble(struct instruction *i,
32 | struct ad_insn **_out){
33 | struct ad_insn *out = *_out;
34 |
35 | unsigned op0 = bits(i->opcode, 25, 28);
36 |
37 | if(op0 == 0){
38 | out->group = AD_G_Reserved;
39 |
40 | unsigned op1 = bits(i->opcode, 16, 24);
41 |
42 | if(op1 != 0)
43 | return 1;
44 |
45 | unsigned imm16 = bits(i->opcode, 0, 15);
46 |
47 | ADD_FIELD(out, op0);
48 | ADD_FIELD(out, op1);
49 | ADD_FIELD(out, imm16);
50 |
51 | ADD_IMM_OPERAND(out, AD_IMM_UINT, *(unsigned *)&imm16);
52 |
53 | concat(&DECODE_STR(out), "udf #%#x", imm16);
54 |
55 | SET_INSTR_ID(out, AD_INSTR_UDF);
56 |
57 | return 0;
58 | }
59 | else if(op0 > 0 && op0 <= 3){
60 | return 1;
61 | }
62 | else if((op0 >> 1) == 4){
63 | out->group = AD_G_DataProcessingImmediate;
64 | return DataProcessingImmediateDisassemble(i, out);
65 | }
66 | else if((op0 >> 1) == 5){
67 | out->group = AD_G_BranchExcSys;
68 | return BranchExcSysDisassemble(i, out);
69 | }
70 | else if((op0 & ~10) == 4){
71 | out->group = AD_G_LoadsAndStores;
72 | return LoadsAndStoresDisassemble(i, out);
73 | }
74 | else if((op0 & ~8) == 5){
75 | out->group = AD_G_DataProcessingRegister;
76 | return DataProcessingRegisterDisassemble(i, out);
77 | }
78 | else if((op0 & ~8) == 7){
79 | out->group = AD_G_DataProcessingFloatingPoint;
80 | return DataProcessingFloatingPointDisassemble(i, out);
81 | }
82 |
83 | return 0;
84 | }
85 |
86 | int ArmadilloDisassemble(unsigned int opcode, unsigned long PC,
87 | struct ad_insn **out){
88 | if(!out || (out && *out))
89 | return 1;
90 |
91 | *out = malloc(sizeof(struct ad_insn));
92 |
93 | (*out)->decoded = NULL;
94 |
95 | (*out)->group = AD_NONE;
96 | (*out)->instr_id = AD_NONE;
97 |
98 | (*out)->fields = NULL;
99 | (*out)->num_fields = 0;
100 |
101 | (*out)->operands = NULL;
102 | (*out)->num_operands = 0;
103 |
104 | (*out)->cc = AD_NONE;
105 |
106 | struct instruction *i = instruction_new(opcode, PC);
107 |
108 | int result = _ArmadilloDisassemble(i, out);
109 |
110 | if(result){
111 | free(DECODE_STR(*out));
112 | DECODE_STR(*out) = NULL;
113 | concat(&DECODE_STR(*out), ".long %#x", i->opcode);
114 | }
115 |
116 | free(i);
117 |
118 | return result;
119 | }
120 |
121 | int ArmadilloDone(struct ad_insn **_insn){
122 | if(!_insn)
123 | return 1;
124 |
125 | struct ad_insn *insn = *_insn;
126 |
127 | free(insn->decoded);
128 | free(insn->fields);
129 | free(insn->operands);
130 |
131 | free(insn);
132 |
133 | *_insn = NULL;
134 |
135 | return 0;
136 | }
137 |
--------------------------------------------------------------------------------
/arch/arm/armadillo.h:
--------------------------------------------------------------------------------
1 | #ifndef _ARMADILLO_H_
2 | #define _ARMADILLO_H_
3 |
4 | #include
5 | #include "adefs.h"
6 |
7 | int ArmadilloDisassemble(unsigned int opcode, unsigned long PC, struct ad_insn **out);
8 | int ArmadilloDone(struct ad_insn **insn);
9 | char *decodeARM(unsigned long int start, char *outbuf, int *outlen, unsigned long int offset0);
10 |
11 | #endif
12 |
--------------------------------------------------------------------------------
/arch/arm/bits.c:
--------------------------------------------------------------------------------
1 | #include "bits.h"
2 |
3 | unsigned int bits(unsigned int number, unsigned int start, unsigned int end){
4 | unsigned int amount = (end - start) + 1;
5 | unsigned int mask = ((1 << amount) - 1) << start;
6 |
7 | return (number & mask) >> start;
8 | }
9 |
10 | unsigned int sign_extend(unsigned int number, int numbits){
11 | if(number & (1 << (numbits - 1)))
12 | return number | ~((1 << numbits) - 1);
13 |
14 | return number;
15 | }
16 |
--------------------------------------------------------------------------------
/arch/arm/bits.h:
--------------------------------------------------------------------------------
1 | #ifndef _BITS_H_
2 | #define _BITS_H_
3 |
4 | unsigned int bits(unsigned int, unsigned int start, unsigned int end);
5 | unsigned int sign_extend(unsigned int number, int numbits);
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/arch/arm/instruction.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "instruction.h"
4 |
5 | struct instruction *instruction_new(unsigned int opcode, unsigned long PC){
6 | struct instruction *i = malloc(sizeof(struct instruction));
7 |
8 | i->opcode = opcode;
9 | i->PC = PC;
10 |
11 | return i;
12 | }
13 |
14 | void instruction_free(struct instruction *i){
15 | free(i);
16 | }
17 |
--------------------------------------------------------------------------------
/arch/arm/instruction.h:
--------------------------------------------------------------------------------
1 | #ifndef _INSTRUCTION_H_
2 | #define _INSTRUCTION_H_
3 |
4 | struct instruction {
5 | unsigned int opcode;
6 | unsigned long PC;
7 | };
8 |
9 | struct instruction *instruction_new(unsigned int, unsigned long);
10 | void instruction_free(struct instruction *);
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/arch/arm/strext.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | static int _concat_internal(char **dst, const char *src, va_list args){
7 | if(!src || !dst)
8 | return 0;
9 |
10 | size_t srclen = strlen(src), dstlen = 0;
11 |
12 | if(*dst)
13 | dstlen = strlen(*dst);
14 |
15 | /* Back up args before it gets used. Client calls va_end
16 | * on the parameter themselves when calling vconcat.
17 | */
18 | va_list args1;
19 | va_copy(args1, args);
20 |
21 | size_t total = srclen + dstlen + vsnprintf(NULL, 0, src, args) + 1;
22 |
23 | char *dst1 = malloc(total);
24 |
25 | if(!(*dst))
26 | *dst1 = '\0';
27 | else{
28 | strncpy(dst1, *dst, dstlen + 1);
29 | free(*dst);
30 | *dst = NULL;
31 | }
32 |
33 | int w = vsnprintf(dst1 + dstlen, total, src, args1);
34 |
35 | va_end(args1);
36 |
37 | *dst = realloc(dst1, strlen(dst1) + 1);
38 |
39 | return w;
40 | }
41 |
42 | int concat(char **dst, const char *src, ...){
43 | va_list args;
44 | va_start(args, src);
45 |
46 | int w = _concat_internal(dst, src, args);
47 |
48 | va_end(args);
49 |
50 | return w;
51 | }
52 |
53 | int vconcat(char **dst, const char *src, va_list args){
54 | return _concat_internal(dst, src, args);
55 | }
56 |
--------------------------------------------------------------------------------
/arch/arm/strext.h:
--------------------------------------------------------------------------------
1 | #ifndef _STREXT_H_
2 | #define _STREXT_H_
3 |
4 | #include
5 |
6 | int vconcat(char **, const char *, va_list);
7 | int concat(char **, const char *, ...);
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/arch/arm/utils.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "utils.h"
6 |
7 | /* Thanks https://github.com/xerub/macho/blob/master/patchfinder64.c */
8 | static unsigned long RORZeroExtendOnes(unsigned int M, unsigned int N,
9 | unsigned int R){
10 | unsigned long val = Ones(M, N);
11 |
12 | if(R == 0)
13 | return val;
14 |
15 | return ((val >> R) & (((unsigned long)1 << (N - R)) - 1)) |
16 | ((val & (((unsigned long)1 << R) - 1)) << (N - R));
17 | }
18 |
19 | int HighestSetBit(unsigned int number, unsigned int n){
20 | int ret = -1;
21 |
22 | for(int i = n-1; i>=0; i--){
23 | if(number & (1 << i))
24 | return i;
25 | }
26 |
27 | return ret;
28 | }
29 |
30 | int LowestSetBit(unsigned int number, unsigned int n){
31 | int ret = n;
32 |
33 | for(int i=0; i> i) & 1) == 1)
46 | result++;
47 | }
48 |
49 | return result;
50 | }
51 |
52 | unsigned long Ones(int len, int N){
53 | (void)N;
54 | unsigned long ret = 0;
55 |
56 | for(int i=len-1; i>=0; i--)
57 | ret |= ((unsigned long)1 << i);
58 |
59 | return ret;
60 | }
61 |
62 | int DecodeBitMasks(unsigned int N, unsigned int imms, unsigned int immr,
63 | int immediate, unsigned long *out){
64 | unsigned int num = (N << 6) | (~imms & 0x3f);
65 | unsigned int len = HighestSetBit(num, 7);
66 |
67 | if(len < 1)
68 | return -1;
69 |
70 | unsigned int levels = Ones(len, 0);
71 |
72 | if(immediate && ((imms & levels) == levels))
73 | return -1;
74 |
75 | unsigned int S = imms & levels;
76 | unsigned int R = immr & levels;
77 | unsigned int esize = 1 << len;
78 |
79 | *out = replicate(RORZeroExtendOnes(S + 1, esize, R), sizeof(unsigned long) * CHAR_BIT, esize);
80 |
81 | return 0;
82 | }
83 |
84 | /*
85 | * num: the number to replicate
86 | * nbits: how many bits make up this number
87 | * cnt: how many times to replicate
88 | */
89 | unsigned long replicate(unsigned long num, int nbits, int cnt){
90 | unsigned long result = 0;
91 |
92 | for(int i=0; i> 6) != 1)
106 | return 0;
107 |
108 | if(sf == 0 && (combined >> 5) != 0)
109 | return 0;
110 |
111 | if(imms < 16)
112 | return (-immr % 16) <= (15 - imms);
113 |
114 | if(imms >= (width - 15))
115 | return (immr % 16) <= (imms - (width - 15));
116 |
117 | return 0;
118 | }
119 |
120 | int IsZero(unsigned long x){
121 | return x == 0;
122 | }
123 |
124 | int IsOnes(unsigned long x, int n){
125 | return x == Ones(n, 0);
126 | }
127 |
128 | int BFXPreferred(unsigned int sf, unsigned int uns,
129 | unsigned int imms, unsigned int immr){
130 | if(imms < immr)
131 | return 0;
132 |
133 | if(imms == ((sf << 6) | 0x3f))
134 | return 0;
135 |
136 | if(immr == 0){
137 | if(sf == 0 && (imms == 0x7 || imms == 0xf))
138 | return 0;
139 | else if(((sf << 1) | uns) == 0x2 && (imms == 0x7 || imms == 0xf || imms == 0x1f))
140 | return 0;
141 | }
142 |
143 | return 1;
144 | }
145 |
146 | char *decode_reg_extend(unsigned int op){
147 | switch(op){
148 | case 0x0:
149 | return "uxtb";
150 | case 0x1:
151 | return "uxth";
152 | case 0x2:
153 | return "uxtw";
154 | case 0x3:
155 | return "uxtx";
156 | case 0x4:
157 | return "sxtb";
158 | case 0x5:
159 | return "sxth";
160 | case 0x6:
161 | return "sxtw";
162 | case 0x7:
163 | return "sxtx";
164 | default:
165 | return NULL;
166 | };
167 | }
168 |
169 | const char *decode_cond(unsigned int cond){
170 | switch(cond){
171 | case 0: return "eq";
172 | case 1: return "ne";
173 | case 2: return "cs";
174 | case 3: return "cc";
175 | case 4: return "mi";
176 | case 5: return "pl";
177 | case 6: return "vs";
178 | case 7: return "vc";
179 | case 8: return "hi";
180 | case 9: return "ls";
181 | case 10: return "ge";
182 | case 11: return "lt";
183 | case 12: return "gt";
184 | case 13: return "le";
185 | case 14: return "al";
186 | case 15: return "nv";
187 | default: return NULL;
188 | };
189 | }
190 |
191 | const char *get_arrangement(unsigned int size, unsigned int Q){
192 | if(size == 0)
193 | return Q == 0 ? "8b" : "16b";
194 | if(size == 1)
195 | return Q == 0 ? "4h" : "8h";
196 | if(size == 2)
197 | return Q == 0 ? "2s" : "4s";
198 | if(size == 3)
199 | return Q == 0 ? "1d" : "2d";
200 |
201 | return NULL;
202 | }
203 |
--------------------------------------------------------------------------------
/arch/arm/utils.h:
--------------------------------------------------------------------------------
1 | #ifndef _UTILS_H_
2 | #define _UTILS_H_
3 |
4 | int HighestSetBit(unsigned int, unsigned int);
5 | int LowestSetBit(unsigned int number, unsigned int n);
6 | int BitCount(unsigned, unsigned);
7 | unsigned long Ones(int len, int N);
8 | int DecodeBitMasks(unsigned int N, unsigned int imms, unsigned int immr, int immediate, unsigned long *out);
9 | unsigned long replicate(unsigned long, int, int);
10 | int MoveWidePreferred(unsigned int sf, unsigned int immN, unsigned int immr, unsigned int imms);
11 | int IsZero(unsigned long x);
12 | int IsOnes(unsigned long x, int n);
13 | int BFXPreferred(unsigned int sf, unsigned int uns, unsigned int imms, unsigned int immr);
14 | char *decode_reg_extend(unsigned int op);
15 | const char *decode_cond(unsigned int cond);
16 | const char *get_arrangement(unsigned int size, unsigned int Q);
17 |
18 | #endif
19 |
--------------------------------------------------------------------------------
/arch/m68k/m68k.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void decodeM68K(unsigned long int start, char *outbuf, int *outlen, unsigned long int offset0);
4 |
--------------------------------------------------------------------------------
/arch/mips/mips.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | char * decodeMIPS(unsigned long int address, char *outbuf, int *lendis, unsigned long int offset);
4 |
--------------------------------------------------------------------------------
/arch/nv/aprintf.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009-2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "util.h"
26 | #include
27 |
28 | char *aprintf(const char *format, ...) {
29 | va_list va;
30 | va_start(va, format);
31 | size_t sz = vsnprintf(0, 0, format, va);
32 | va_end(va);
33 | char *res = malloc(sz + 1);
34 | va_start(va, format);
35 | vsnprintf(res, sz + 1, format, va);
36 | va_end(va);
37 | return res;
38 | }
39 |
--------------------------------------------------------------------------------
/arch/nv/astr.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "util.h"
26 |
27 | void print_escaped_astr(FILE *out, struct astr *astr) {
28 | int i;
29 | fprintf(out, "\"");
30 | for (i = 0; i < astr->len; i++) {
31 | unsigned char c = astr->str[i];
32 | switch (c) {
33 | case '\\':
34 | fprintf(out, "\\\\");
35 | break;
36 | case '\"':
37 | fprintf(out, "\\\"");
38 | break;
39 | case '\n':
40 | fprintf(out, "\\n");
41 | break;
42 | case '\f':
43 | fprintf(out, "\\f");
44 | break;
45 | case '\t':
46 | fprintf(out, "\\t");
47 | break;
48 | case '\a':
49 | fprintf(out, "\\a");
50 | break;
51 | case '\v':
52 | fprintf(out, "\\v");
53 | break;
54 | case '\r':
55 | fprintf(out, "\\r");
56 | break;
57 | default:
58 | if (c >= 0x20 && c <= 0x7e) {
59 | fprintf(out, "%c", c);
60 | } else {
61 | fprintf(out, "\\x%02x", c);
62 | }
63 | break;
64 | }
65 | }
66 | fprintf(out, "\"");
67 | }
68 |
--------------------------------------------------------------------------------
/arch/nv/colors.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "colors.h"
26 |
27 | const struct envy_colors envy_null_colors = {
28 | .reset = "",
29 | .iname = "",
30 | .rname = "",
31 | .mod = "",
32 | .sym = "",
33 | .reg = "",
34 | .regsp = "",
35 | .num = "",
36 | .mem = "",
37 | .btarg = "",
38 | .ctarg = "",
39 | .bctarg = "",
40 | .eval = "",
41 | .comm = "",
42 | .err = "",
43 | };
44 |
45 | const struct envy_colors envy_def_colors = {
46 | .reset = "\x1b[0m",
47 | .iname = "\x1b[0;32m",
48 | .rname = "\x1b[0;32m",
49 | .mod = "\x1b[0;36m",
50 | .sym = "\x1b[0;36m",
51 | .reg = "\x1b[0;31m",
52 | .regsp = "\x1b[0;35m",
53 | .num = "\x1b[0;33m",
54 | .mem = "\x1b[0;35m",
55 | .btarg = "\x1b[0;35m",
56 | .ctarg = "\x1b[0;1;37m",
57 | .bctarg = "\x1b[0;1;35m",
58 | .eval = "\x1b[0;35m",
59 | .comm = "\x1b[0;34m",
60 | .err = "\x1b[0;1;31m",
61 | };
62 |
--------------------------------------------------------------------------------
/arch/nv/colors.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef COLORS_H
26 | #define COLORS_H
27 |
28 | struct envy_colors {
29 | const char *reset;
30 | const char *iname; /* instruction name */
31 | const char *rname; /* register or bitfield name */
32 | const char *mod; /* instruction modifier */
33 | const char *sym; /* auxiliary char like { , + */
34 | const char *reg; /* ISA register */
35 | const char *regsp; /* special ISA register */
36 | const char *num; /* immediate number */
37 | const char *mem; /* memory reference */
38 | const char *btarg; /* branch target */
39 | const char *ctarg; /* call target */
40 | const char *bctarg; /* branch and call target */
41 | const char *eval; /* enum value */
42 | const char *comm; /* comment */
43 | const char *err; /* error */
44 | };
45 |
46 | extern const struct envy_colors envy_null_colors;
47 | extern const struct envy_colors envy_def_colors;
48 |
49 | #endif
50 |
--------------------------------------------------------------------------------
/arch/nv/core.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009-2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "dis.h"
26 | #include "dis-intern.h"
27 |
28 | int op1blen[] = { 1 };
29 | int op2blen[] = { 2 };
30 | int op3blen[] = { 3 };
31 | int op4blen[] = { 4 };
32 | int op5blen[] = { 5 };
33 | int op6blen[] = { 6 };
34 | int op8blen[] = { 8 };
35 |
36 | static const struct {
37 | const char *name;
38 | struct disisa *isa;
39 | } isas[] = {
40 | "g80", &g80_isa_s,
41 | "nv50", &g80_isa_s, /* XXX remove some day */
42 | "gf100", &gf100_isa_s,
43 | "nvc0", &gf100_isa_s, /* XXX remove some day */
44 | "gk110", &gk110_isa_s,
45 | "gm107", &gm107_isa_s,
46 | "ctx", &ctx_isa_s,
47 | "falcon", &falcon_isa_s,
48 | "fuc", &falcon_isa_s, /* XXX remove some day */
49 | "fµc", &falcon_isa_s, /* XXX remove some day */
50 | "hwsq", &hwsq_isa_s,
51 | "xtensa", &xtensa_isa_s,
52 | "vp2", &xtensa_isa_s, /* XXX remove some day, preferably before cracking the actual VP2 ISA open */
53 | "vuc", &vuc_isa_s,
54 | "vµc", &vuc_isa_s,
55 | "macro", ¯o_isa_s,
56 | "vp1", &vp1_isa_s,
57 | "vcomp", &vcomp_isa_s,
58 | };
59 |
60 | const struct disisa *ed_getisa(const char *name) {
61 | int i;
62 | for (i = 0; i < sizeof isas / sizeof *isas; i++)
63 | if (!strcmp(name, isas[i].name)) {
64 | struct disisa *isa = isas[i].isa;
65 | if (!isa->prepdone) {
66 | if (isa->prep)
67 | isa->prep(isa);
68 | if (!isa->vardata) {
69 | isa->vardata = vardata_new("empty");
70 | vardata_validate(isa->vardata);
71 | }
72 | isa->prepdone = 1;
73 | }
74 | return isa;
75 | }
76 | return 0;
77 | };
78 |
79 | void ed_freeisa(const struct disisa *isa) {
80 | if (!isa->prepdone)
81 | return;
82 | vardata_del(isa->vardata);
83 | ((struct disisa *)isa)->prepdone = 0;
84 | }
85 |
86 | uint32_t ed_getcbsz(const struct disisa *isa, struct varinfo *varinfo) {
87 | if (isa->getcbsz)
88 | return isa->getcbsz(isa, varinfo);
89 | return isa->posunit * 8;
90 | }
91 |
--------------------------------------------------------------------------------
/arch/nv/dis.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009-2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef DIS_H
26 | #define DIS_H
27 |
28 | #include "util.h"
29 | #include "var.h"
30 | #include "colors.h"
31 | #include
32 | #include
33 |
34 | struct disisa {
35 | struct insn *troot;
36 | int maxoplen;
37 | int opunit;
38 | int posunit;
39 | int i_need_g80as_hack;
40 | int prepdone;
41 | void (*prep)(struct disisa *);
42 | struct vardata *vardata;
43 | uint32_t (*getcbsz)(const struct disisa *isa, struct varinfo *varinfo);
44 | struct insn *trootas;
45 | struct insn *tsched;
46 | int schedpos;
47 | };
48 |
49 | struct label {
50 | const char *name;
51 | unsigned long long val;
52 | int type;
53 | unsigned size;
54 | };
55 |
56 | const struct disisa *ed_getisa(const char *name);
57 | void ed_freeisa(const struct disisa *isa);
58 |
59 | uint32_t ed_getcbsz(const struct disisa *isa, struct varinfo *varinfo);
60 |
61 | static inline int ed_getcstride(const struct disisa *isa, struct varinfo *varinfo) {
62 | return CEILDIV(ed_getcbsz(isa, varinfo), 8);
63 | }
64 |
65 | void envydis (const struct disisa *isa, FILE *out, uint8_t *code, uint32_t start, int num, struct varinfo *varinfo, int quiet, struct label *labels, int labelsnum, const struct envy_colors *cols);
66 |
67 | #endif
68 |
--------------------------------------------------------------------------------
/arch/nv/easm_xfrm.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "easm.h"
26 | #include
27 |
28 | void easm_cfold_sinsn(struct easm_sinsn *sinsn) {
29 | int i, j;
30 | for (i = 0; i < sinsn->operandsnum; i++)
31 | for (j = 0; j < sinsn->operands[i]->exprsnum; j++)
32 | easm_cfold_expr(sinsn->operands[i]->exprs[j]);
33 | }
34 |
35 | int easm_cfold_expr(struct easm_expr *expr) {
36 | if (expr->type == EASM_EXPR_NUM)
37 | return 1;
38 | int e1f = 1, e2f = 1;
39 | if (expr->e1)
40 | e1f = easm_cfold_expr(expr->e1);
41 | if (expr->e2)
42 | e2f = easm_cfold_expr(expr->e2);
43 | if (expr->sinsn)
44 | easm_cfold_sinsn(expr->sinsn);
45 | if (!e1f || !e2f || expr->type < EASM_EXPR_LOR || expr->type > EASM_EXPR_LNOT)
46 | return 0;
47 | uint64_t val;
48 | uint64_t x;
49 | switch (expr->type) {
50 | case EASM_EXPR_NEG:
51 | val = -expr->e1->num;
52 | break;
53 | case EASM_EXPR_NOT:
54 | val = ~expr->e1->num;
55 | break;
56 | case EASM_EXPR_LNOT:
57 | val = !expr->e1->num;
58 | break;
59 | case EASM_EXPR_MUL:
60 | val = expr->e1->num * expr->e2->num;
61 | break;
62 | case EASM_EXPR_DIV:
63 | x = expr->e2->num;
64 | if (x)
65 | val = expr->e1->num / x;
66 | else {
67 | fprintf (stderr, LOC_FORMAT(expr->loc, "Division by 0\n"));
68 | return 0;
69 | }
70 | break;
71 | case EASM_EXPR_MOD:
72 | x = expr->e2->num;
73 | if (x)
74 | val = expr->e1->num % x;
75 | else {
76 | fprintf (stderr, LOC_FORMAT(expr->loc, "Division by 0\n"));
77 | return 0;
78 | }
79 | break;
80 | case EASM_EXPR_ADD:
81 | val = expr->e1->num + expr->e2->num;
82 | break;
83 | case EASM_EXPR_SUB:
84 | val = expr->e1->num - expr->e2->num;
85 | break;
86 | case EASM_EXPR_SHL:
87 | val = expr->e1->num << expr->e2->num;
88 | break;
89 | case EASM_EXPR_SHR:
90 | val = expr->e1->num >> expr->e2->num;
91 | break;
92 | case EASM_EXPR_AND:
93 | val = expr->e1->num & expr->e2->num;
94 | break;
95 | case EASM_EXPR_XOR:
96 | val = expr->e1->num ^ expr->e2->num;
97 | break;
98 | case EASM_EXPR_OR:
99 | val = expr->e1->num | expr->e2->num;
100 | break;
101 | case EASM_EXPR_LAND:
102 | val = expr->e1->num && expr->e2->num;
103 | break;
104 | case EASM_EXPR_LOR:
105 | val = expr->e1->num || expr->e2->num;
106 | break;
107 | default:
108 | abort();
109 | }
110 | easm_del_expr(expr->e1);
111 | easm_del_expr(expr->e2);
112 | expr->e1 = 0;
113 | expr->e2 = 0;
114 | expr->num = val;
115 | expr->type = EASM_EXPR_NUM;
116 | return 1;
117 | }
118 |
119 | void easm_cfold_insn(struct easm_insn *insn) {
120 | int i, j;
121 | for (i = 0; i < insn->subinsnsnum; i++) {
122 | for (j = 0; j < insn->subinsns[i]->prefsnum; j++)
123 | easm_cfold_expr(insn->subinsns[i]->prefs[j]);
124 | easm_cfold_sinsn(insn->subinsns[i]->sinsn);
125 | }
126 | }
127 |
128 | void easm_substpos_sinsn(struct easm_sinsn *sinsn, uint64_t val) {
129 | int i, j;
130 | for (i = 0; i < sinsn->operandsnum; i++)
131 | for (j = 0; j < sinsn->operands[i]->exprsnum; j++)
132 | easm_substpos_expr(sinsn->operands[i]->exprs[j], val);
133 | }
134 |
135 | void easm_substpos_expr(struct easm_expr *expr, uint64_t val) {
136 | if (expr->e1)
137 | easm_substpos_expr(expr->e1, val);
138 | if (expr->e2)
139 | easm_substpos_expr(expr->e2, val);
140 | if (expr->sinsn)
141 | easm_substpos_sinsn(expr->sinsn, val);
142 | if (expr->type == EASM_EXPR_POS) {
143 | expr->type = EASM_EXPR_NUM;
144 | expr->num = val;
145 | }
146 | }
147 |
148 | void easm_substpos_insn(struct easm_insn *insn, uint64_t val) {
149 | int i, j;
150 | for (i = 0; i < insn->subinsnsnum; i++) {
151 | for (j = 0; j < insn->subinsns[i]->prefsnum; j++)
152 | easm_substpos_expr(insn->subinsns[i]->prefs[j], val);
153 | easm_substpos_sinsn(insn->subinsns[i]->sinsn, val);
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/arch/nv/envyas.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009-2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef ENVYAS_H
26 | #define ENVYAS_H
27 |
28 | #include "easm.h"
29 | #include "dis-intern.h"
30 |
31 | struct reloc {
32 | const struct rbitfield *bf;
33 | struct easm_expr *expr;
34 | };
35 |
36 | struct match {
37 | int oplen;
38 | ull a[MAXOPLEN], m[MAXOPLEN];
39 | int lpos;
40 | struct reloc relocs[8];
41 | int nrelocs;
42 | };
43 |
44 | struct matches {
45 | struct match *m;
46 | int mnum;
47 | int mmax;
48 | };
49 |
50 | int setsbf (struct match *res, int pos, int len, ull num);
51 |
52 | ull getrbf_as(const struct rbitfield *bf, ull *a, ull *m, ull cpos);
53 |
54 | struct matches *do_as(const struct disisa *isa, struct varinfo *varinfo, struct easm_insn *insn);
55 |
56 | #endif
57 |
--------------------------------------------------------------------------------
/arch/nv/hash.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "util.h"
26 |
27 | uint32_t elf_hash(const char *str) {
28 | const unsigned char *ustr = (const unsigned char *)str;
29 | uint32_t h = 0;
30 | while (*ustr) {
31 | h <<= 4;
32 | h += *ustr++;
33 | h ^= (h >> 24) & 0xf0;
34 | h &= 0xfffffff;
35 | }
36 | return h;
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/arch/nv/hwsq.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "dis-intern.h"
26 |
27 | #define F_NV41P 1
28 | #define F_NV17F 2
29 | #define F_NV41F 4
30 |
31 | /*
32 | * Immediate fields
33 | */
34 |
35 | static struct rbitfield imm16off = { 8, 16 };
36 | #define IMM16 atomrimm, &imm16off
37 | static struct rbitfield imm32off = { 8, 32 };
38 | #define IMM32 atomrimm, &imm32off
39 | static struct bitfield flagoff = { 0, 5 };
40 | #define FLAG atomimm, &flagoff
41 | static struct bitfield waitoff = { 0, 2 };
42 | #define WAIT atomimm, &waitoff
43 | static struct bitfield waitsoff = { 2, 4, .shr = 1 };
44 | #define WAITS atomimm, &waitsoff
45 | static struct bitfield eventoff = { 8, 5 };
46 | #define EVENT atomimm, &eventoff
47 | static struct bitfield evaloff = { 16, 1 };
48 | #define EVAL atomimm, &evaloff
49 |
50 | static struct insn tabevent[] = {
51 | { 0x0000, 0xff00, C("FB_PAUSED") },
52 | { 0x0100, 0xff00, C("HEAD0_VBLANK") },
53 | { 0x0200, 0xff00, C("HEAD0_HBLANK") },
54 | { 0x0300, 0xff00, C("HEAD1_VBLANK") },
55 | { 0x0400, 0xff00, C("HEAD1_HBLANK") },
56 | { 0, 0, EVENT },
57 | };
58 |
59 | static struct insn tabfl[] = {
60 | { 0x00, 0x1f, C("GPIO_2_OUT"), .fmask = F_NV17F },
61 | { 0x01, 0x1f, C("GPIO_2_OE"), .fmask = F_NV17F },
62 | { 0x02, 0x1f, C("GPIO_3_OUT"), .fmask = F_NV17F },
63 | { 0x03, 0x1f, C("GPIO_3_OE"), .fmask = F_NV17F },
64 | { 0x04, 0x1f, C("PRAMDAC0_UNK880_28"), .fmask = F_NV17F },
65 | { 0x05, 0x1f, C("PRAMDAC1_UNK880_28"), .fmask = F_NV17F },
66 | { 0x06, 0x1f, C("PRAMDAC0_UNK880_29"), .fmask = F_NV17F },
67 | { 0x07, 0x1f, C("PRAMDAC1_UNK880_29"), .fmask = F_NV17F },
68 | { 0x0e, 0x1f, C("GPIO_9_OUT"), .fmask = F_NV17F },
69 | { 0x0f, 0x1f, C("GPIO_9_OE"), .fmask = F_NV17F },
70 | { 0x10, 0x1f, C("FB_PAUSE"), .fmask = F_NV41P },
71 | { 0x19, 0x1f, C("PWM_2_ENABLE"), .fmask = F_NV41F },
72 | { 0x1a, 0x1f, C("PWM_1_ENABLE"), .fmask = F_NV41F },
73 | { 0x1b, 0x1f, C("PWM_0_ENABLE"), .fmask = F_NV17F },
74 | { 0x1c, 0x1f, C("PBUS_DEBUG_1_UNK22"), .fmask = F_NV17F },
75 | { 0x1d, 0x1f, C("PBUS_DEBUG_1_UNK24"), .fmask = F_NV17F },
76 | { 0x1e, 0x1f, C("PBUS_DEBUG_1_UNK26"), .fmask = F_NV17F },
77 | { 0x1f, 0x1f, C("PBUS_DEBUG_1_UNK27"), .fmask = F_NV17F },
78 | { 0, 0, FLAG },
79 | };
80 |
81 | static struct insn tabm[] = {
82 | { 0x00, 0xff, OP1B, N("nop") },
83 | { 0x00, 0xc0, OP1B, N("wait"), WAIT, N("shl"), WAITS },
84 | { 0x40, 0xff, OP3B, N("addrlo"), IMM16, .fmask = F_NV41P },
85 | { 0x42, 0xff, OP3B, N("datalo"), IMM16, .fmask = F_NV41P },
86 | { 0x5f, 0xff, OP3B, N("ewait"), T(event), EVAL, .fmask = F_NV41P },
87 | { 0x7f, 0xff, OP1B, N("exit") },
88 | { 0x80, 0xe0, OP1B, N("unset"), T(fl) },
89 | { 0xa0, 0xe0, OP1B, N("set1"), T(fl) },
90 | { 0xc0, 0xe0, OP1B, N("set0"), T(fl) },
91 | { 0xe0, 0xff, OP5B, N("addr"), IMM32, .fmask = F_NV41P },
92 | { 0xe2, 0xff, OP5B, N("data"), IMM32, .fmask = F_NV41P },
93 | { 0, 0, OP1B, OOPS },
94 | };
95 |
96 | static void hwsq_prep(struct disisa *isa) {
97 | isa->vardata = vardata_new("hwsq");
98 | int f_nv41op = vardata_add_feature(isa->vardata, "nv41op", "NV41+ features");
99 | int f_nv17f = vardata_add_feature(isa->vardata, "nv17f", "NV17:G80 flags");
100 | int f_nv41f = vardata_add_feature(isa->vardata, "nv41f", "NV41:G80 flags");
101 | if (f_nv41op == -1 || f_nv17f == -1 || f_nv41f == -1)
102 | abort();
103 | int vs_chipset = vardata_add_varset(isa->vardata, "chipset", "GPU chipset");
104 | if (vs_chipset == -1)
105 | abort();
106 | int v_nv17 = vardata_add_variant(isa->vardata, "nv17", "NV17:NV41", vs_chipset);
107 | int v_nv41 = vardata_add_variant(isa->vardata, "nv41", "NV41:G80", vs_chipset);
108 | int v_g80 = vardata_add_variant(isa->vardata, "g80", "G80+", vs_chipset);
109 | if (v_nv17 == -1 || v_nv41 == -1 || v_g80 == -1)
110 | abort();
111 | vardata_variant_feature(isa->vardata, v_nv17, f_nv17f);
112 | vardata_variant_feature(isa->vardata, v_nv41, f_nv17f);
113 | vardata_variant_feature(isa->vardata, v_nv41, f_nv41f);
114 | vardata_variant_feature(isa->vardata, v_nv41, f_nv41op);
115 | vardata_variant_feature(isa->vardata, v_g80, f_nv41op);
116 | if (vardata_validate(isa->vardata))
117 | abort();
118 | }
119 |
120 | struct disisa hwsq_isa_s = {
121 | tabm,
122 | 5,
123 | 1,
124 | 1,
125 | .prep = hwsq_prep,
126 | };
127 |
--------------------------------------------------------------------------------
/arch/nv/mask.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "mask.h"
26 |
27 | void mask_or(uint32_t *dmask, uint32_t *smask, int size) {
28 | int rsize = MASK_SIZE(size);
29 | int i;
30 | for (i = 0; i < rsize; i++)
31 | dmask[i] |= smask[i];
32 | }
33 | int mask_or_r(uint32_t *dmask, uint32_t *smask, int size) {
34 | int rsize = MASK_SIZE(size);
35 | int i;
36 | int res = 0;
37 | for (i = 0; i < rsize; i++) {
38 | uint32_t n = dmask[i] | smask[i];
39 | if (n != dmask[i])
40 | res = 1;
41 | dmask[i] = n;
42 | }
43 | return res;
44 | }
45 |
46 | int mask_intersect(uint32_t *a, uint32_t *b, int size) {
47 | int rsize = MASK_SIZE(size);
48 | int i;
49 | for (i = 0; i < rsize; i++)
50 | if (a[i] & b[i]) {
51 | int j;
52 | for (j = 0; j < MASK_CHUNK_SIZE; j++)
53 | if (a[i] & b[i] & (uint32_t)1 << j)
54 | return j;
55 | }
56 | return -1;
57 | }
58 |
59 | int mask_contains(uint32_t *a, uint32_t *b, int size) {
60 | int rsize = MASK_SIZE(size);
61 | int i;
62 | for (i = 0; i < rsize; i++)
63 | if ((a[i] & b[i]) != b[i]) {
64 | return 0;
65 | }
66 | return 1;
67 | }
68 |
69 | void mask_print(FILE *out, uint32_t *mask, int size) {
70 | int rsize = MASK_SIZE(size);
71 | int i;
72 | for (i = 0; i < rsize; i++) {
73 | if (i)
74 | fprintf(out, " ");
75 | fprintf (out, "%08"PRIx32, mask[i]);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/arch/nv/mask.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef MASK_H
26 | #define MASK_H
27 |
28 | #include "util.h"
29 | #include
30 | #include
31 | #include
32 |
33 | #define MASK_CHUNK_SIZE 32
34 | #define MASK_SIZE(num) CEILDIV((num), MASK_CHUNK_SIZE)
35 |
36 | static inline int mask_get(uint32_t *mask, int num) {
37 | return (mask[num/32] >> (num % 32)) & 1;
38 | }
39 |
40 | static inline void mask_set(uint32_t *mask, int num) {
41 | mask[num/32] |= 1 << (num % 32);
42 | }
43 |
44 | static inline uint32_t *mask_new(int num) {
45 | uint32_t *res = calloc(sizeof *res, MASK_SIZE(num));
46 | return res;
47 | }
48 |
49 | static inline uint32_t *mask_dup(uint32_t *mask, int num) {
50 | uint32_t *res = mask_new(num);
51 | memcpy(res, mask, MASK_SIZE(num) * sizeof *res);
52 | return res;
53 | }
54 |
55 | void mask_or(uint32_t *dmask, uint32_t *smask, int size);
56 | int mask_or_r(uint32_t *dmask, uint32_t *smask, int size);
57 |
58 | int mask_intersect(uint32_t *a, uint32_t *b, int size);
59 | int mask_contains(uint32_t *a, uint32_t *b, int size);
60 | void mask_print(FILE *out, uint32_t *mask, int size);
61 |
62 | #endif
63 |
--------------------------------------------------------------------------------
/arch/nv/nv.c:
--------------------------------------------------------------------------------
1 | // Copyright (c) Namdak Tonpa
2 | // nVidia G80 SM89
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include "../../editor.h"
10 |
11 | uint64_t NVword(unsigned long int address) {
12 | uint64_t operation = (uint64_t)*((unsigned long int *)address);
13 | return operation;
14 | }
15 |
16 | char nvout[1000];
17 |
18 | char *decodeNV(unsigned long int address, char *outbuf, int *lendis, unsigned long int offset0)
19 | {
20 | struct editor *e = editor();
21 | unsigned long int start = address, i;
22 | unsigned long int finish = address + 8;
23 | uint64_t operation = NVword(start);
24 | for (i = 0; i < 1000; i++) nvout[i] = 0;
25 |
26 | sprintf(nvout, "%s ", "nop");
27 |
28 | memcpy(outbuf,nvout,strlen(nvout));
29 | outbuf[strlen(nvout)] = '\0';
30 | *lendis = finish - start;
31 | return outbuf;
32 | }
33 |
--------------------------------------------------------------------------------
/arch/nv/nv.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | char * decodeNV(unsigned long int address, char *outbuf, int *lendis, unsigned long int offset);
4 |
--------------------------------------------------------------------------------
/arch/nv/nvidia.txt:
--------------------------------------------------------------------------------
1 |
2 | Maxwell Volta Turing Ampere Hopper
3 | Pascal Ada
4 |
5 | ▶ RX x x x x x
6 | ▶ SRX x x x x x
7 | ▶ PX x x x x x
8 | ▶ c[X][Y] x x x x x
9 | ▶ URX - - x x x
10 | ▶ UPX - - - x x
11 | ▶ desc[URX][RY] - - - - x
12 |
13 |
14 | GM100 Maxwell
15 | GP100 Pascal
16 | TU100 Turing
17 | GV100 Volta
18 | GA100 Ampere
19 | G80/G84/G86/G92/G94/G96/G98 Tesla
20 | AD100 Ada
21 |
22 | 00 00000000 NOP
23 | 02 00000010 HMNMX2
24 | 04 00000100 FMUL
25 | 00001000 IADD3.[X]
26 | 00001001 UIADD3.[X,64,64.X]
27 | 00001100 HADD2.[F32]
28 | 00010000 FSEL
29 | 00010100 DMUL
30 | 00011000 @P0 NOP
31 | 00011100 I2I.SAT.S32
32 | 00100000 R2P
33 | 00100001 UR2UP
34 | 00100100 IMAD
35 | 00100101 UIMAD.[X,WIDE,WIDE.X]
36 | 00101000 VABSDIFF
37 | 00101100 HSETP2
38 | 00110000 ISETP.AND.[EX]
39 | 00110001 UISETP.AND.[EX]
40 | 00111000 PLOP3.LUT
41 | 01000000 MOV
42 | 01000001 UMOV/LDC
43 | 01000100 FSWZADD
44 | 01001000 LOP3.LUT
45 | 01001001 ULOP3.LUT
46 | 01001100 HMUL2
47 | 01010000 FSET.BF
48 | 01010100 DSETP
49 | 01011000 SGXT
50 | 01011101 UF2FP
51 | 01100000 VOTE
52 | 01100001 VOTEU
53 | 01100100 IPA
54 | 01101000 PRMT
55 | 01101001 UPRMT
56 | 01111100 F2FP.[RS,F32.MERGE_C,F32.PACK_AB,F32]
57 | 10000000 PMTRIG
58 | 10000100 FADD
59 | 10000100 ALD.[P,PHYS]
60 | 10001000 LEA.[X,HI,HI.X,SX32.HI.X,HI.SX32]
61 | 10001001 ULEA.[X,HI,HI.X,SX32.HI.X,HI.SX32]
62 | 10001100 HFMA2
63 | 10010000 FMNMX
64 | 10010001 SHFL
65 | 10010100 DADD
66 | 10011000 SHF
67 | 10011001 USHF
68 | 10011100 I2IP.S32
69 | 10100000 CS2R
70 | 10100010 I2FP
71 | 10101000 VABSDIFF4
72 | 10110000 CSMTEST
73 | 11000000 P2R
74 | 11000001 UP2UR
75 | 11000010 F2IP.F32
76 | 11000100 FFMA
77 | 11001000 IABS
78 | 11001001 SUATOM.D.[CAS]
79 | 11001100 HSET2
80 | 11001101 SUQUERY
81 | 11010000 FSETP
82 | 11010100 DFMA
83 | 11011000 BMSK
84 | 11011100 LDSM.16
85 | 11100000 SEL
86 | 11100001 USEL
87 | 11101000 IMNMX
88 | 11101001 SULD.[P,D]
89 |
--------------------------------------------------------------------------------
/arch/nv/symtab.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "symtab.h"
26 | #include "util.h"
27 | #include
28 | #include
29 |
30 | /* the last one is not a prime - elf hash only goes up to 2^28-1, no point in having more buckets */
31 | static const int primes[] = { 127, 509, 2039, 8191, 32749, 131071, 524287, 2097143, 8388593, 33554393, 134217689, 268435456 };
32 | static const int numprimes = sizeof primes / sizeof *primes;
33 |
34 | struct symtab *symtab_new() {
35 | int i;
36 | struct symtab *res = calloc(sizeof *res, 1);
37 | res->bucketsnum = primes[0];
38 | res->buckets = malloc(sizeof *res->buckets * res->bucketsnum);
39 | for (i = 0; i < res->bucketsnum; i++)
40 | res->buckets[i] = -1;
41 | return res;
42 | }
43 |
44 | void symtab_del(struct symtab *tab) {
45 | int i;
46 | for (i = 0; i < tab->symsnum; i++)
47 | free(tab->syms[i].name);
48 | free(tab->buckets);
49 | free(tab->syms);
50 | free(tab);
51 | }
52 |
53 | int symtab_get(struct symtab *tab, const char *name, int *ptype, int *pdata) {
54 | int i = tab->buckets[elf_hash(name) % tab->bucketsnum];
55 | while (i != -1) {
56 | if (!strcmp(tab->syms[i].name, name)) {
57 | if (ptype)
58 | *ptype = tab->syms[i].type;
59 | if (pdata)
60 | *pdata = tab->syms[i].data;
61 | return i;
62 | }
63 | i = tab->syms[i].hchain;
64 | }
65 | return -1;
66 | }
67 |
68 | int symtab_get_t(struct symtab *tab, const char *name, int type, int *pdata) {
69 | int rtype;
70 | int res = symtab_get(tab, name, &rtype, pdata);
71 | if (res == -1)
72 | return res;
73 | if (rtype != type)
74 | return -1;
75 | return res;
76 | }
77 |
78 | int symtab_put(struct symtab *tab, const char *cname, int type, int data) {
79 | if (symtab_get(tab, cname, 0, 0) != -1)
80 | return -1;
81 | char *name = strdup(cname);
82 | struct symtab_sym sym;
83 | uint32_t hash = elf_hash(name);
84 | int bucket = hash % tab->bucketsnum;
85 | int res = tab->symsnum;
86 | sym.name = name;
87 | sym.type = type;
88 | sym.data = data;
89 | sym.hash = hash;
90 | sym.hchain = tab->buckets[bucket];
91 | tab->buckets[bucket] = res;
92 | ADDARRAY(tab->syms, sym);
93 | if (tab->symsnum > tab->bucketsnum && tab->bucketsnum != primes[numprimes-1]) {
94 | /* rehash */
95 | int i;
96 | free(tab->buckets);
97 | for (i = 0; i < numprimes-1; i++)
98 | if (tab->symsnum <= primes[i])
99 | break;
100 | tab->bucketsnum = primes[i];
101 | tab->buckets = malloc(tab->bucketsnum * sizeof *tab->buckets);
102 | for (i = 0; i < tab->bucketsnum; i++)
103 | tab->buckets[i] = -1;
104 | for (i = 0; i < tab->symsnum; i++) {
105 | bucket = tab->syms[i].hash % tab->bucketsnum;
106 | tab->syms[i].hchain = tab->buckets[bucket];
107 | tab->buckets[bucket] = i;
108 | }
109 | }
110 | return res;
111 | }
112 |
--------------------------------------------------------------------------------
/arch/nv/symtab.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef SYMTAB_H
26 | #define SYMTAB_H
27 |
28 | #include
29 |
30 | struct symtab_sym {
31 | char *name;
32 | uint32_t hash;
33 | int hchain;
34 | int type;
35 | int data;
36 | };
37 |
38 | struct symtab {
39 | struct symtab_sym *syms;
40 | int symsnum;
41 | int symsmax;
42 | int *buckets;
43 | int bucketsnum;
44 | };
45 |
46 | struct symtab *symtab_new();
47 | void symtab_del(struct symtab *tab);
48 | int symtab_get(struct symtab *tab, const char *name, int *ptype, int *pdata);
49 | int symtab_get_t(struct symtab *tab, const char *name, int type, int *pdata);
50 | int symtab_put(struct symtab *tab, const char *name, int type, int data);
51 | static inline int symtab_get_td(struct symtab *tab, const char *name, int type) {
52 | int res;
53 | if (symtab_get_t(tab, name, type, &res) == -1)
54 | return -1;
55 | return res;
56 | }
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/arch/nv/util.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2011 Marcelina Kościelnicka
3 | * Copyright (C) 2010 Francisco Jerez
4 | * All Rights Reserved.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a
7 | * copy of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom the
11 | * Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice (including the next
14 | * paragraph) shall be included in all copies or substantial portions of the
15 | * Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 | * OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | #ifndef UTIL_H
27 | #define UTIL_H
28 |
29 | #include
30 | #include
31 | #include
32 | #include
33 |
34 | #define ADDARRAY(a, e) \
35 | do { \
36 | if ((a ## num) >= (a ## max)) { \
37 | if (!(a ## max)) \
38 | (a ## max) = 16; \
39 | else \
40 | (a ## max) *= 2; \
41 | (a) = realloc((a), (a ## max)*sizeof(*(a))); \
42 | } \
43 | (a)[(a ## num)++] = (e); \
44 | } while(0)
45 |
46 | #define FINDARRAY(a, tmp, pred) \
47 | ({ \
48 | int __i; \
49 | \
50 | for (__i = 0; __i < (a ## num); __i++) { \
51 | tmp = (a)[__i]; \
52 | if (pred) \
53 | break; \
54 | } \
55 | \
56 | tmp = ((pred) ? tmp : NULL); \
57 | })
58 |
59 | /* ceil(log2(x)) */
60 | static inline int clog2(uint64_t x) {
61 | if (!x)
62 | return x;
63 | int r = 0;
64 | while (x - 1 > (1ull << r) - 1)
65 | r++;
66 | return r;
67 | }
68 |
69 | #define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a))
70 |
71 | #ifndef __cplusplus
72 |
73 | #define min(a,b) \
74 | ({ \
75 | typeof (a) _a = (a); \
76 | typeof (b) _b = (b); \
77 | _a < _b ? _a : _b; \
78 | })
79 |
80 | #define max(a,b) \
81 | ({ \
82 | typeof (a) _a = (a); \
83 | typeof (b) _b = (b); \
84 | _a > _b ? _a : _b; \
85 | })
86 |
87 | #endif
88 |
89 | #define CEILDIV(a, b) (((a) + (b) - 1)/(b))
90 |
91 | #define extr(a, b, c) ((uint64_t)(a) << (64 - (b) - (c)) >> (64 - (c)))
92 | #define extrs(a, b, c) ((int64_t)(a) << (64 - (b) - (c)) >> (64 - (c)))
93 | #define sext(a, b) extrs((a), 0, (b)+1)
94 | #define bflmask(a) ((2ull << ((a)-1)) - 1)
95 | #define insrt(a, b, c, d) ((a) = ((a) & ~(bflmask(c) << (b))) | ((d) & bflmask(c)) << (b))
96 |
97 | struct envy_loc {
98 | int lstart;
99 | int cstart;
100 | int lend;
101 | int cend;
102 | const char *file;
103 | };
104 |
105 | #define LOC_FORMAT(loc, str) "%s:%d.%d-%d.%d: " str, (loc).file, (loc).lstart, (loc).cstart, (loc).lend, (loc).cend
106 |
107 | uint32_t elf_hash(const char *str);
108 |
109 | FILE *find_in_path(const char *name, const char *path, char **pfullname);
110 |
111 | struct astr {
112 | char *str;
113 | size_t len;
114 | };
115 |
116 | void print_escaped_astr(FILE *out, struct astr *astr);
117 |
118 | char *aprintf(const char *format, ...);
119 |
120 | FILE *open_input(const char *filename);
121 |
122 | #ifdef NDEBUG
123 | #undef assert
124 | #define UNUSED(x) ((void)(x))
125 | #define assert(expression) UNUSED(expression)
126 | #endif
127 |
128 | #endif
129 |
--------------------------------------------------------------------------------
/arch/nv/var.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #ifndef VAR_H
26 | #define VAR_H
27 |
28 | #include "mask.h"
29 | #include "symtab.h"
30 |
31 | struct vardata_feature {
32 | char *name;
33 | char *description;
34 | uint32_t *ifmask;
35 | uint32_t *cfmask;
36 | int *implies;
37 | int impliesnum;
38 | int impliesmax;
39 | int *conflicts;
40 | int conflictsnum;
41 | int conflictsmax;
42 | };
43 |
44 | struct vardata_variant {
45 | char *name;
46 | char *description;
47 | uint32_t *fmask;
48 | int *features;
49 | int featuresnum;
50 | int featuresmax;
51 | int varset;
52 | };
53 |
54 | struct vardata_varset {
55 | char *name;
56 | char *description;
57 | };
58 |
59 | struct vardata_mode {
60 | char *name;
61 | char *description;
62 | uint32_t *rfmask;
63 | int *rfeatures;
64 | int rfeaturesnum;
65 | int rfeaturesmax;
66 | int modeset;
67 | };
68 |
69 | struct vardata_modeset {
70 | char *name;
71 | char *description;
72 | int defmode;
73 | };
74 |
75 | struct vardata {
76 | char *name;
77 | struct vardata_feature *features;
78 | int featuresnum;
79 | int featuresmax;
80 | struct vardata_variant *variants;
81 | int variantsnum;
82 | int variantsmax;
83 | struct vardata_varset *varsets;
84 | int varsetsnum;
85 | int varsetsmax;
86 | struct vardata_mode *modes;
87 | int modesnum;
88 | int modesmax;
89 | struct vardata_modeset *modesets;
90 | int modesetsnum;
91 | int modesetsmax;
92 | struct symtab *symtab;
93 | int validated;
94 | };
95 |
96 | enum vardata_symtype {
97 | VARDATA_ST_FEATURE,
98 | VARDATA_ST_VARIANT,
99 | VARDATA_ST_VARSET,
100 | VARDATA_ST_MODE,
101 | VARDATA_ST_MODESET,
102 | };
103 |
104 | struct vardata *vardata_new(const char *name);
105 | void vardata_del(struct vardata *data);
106 | int vardata_add_feature(struct vardata *data, const char *name, const char *description);
107 | int vardata_add_varset(struct vardata *data, const char *name, const char *description);
108 | int vardata_add_variant(struct vardata *data, const char *name, const char *description, int varset);
109 | int vardata_add_modeset(struct vardata *data, const char *name, const char *description);
110 | int vardata_add_mode(struct vardata *data, const char *name, const char *description, int modeset);
111 | void vardata_feature_imply(struct vardata *data, int f1, int f2);
112 | void vardata_feature_conflict(struct vardata *data, int f1, int f2);
113 | void vardata_variant_feature(struct vardata *data, int v, int f);
114 | void vardata_mode_require(struct vardata *data, int m, int f);
115 | int vardata_modeset_def(struct vardata *data, int ms, int m);
116 | int vardata_validate(struct vardata *data);
117 |
118 | struct varinfo {
119 | struct vardata *data;
120 | uint32_t *fmask;
121 | int *variants;
122 | int *modes;
123 | };
124 |
125 | struct varinfo *varinfo_new(struct vardata *data);
126 | void varinfo_del(struct varinfo *info);
127 | int varinfo_set_variant(struct varinfo *info, const char *variant);
128 | int varinfo_set_feature(struct varinfo *info, const char *feature);
129 | int varinfo_set_mode(struct varinfo *info, const char *mode);
130 |
131 | struct varselect {
132 | struct vardata *data;
133 | uint32_t *fmask;
134 | uint32_t *msmask;
135 | uint32_t *mmask;
136 | uint32_t *vsmask;
137 | uint32_t *vmask;
138 | };
139 |
140 | struct varselect *varselect_new(struct vardata *data);
141 | void varselect_del(struct varselect *select);
142 | void varselect_need_feature(struct varselect *select, int f);
143 | void varselect_need_mode(struct varselect *select, int m);
144 | void varselect_need_variant(struct varselect *select, int v);
145 |
146 | int varselect_match(struct varselect *select, struct varinfo *info);
147 |
148 | #endif
149 |
--------------------------------------------------------------------------------
/arch/nv/varinfo.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Marcelina Kościelnicka
3 | * All Rights Reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining a
6 | * copy of this software and associated documentation files (the "Software"),
7 | * to deal in the Software without restriction, including without limitation
8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | * and/or sell copies of the Software, and to permit persons to whom the
10 | * Software is furnished to do so, subject to the following conditions:
11 | *
12 | * The above copyright notice and this permission notice (including the next
13 | * paragraph) shall be included in all copies or substantial portions of the
14 | * Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | * OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | #include "var.h"
26 |
27 | struct varinfo *varinfo_new(struct vardata *data) {
28 | struct varinfo *res = calloc(sizeof *res, 1);
29 | int i;
30 | res->data = data;
31 | res->fmask = mask_new(data->featuresnum);
32 | res->variants = calloc(sizeof *res->variants, data->varsetsnum);
33 | for (i = 0; i < data->varsetsnum; i++)
34 | res->variants[i] = -1;
35 | res->modes = calloc(sizeof *res->modes, data->modesetsnum);
36 | for (i = 0; i < data->modesetsnum; i++)
37 | res->modes[i] = data->modesets[i].defmode;
38 | return res;
39 | }
40 |
41 | void varinfo_del(struct varinfo *info) {
42 | free(info->fmask);
43 | free(info->variants);
44 | free(info->modes);
45 | free(info);
46 | }
47 |
48 | int varinfo_set_variant(struct varinfo *info, const char *variant) {
49 | int data;
50 | if (symtab_get_t(info->data->symtab, variant, VARDATA_ST_VARIANT, &data) == -1) {
51 | fprintf(stderr, "no variant %s\n", variant);
52 | return -1;
53 | }
54 | int varset = info->data->variants[data].varset;
55 | if (info->variants[varset] != -1 && info->variants[varset] != data) {
56 | fprintf(stderr, "a variant from varset %s has already been selected\n", info->data->varsets[varset].name);
57 | return -1;
58 | }
59 | int i;
60 | for (i = 0; i < info->data->featuresnum; i++) {
61 | if (mask_get(info->fmask, i) && mask_intersect(info->data->features[i].cfmask, info->data->variants[data].fmask, info->data->featuresnum) != -1) {
62 | fprintf(stderr, "variant %s conflicts with feature %s\n", info->data->variants[data].name, info->data->features[i].name);
63 | return -1;
64 | }
65 | }
66 | info->variants[varset] = data;
67 | mask_or(info->fmask, info->data->variants[data].fmask, info->data->featuresnum);
68 | return 0;
69 | }
70 |
71 | int varinfo_set_feature(struct varinfo *info, const char *feature) {
72 | int data;
73 | if (symtab_get_t(info->data->symtab, feature, VARDATA_ST_FEATURE, &data) == -1) {
74 | fprintf(stderr, "no feature %s\n", feature);
75 | return -1;
76 | }
77 | int cf;
78 | if ((cf = mask_intersect(info->data->features[data].cfmask, info->fmask, info->data->featuresnum)) != -1) {
79 | fprintf(stderr, "feature %s conflicts with already set feature %s\n", info->data->features[data].name, info->data->features[cf].name);
80 | return -1;
81 | }
82 | mask_or(info->fmask, info->data->features[data].ifmask, info->data->featuresnum);
83 | return 0;
84 | }
85 |
86 | int varinfo_set_mode(struct varinfo *info, const char *mode) {
87 | int data;
88 | if (symtab_get_t(info->data->symtab, mode, VARDATA_ST_MODE, &data) == -1) {
89 | fprintf(stderr, "no mode %s\n", mode);
90 | return -1;
91 | }
92 | int modeset = info->data->modes[data].modeset;
93 | if (info->modes[modeset] != -1 && info->modes[modeset] != data) {
94 | fprintf(stderr, "a mode from modeset %s has already been selected\n", info->data->modesets[modeset].name);
95 | return -1;
96 | }
97 | info->modes[modeset] = data;
98 | /* XXX: validate mode */
99 | return 0;
100 | }
101 |
--------------------------------------------------------------------------------
/arch/pdp11/pdp11.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | char * decodePDP11(unsigned long int address, char *outbuf, int *len, unsigned long int offset0);
4 |
--------------------------------------------------------------------------------
/arch/ppc/ppc_disasm.h:
--------------------------------------------------------------------------------
1 | /* $VER: ppc_disasm.h V1.4 (29.08.2001)
2 | *
3 | * Disassembler module for the PowerPC microprocessor family
4 | * Copyright (c) 1998-2001 Frank Wille
5 | *
6 | * ppc_disasm.c is freeware and may be freely redistributed as long as
7 | * no modifications are made and nothing is charged for it.
8 | * Non-commercial usage is allowed without any restrictions.
9 | * EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
10 | * SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
11 | *
12 | *
13 | * v1.4 (29.08.2001) phx
14 | * AltiVec support.
15 | * v0.1 (23.05.1998) phx
16 | * First version, which implements all PowerPC instructions.
17 | * v0.0 (09.05.1998) phx
18 | * File created.
19 | */
20 |
21 |
22 | #ifndef PPC_DISASM_H
23 | #define PPC_DISASM_H
24 |
25 | #define PPC_GEKKO
26 |
27 | /* version/revision */
28 | #define PPCDISASM_VER 1
29 | #define PPCDISASM_REV 4
30 |
31 |
32 | /* typedefs */
33 | typedef unsigned int ppc_word;
34 | #ifndef NULL
35 | #define NULL (0L)
36 | #endif
37 |
38 |
39 | /* endianess */
40 | #define LITTLEENDIAN
41 | #endif
42 |
43 | #if !defined(BIGENDIAN) && !defined(LITTLEENDIAN)
44 | #error Define either BIGENDIAN or LITTLEENDIAN!
45 | #define LITTLEENDIAN
46 | #endif
47 |
48 |
49 | /* general defines */
50 | #define PPCIDXMASK 0xfc000000
51 | #define PPCIDX2MASK 0x000007fe
52 | #define PPCDMASK 0x03e00000
53 | #define PPCAMASK 0x001f0000
54 | #define PPCBMASK 0x0000f800
55 | #define PPCCMASK 0x000007c0
56 | #define PPCMMASK 0x0000003e
57 | #define PPCCRDMASK 0x03800000
58 | #define PPCCRAMASK 0x001c0000
59 | #define PPCLMASK 0x00600000
60 | #define PPCOE 0x00000400
61 | #define PPCVRC 0x00000400
62 | #define PPCDST 0x02000000
63 | #define PPCSTRM 0x00600000
64 |
65 | #define PPCIDXSH 26
66 | #define PPCDSH 21
67 | #define PPCASH 16
68 | #define PPCBSH 11
69 | #define PPCCSH 6
70 | #define PPCMSH 1
71 | #define PPCCRDSH 23
72 | #define PPCCRASH 18
73 | #define PPCLSH 21
74 | #define PPCIDX2SH 1
75 |
76 | #define PPCGETIDX(x) (((x)&PPCIDXMASK)>>PPCIDXSH)
77 | #define PPCGETD(x) (((x)&PPCDMASK)>>PPCDSH)
78 | #define PPCGETA(x) (((x)&PPCAMASK)>>PPCASH)
79 | #define PPCGETB(x) (((x)&PPCBMASK)>>PPCBSH)
80 | #define PPCGETC(x) (((x)&PPCCMASK)>>PPCCSH)
81 | #define PPCGETM(x) (((x)&PPCMMASK)>>PPCMSH)
82 | #define PPCGETCRD(x) (((x)&PPCCRDMASK)>>PPCCRDSH)
83 | #define PPCGETCRA(x) (((x)&PPCCRAMASK)>>PPCCRASH)
84 | #define PPCGETL(x) (((x)&PPCLMASK)>>PPCLSH)
85 | #define PPCGETIDX2(x) (((x)&PPCIDX2MASK)>>PPCIDX2SH)
86 | #define PPCGETSTRM(x) (((x)&PPCSTRM)>>PPCDSH)
87 |
88 |
89 | #ifdef PPC_GEKKO
90 | #define GEKKOLASMASK 0x0000007e
91 | #define GEKKOWMASK 0x00000400
92 | #define GEKKOIMASK 0x00000380
93 | #define GEKKOW2MASK 0x00008000
94 | #define GEKKOI2MASK 0x00007000
95 | #define GEKKOVMASK 0x00000FFF
96 |
97 | #define GEKKOLASSH 1
98 | #define GEKKOWSH 10
99 | #define GEKKOISH 7
100 | #define GEKKOW2SH 15
101 | #define GEKKOI2SH 12
102 | #define GEKKOVSH 0
103 |
104 | #define GEKKOGETLASID(x) (((x)&GEKKOLASMASK)>>GEKKOLASSH)
105 | #define GEKKOGETW(x) (((x)&GEKKOWMASK)>>GEKKOWSH)
106 | #define GEKKOGETI(x) (((x)&GEKKOIMASK)>>GEKKOISH)
107 | #define GEKKOGETW2(x) (((x)&GEKKOW2MASK)>>GEKKOW2SH)
108 | #define GEKKOGETI2(x) (((x)&GEKKOI2MASK)>>GEKKOI2SH)
109 | #define GEKKOGETV(x) (((x)&GEKKOVMASK)>>GEKKOVSH)
110 | #endif
111 |
112 | /* Disassembler structure, the interface to the application */
113 |
114 | struct DisasmPara_PPC {
115 | ppc_word *instr; /* pointer to instruction to disassemble */
116 | ppc_word *iaddr; /* instr.addr., usually the same as instr */
117 | char *opcode; /* buffer for opcode, min. 10 chars. */
118 | char *operands; /* operand buffer, min. 24 chars. */
119 | /* changed by disassembler: */
120 | unsigned char type; /* type of instruction, see below */
121 | unsigned char flags; /* additional flags */
122 | unsigned short sreg; /* register in load/store instructions */
123 | ppc_word displacement; /* branch- or load/store displacement */
124 | };
125 |
126 | #define PPCINSTR_OTHER 0 /* no additional info for other instr. */
127 | #define PPCINSTR_BRANCH 1 /* branch dest. = PC+displacement */
128 | #define PPCINSTR_LDST 2 /* load/store instruction: displ(sreg) */
129 | #define PPCINSTR_IMM 3 /* 16-bit immediate val. in displacement */
130 |
131 | #define PPCF_ILLEGAL (1<<0) /* illegal PowerPC instruction */
132 | #define PPCF_UNSIGNED (1<<1) /* unsigned immediate instruction */
133 | #define PPCF_SUPER (1<<2) /* supervisor level instruction */
134 | #define PPCF_64 (1<<3) /* 64-bit only instruction */
135 | #define PPCF_ALTIVEC (1<<4) /* AltiVec instruction */
136 |
137 |
138 | extern ppc_word *PPC_Disassemble(struct DisasmPara_PPC *);
139 |
140 | char * decodePPC(unsigned long int start, char *outbuf, int *lendis, unsigned long int offset0);
141 |
142 |
--------------------------------------------------------------------------------
/arch/riscv/rv32i.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | uint32_t extract_bits(uint32_t word, int start, int length) {
5 | return (word >> start) & ((1U << length) - 1);
6 | }
7 |
8 | int32_t sign_extend(uint32_t value, int bits) {
9 | int32_t mask = 1 << (bits - 1);
10 | return (value ^ mask) - mask;
11 | }
12 |
13 | void decode_rv32i(uint32_t instruction) {
14 | uint32_t opcode = extract_bits(instruction, 0, 7);
15 | uint32_t rd = extract_bits(instruction, 7, 5);
16 | uint32_t funct3 = extract_bits(instruction, 12, 3);
17 | uint32_t rs1 = extract_bits(instruction, 15, 5);
18 | uint32_t rs2 = extract_bits(instruction, 20, 5);
19 | uint32_t funct7 = extract_bits(instruction, 25, 7);
20 | int32_t imm_i = sign_extend(extract_bits(instruction, 20, 12), 12);
21 | int32_t imm_s = sign_extend((extract_bits(instruction, 25, 7) << 5) | extract_bits(instruction, 7, 5), 12);
22 | int32_t imm_b = sign_extend((extract_bits(instruction, 31, 1) << 12) |
23 | (extract_bits(instruction, 7, 1) << 11) |
24 | (extract_bits(instruction, 25, 6) << 5) |
25 | (extract_bits(instruction, 8, 4)), 13);
26 | int32_t imm_u = instruction & 0xFFFFF000;
27 | int32_t imm_j = sign_extend((extract_bits(instruction, 31, 1) << 20) |
28 | (extract_bits(instruction, 12, 8) << 12) |
29 | (extract_bits(instruction, 20, 1) << 11) |
30 | (extract_bits(instruction, 21, 10) << 1), 21);
31 |
32 | switch (opcode) {
33 | case 0x33: // R-type
34 | switch (funct3) {
35 | case 0x0: printf(funct7 == 0x20 ? "sub x%d, x%d, x%d\n" : "add x%d, x%d, x%d\n", rd, rs1, rs2); break;
36 | case 0x7: printf("and x%d, x%d, x%d\n", rd, rs1, rs2); break;
37 | case 0x6: printf("or x%d, x%d, x%d\n", rd, rs1, rs2); break;
38 | case 0x4: printf("xor x%d, x%d, x%d\n", rd, rs1, rs2); break;
39 | case 0x1: printf("sll x%d, x%d, x%d\n", rd, rs1, rs2); break;
40 | case 0x5: printf(funct7 == 0x20 ? "sra x%d, x%d, x%d\n" : "srl x%d, x%d, x%d\n", rd, rs1, rs2); break;
41 | }
42 | break;
43 | case 0x13: // I-type
44 | switch (funct3) {
45 | case 0x0: printf("addi x%d, x%d, %d\n", rd, rs1, imm_i); break;
46 | case 0x7: printf("andi x%d, x%d, %d\n", rd, rs1, imm_i); break;
47 | case 0x6: printf("ori x%d, x%d, %d\n", rd, rs1, imm_i); break;
48 | case 0x1: printf("slli x%d, x%d, %d\n", rd, rs1, imm_i & 0x1F); break;
49 | case 0x5: printf((funct7 == 0x20 ? "srai" : "srli"), rd, rs1, imm_i & 0x1F); break;
50 | }
51 | break;
52 | case 0x03: // Load
53 | switch (funct3) {
54 | case 0x2: printf("lw x%d, %d(x%d)\n", rd, imm_i, rs1); break;
55 | }
56 | break;
57 | case 0x23: // S-type
58 | switch (funct3) {
59 | case 0x2: printf("sw x%d, %d(x%d)\n", rs2, imm_s, rs1); break;
60 | }
61 | break;
62 | case 0x63: // B-type
63 | switch (funct3) {
64 | case 0x0: printf("beq x%d, x%d, %d\n", rs1, rs2, imm_b); break;
65 | case 0x1: printf("bne x%d, x%d, %d\n", rs1, rs2, imm_b); break;
66 | }
67 | break;
68 | case 0x37: // U-type
69 | printf("lui x%d, %d\n", rd, imm_u); break;
70 | case 0x6F: // J-type
71 | printf("jal x%d, %d\n", rd, imm_j); break;
72 | case 0x67: // JALR
73 | printf("jalr x%d, %d(x%d)\n", rd, imm_i, rs1); break;
74 | default:
75 | printf("Unknown instruction: 0x%08X\n", instruction);
76 | break;
77 | }
78 | }
79 |
80 | int main() {
81 | uint32_t instruction;
82 | printf("RISC-V (RV32I) Disassembler (c) 2025 Namdak Tonpa.\n");
83 | printf("Enter RV32I instructions in hexadecimal (one per line, Ctrl+D to end):\n");
84 | while (scanf("%x", &instruction) == 1) {
85 | decode_rv32i(instruction);
86 | }
87 |
88 | return 0;
89 | }
90 |
91 |
--------------------------------------------------------------------------------
/arch/sh4/sh4.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "sh4dis.h"
5 | #include "sh4asm_txt_emit.h"
6 |
7 | #define SH4ASM_TXT_LEN 228
8 | static char sh4asm_disas[SH4ASM_TXT_LEN];
9 | unsigned sh4asm_disas_len;
10 | static void clear_asm(void) { sh4asm_disas_len = 0; }
11 | static void neo_asm_emit(char ch) {
12 | if (sh4asm_disas_len < SH4ASM_TXT_LEN)
13 | sh4asm_disas[sh4asm_disas_len++] = ch;
14 | }
15 |
16 | char * decodeSH4(unsigned long int address, char *outbuf, int *lendis, unsigned long int offset0)
17 | {
18 | uint16_t inst16 = (uint16_t)*((unsigned long int *)address);
19 | memset(sh4_buf, 0, sizeof(sh4_buf));
20 | clear_asm();
21 | sh4asm_disas_inst(inst16, neo_asm_emit, 0);
22 | memcpy(outbuf,sh4asm_disas,sh4asm_disas_len);
23 | memcpy(outbuf+sh4asm_disas_len,"\0",1);
24 | *lendis = 2;
25 | }
26 |
--------------------------------------------------------------------------------
/arch/sh4/sh4.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | char * decodeSH4(unsigned long int address, char *outbuf, int *lendis, unsigned long int offset0);
5 |
--------------------------------------------------------------------------------
/arch/x86/alloc.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2019 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * nasmlib.c library routines for the Netwide Assembler
36 | */
37 |
38 | #include "compiler.h"
39 | #include "nasmlib.h"
40 | #include "error.h"
41 | #include "alloc.h"
42 |
43 | size_t _nasm_last_string_size;
44 |
45 | fatal_func nasm_alloc_failed(void)
46 | {
47 | exit(-1);
48 | }
49 |
50 | void *nasm_malloc(size_t size)
51 | {
52 | void *p;
53 |
54 | again:
55 | p = malloc(size);
56 |
57 | if (unlikely(!p)) {
58 | if (!size) {
59 | size = 1;
60 | goto again;
61 | }
62 | nasm_alloc_failed();
63 | }
64 | return p;
65 | }
66 |
67 | void *nasm_calloc(size_t nelem, size_t size)
68 | {
69 | void *p;
70 |
71 | again:
72 | p = calloc(nelem, size);
73 |
74 | if (unlikely(!p)) {
75 | if (!nelem || !size) {
76 | nelem = size = 1;
77 | goto again;
78 | }
79 | nasm_alloc_failed();
80 | }
81 |
82 | return p;
83 | }
84 |
85 | void *nasm_zalloc(size_t size)
86 | {
87 | return nasm_calloc(size, 1);
88 | }
89 |
90 | /*
91 | * Unlike the system realloc, we do *not* allow size == 0 to be
92 | * the equivalent to free(); we guarantee returning a non-NULL pointer.
93 | *
94 | * The check for calling malloc() is theoretically redundant, but be
95 | * paranoid about the system library...
96 | */
97 | void *nasm_realloc(void *q, size_t size)
98 | {
99 | if (unlikely(!size))
100 | size = 1;
101 | q = q ? realloc(q, size) : malloc(size);
102 | return validate_ptr(q);
103 | }
104 |
105 | void nasm_free(void *q)
106 | {
107 | if (q)
108 | free(q);
109 | }
110 |
111 | char *nasm_strdup(const char *s)
112 | {
113 | char *p;
114 | const size_t size = strlen(s) + 1;
115 |
116 | _nasm_last_string_size = size;
117 | p = nasm_malloc(size);
118 | return memcpy(p, s, size);
119 | }
120 |
121 | char *nasm_strndup(const char *s, size_t len)
122 | {
123 | char *p;
124 |
125 | len = strnlen(s, len);
126 | _nasm_last_string_size = len + 1;
127 | p = nasm_malloc(len+1);
128 | p[len] = '\0';
129 | return memcpy(p, s, len);
130 | }
131 |
132 | char *nasm_strcat(const char *one, const char *two)
133 | {
134 | char *rslt;
135 | const size_t l1 = strlen(one);
136 | const size_t s2 = strlen(two) + 1;
137 |
138 | _nasm_last_string_size = l1 + s2;
139 | rslt = nasm_malloc(l1 + s2);
140 | memcpy(rslt, one, l1);
141 | memcpy(rslt + l1, two, s2);
142 | return rslt;
143 | }
144 |
145 | char *nasm_strcatn(const char *str1, ...)
146 | {
147 | va_list ap;
148 | char *rslt; /* Output buffer */
149 | size_t s; /* Total buffer size */
150 | size_t n; /* Number of arguments */
151 | size_t *ltbl; /* Table of lengths */
152 | size_t l, *lp; /* Length for current argument */
153 | const char *p; /* Currently examined argument */
154 | char *q; /* Output pointer */
155 |
156 | n = 0; /* No strings encountered yet */
157 | p = str1;
158 | va_start(ap, str1);
159 | while (p) {
160 | n++;
161 | p = va_arg(ap, const char *);
162 | }
163 | va_end(ap);
164 |
165 | ltbl = nasm_malloc(n * sizeof(size_t));
166 |
167 | s = 1; /* Space for final NULL */
168 | p = str1;
169 | lp = ltbl;
170 | va_start(ap, str1);
171 | while (p) {
172 | *lp++ = l = strlen(p);
173 | s += l;
174 | p = va_arg(ap, const char *);
175 | }
176 | va_end(ap);
177 |
178 | _nasm_last_string_size = s;
179 |
180 | q = rslt = nasm_malloc(s);
181 |
182 | p = str1;
183 | lp = ltbl;
184 | va_start(ap, str1);
185 | while (p) {
186 | l = *lp++;
187 | memcpy(q, p, l);
188 | q += l;
189 | p = va_arg(ap, const char *);
190 | }
191 | va_end(ap);
192 | *q = '\0';
193 |
194 | nasm_free(ltbl);
195 |
196 | return rslt;
197 | }
198 |
--------------------------------------------------------------------------------
/arch/x86/alloc.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | #ifndef NASMLIB_ALLOC_H
35 | #define NASMLIB_ALLOC_H
36 |
37 | #include "compiler.h"
38 |
39 | fatal_func nasm_alloc_failed(void);
40 |
41 | static inline void *validate_ptr(void *p)
42 | {
43 | if (unlikely(!p))
44 | nasm_alloc_failed();
45 | return p;
46 | }
47 |
48 | extern size_t _nasm_last_string_size;
49 |
50 | #endif /* NASMLIB_ALLOC_H */
51 |
--------------------------------------------------------------------------------
/arch/x86/common.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * common.c - code common to nasm and ndisasm
36 | */
37 |
38 | #include "compiler.h"
39 | #include "nasm.h"
40 | #include "nasmlib.h"
41 | #include "insns.h"
42 |
43 | /*
44 | * The current bit size of the CPU
45 | */
46 | int globalbits = 0;
47 | /*
48 | * Common list of prefix names; ideally should be auto-generated
49 | * from tokens.dat
50 | */
51 | const char *prefix_name(int token)
52 | {
53 | static const char *prefix_names[] = {
54 | "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
55 | "rep", "repe", "repne", "repnz", "repz", "times", "wait",
56 | "xacquire", "xrelease", "bnd"
57 | };
58 | unsigned int prefix = token-PREFIX_ENUM_START;
59 |
60 | if (prefix >= ARRAY_SIZE(prefix_names))
61 | return NULL;
62 |
63 | return prefix_names[prefix];
64 | }
65 |
--------------------------------------------------------------------------------
/arch/x86/config/unknown.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * config/unknown.h
36 | *
37 | * Compiler definitions for an unknown compiler. Assume the worst.
38 | */
39 |
40 | #ifndef NASM_CONFIG_UNKNOWN_H
41 | #define NASM_CONFIG_UNKNOWN_H
42 |
43 | #endif /* NASM_CONFIG_UNKNOWN_H */
44 |
--------------------------------------------------------------------------------
/arch/x86/directiv.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is generated from ./asm/directiv.dat
3 | * by perfhash.pl; do not edit.
4 | */
5 |
6 | #ifndef DIRECTIV_H
7 | #define DIRECTIV_H 1
8 |
9 | #include "perfhash.h"
10 |
11 | enum directive {
12 | D_none,
13 | D_unknown,
14 | D_corrupt,
15 | D_ABSOLUTE,
16 | D_BITS,
17 | D_COMMON,
18 | D_CPU,
19 | D_DEBUG,
20 | D_DEFAULT,
21 | D_EXTERN,
22 | D_FLOAT,
23 | D_GLOBAL,
24 | D_STATIC,
25 | D_LIST,
26 | D_SECTION,
27 | D_SEGMENT,
28 | D_WARNING,
29 | D_SECTALIGN,
30 | D_PRAGMA,
31 | D_REQUIRED,
32 | D_EXPORT,
33 | D_GROUP,
34 | D_IMPORT,
35 | D_LIBRARY,
36 | D_MAP,
37 | D_MODULE,
38 | D_ORG,
39 | D_OSABI,
40 | D_SAFESEH,
41 | D_UPPERCASE,
42 | D_PREFIX,
43 | D_SUFFIX,
44 | D_GPREFIX,
45 | D_GSUFFIX,
46 | D_LPREFIX,
47 | D_LSUFFIX,
48 | D_LIMIT,
49 | D_OPTIONS,
50 | D_SUBSECTIONS_VIA_SYMBOLS,
51 | D_NO_DEAD_STRIP,
52 | D_MAXDUMP,
53 | D_NODEPEND,
54 | D_NOSECLABELS
55 | };
56 |
57 | extern const struct perfect_hash directive_hash;
58 | extern const char * const directive_tbl[40];
59 |
60 | static inline enum directive directive_find(const char *str)
61 | {
62 | return perfhash_find(&directive_hash, str);
63 | }
64 |
65 | static inline const char * directive_name(enum directive x)
66 | {
67 | size_t ix = (size_t)x - (3);
68 | if (ix >= 40)
69 | return NULL;
70 | return directive_tbl[ix];
71 | }
72 |
73 | static inline const char * directive_dname(enum directive x)
74 | {
75 | const char *y = directive_name(x);
76 | return y ? y : invalid_enum_str(x);
77 | }
78 |
79 | #endif /* DIRECTIV_H */
80 |
--------------------------------------------------------------------------------
/arch/x86/disasm.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * disasm.h header file for disasm.c
36 | */
37 |
38 | #ifndef NASM_DISASM_H
39 | #define NASM_DISASM_H
40 |
41 | #include "sync.h"
42 | #include "nctype.h"
43 |
44 | #include "iflag.h"
45 |
46 | #define INSN_MAX 32 /* one instruction can't be longer than this */
47 |
48 | int32_t disasm(uint8_t *data, int32_t data_size, char *output, int outbufsize, int segsize,
49 | int64_t offset, int autosync, iflag_t *prefer);
50 | int32_t eatbyte(uint8_t *data, char *output, int outbufsize, int segsize);
51 |
52 | char * decodeEM64T(unsigned long int start, char *outbuf, int *lendis, unsigned long int offset0);
53 |
54 | #endif
55 |
--------------------------------------------------------------------------------
/arch/x86/disp8.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * disp8.c : Contains a common logic for EVEX compressed displacement
36 | */
37 |
38 | #include "disp8.h"
39 |
40 | /*
41 | * Find N value for compressed displacement (disp8 * N)
42 | */
43 | uint8_t get_disp8N(insn *ins)
44 | {
45 | static const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
46 | {{16, 32, 64}, {8, 8, 8}}};
47 | static const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
48 | static const uint8_t dup_n[VLMAX] = {8, 32, 64};
49 |
50 | bool evex_b = (ins->evex_p[2] & EVEX_P2B) >> 4;
51 | enum ttypes tuple = ins->evex_tuple;
52 | enum vectlens vectlen = (ins->evex_p[2] & EVEX_P2LL) >> 5;
53 | bool evex_w = (ins->evex_p[1] & EVEX_P1W) >> 7;
54 | uint8_t n = 0;
55 |
56 | switch(tuple) {
57 | case FV:
58 | n = fv_n[evex_w][evex_b][vectlen];
59 | break;
60 | case HV:
61 | n = hv_n[evex_b][vectlen];
62 | break;
63 |
64 | case FVM:
65 | /* 16, 32, 64 for VL 128, 256, 512 respectively*/
66 | n = 1 << (vectlen + 4);
67 | break;
68 | case T1S8: /* N = 1 */
69 | case T1S16: /* N = 2 */
70 | n = tuple - T1S8 + 1;
71 | break;
72 | case T1S:
73 | /* N = 4 for 32bit, 8 for 64bit */
74 | n = evex_w ? 8 : 4;
75 | break;
76 | case T1F32:
77 | case T1F64:
78 | /* N = 4 for 32bit, 8 for 64bit */
79 | n = (tuple == T1F32 ? 4 : 8);
80 | break;
81 | case T2:
82 | case T4:
83 | case T8:
84 | if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
85 | n = 0;
86 | else
87 | n = 1 << (tuple - T2 + evex_w + 3);
88 | break;
89 | case HVM:
90 | case QVM:
91 | case OVM:
92 | n = 1 << (OVM - tuple + vectlen + 1);
93 | break;
94 | case M128:
95 | n = 16;
96 | break;
97 | case DUP:
98 | n = dup_n[vectlen];
99 | break;
100 |
101 | default:
102 | break;
103 | }
104 |
105 | return n;
106 | }
107 |
108 | /*
109 | * Check if offset is a multiple of N with corresponding tuple type
110 | * if Disp8*N is available, compressed displacement is stored in compdisp
111 | */
112 | bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
113 | {
114 | int32_t off = input->offset;
115 | uint8_t n;
116 | int32_t disp8;
117 |
118 | n = get_disp8N(ins);
119 |
120 | if (n && !(off & (n - 1))) {
121 | disp8 = off / n;
122 | /* if it fits in Disp8 */
123 | if (disp8 >= -128 && disp8 <= 127) {
124 | *compdisp = disp8;
125 | return true;
126 | }
127 | }
128 |
129 | *compdisp = 0;
130 | return false;
131 | }
132 |
--------------------------------------------------------------------------------
/arch/x86/disp8.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * disp8.h header file for disp8.c
36 | */
37 |
38 | #ifndef NASM_DISP8_H
39 | #define NASM_DISP8_H
40 |
41 | #include "nasm.h"
42 |
43 | uint8_t get_disp8N(insn *ins);
44 | bool is_disp8n(operand *input, insn *ins, int8_t *compdisp);
45 | #endif /* NASM_DISP8_H */
46 |
--------------------------------------------------------------------------------
/arch/x86/hashtbl.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * hashtbl.h
36 | *
37 | * Efficient dictionary hash table class.
38 | */
39 |
40 | #ifndef NASM_HASHTBL_H
41 | #define NASM_HASHTBL_H
42 |
43 | #include "nasmlib.h"
44 | #include
45 |
46 | struct hash_node {
47 | uint64_t hash;
48 | const void *key;
49 | size_t keylen;
50 | void *data;
51 | };
52 |
53 | struct hash_table {
54 | struct hash_node *table;
55 | size_t load;
56 | size_t size;
57 | size_t max_load;
58 | };
59 |
60 | struct hash_insert {
61 | struct hash_table *head;
62 | struct hash_node *where;
63 | struct hash_node node;
64 | };
65 |
66 | struct hash_iterator {
67 | const struct hash_table *head;
68 | const struct hash_node *next;
69 | };
70 |
71 | uint64_t crc64(uint64_t crc, const char *string);
72 | uint64_t crc64i(uint64_t crc, const char *string);
73 | uint64_t crc64b(uint64_t crc, const void *data, size_t len);
74 | uint64_t crc64ib(uint64_t crc, const void *data, size_t len);
75 | #define CRC64_INIT UINT64_C(0xffffffffffffffff)
76 |
77 | static inline uint64_t crc64_byte(uint64_t crc, uint8_t v)
78 | {
79 | extern const uint64_t crc64_tab[256];
80 | return crc64_tab[(uint8_t)(v ^ crc)] ^ (crc >> 8);
81 | }
82 |
83 | void **hash_find(struct hash_table *head, const char *string,
84 | struct hash_insert *insert);
85 | void **hash_findb(struct hash_table *head, const void *key, size_t keylen,
86 | struct hash_insert *insert);
87 | void **hash_findi(struct hash_table *head, const char *string,
88 | struct hash_insert *insert);
89 | void **hash_findib(struct hash_table *head, const void *key, size_t keylen,
90 | struct hash_insert *insert);
91 | void **hash_add(struct hash_insert *insert, const void *key, void *data);
92 | static inline void hash_iterator_init(const struct hash_table *head,
93 | struct hash_iterator *iterator)
94 | {
95 | iterator->head = head;
96 | iterator->next = head->table;
97 | }
98 | const struct hash_node *hash_iterate(struct hash_iterator *iterator);
99 |
100 | #define hash_for_each(_head,_it,_np) \
101 | for (hash_iterator_init((_head), &(_it)), (_np) = hash_iterate(&(_it)) ; \
102 | (_np) ; (_np) = hash_iterate(&(_it)))
103 |
104 | void hash_free(struct hash_table *head);
105 | void hash_free_all(struct hash_table *head, bool free_keys);
106 |
107 | #endif /* NASM_HASHTBL_H */
108 |
--------------------------------------------------------------------------------
/arch/x86/iflag.h:
--------------------------------------------------------------------------------
1 | #ifndef NASM_IFLAG_H
2 | #define NASM_IFLAG_H
3 |
4 | #include "compiler.h"
5 |
6 |
7 | #include "iflaggen.h"
8 |
9 | #define IF_GENBIT(bit) (UINT32_C(1) << ((bit) & 31))
10 |
11 | static inline int ifcomp(uint32_t a, uint32_t b)
12 | {
13 | return (a > b) - (a < b);
14 | }
15 |
16 | static inline bool iflag_test(const iflag_t *f, unsigned int bit)
17 | {
18 | return !!(f->field[bit >> 5] & IF_GENBIT(bit));
19 | }
20 |
21 | static inline void iflag_set(iflag_t *f, unsigned int bit)
22 | {
23 | f->field[bit >> 5] |= IF_GENBIT(bit);
24 | }
25 |
26 | static inline void iflag_clear(iflag_t *f, unsigned int bit)
27 | {
28 | f->field[bit >> 5] &= ~IF_GENBIT(bit);
29 | }
30 |
31 | static inline void iflag_clear_all(iflag_t *f)
32 | {
33 | memset(f, 0, sizeof(*f));
34 | }
35 |
36 | static inline void iflag_set_all(iflag_t *f)
37 | {
38 | memset(f, ~0, sizeof(*f));
39 | }
40 |
41 | #define iflag_for_each_field(v) for ((v) = 0; (v) < IF_FIELD_COUNT; (v)++)
42 |
43 | static inline int iflag_cmp(const iflag_t *a, const iflag_t *b)
44 | {
45 | int i;
46 |
47 | /* This is intentionally a reverse loop! */
48 | for (i = IF_FIELD_COUNT-1; i >= 0; i--) {
49 | if (a->field[i] == b->field[i])
50 | continue;
51 |
52 | return ifcomp(a->field[i], b->field[i]);
53 | }
54 |
55 | return 0;
56 | }
57 |
58 | #define IF_GEN_HELPER(name, op) \
59 | static inline iflag_t iflag_##name(const iflag_t *a, const iflag_t *b) \
60 | { \
61 | unsigned int i; \
62 | iflag_t res; \
63 | \
64 | iflag_for_each_field(i) \
65 | res.field[i] = a->field[i] op b->field[i]; \
66 | \
67 | return res; \
68 | }
69 |
70 | IF_GEN_HELPER(xor, ^)
71 |
72 | /* Some helpers which are to work with predefined masks */
73 | #define IF_SMASK (IFM_SB|IFM_SW|IFM_SD|IFM_SQ|IFM_SO|IFM_SY|IFM_SZ|IFM_SIZE|IFM_ANYSIZE)
74 | #define IF_ARMASK (IFM_AR0|IFM_AR1|IFM_AR2|IFM_AR3|IFM_AR4)
75 |
76 | #define _itemp_smask(idx) (insns_flags[(idx)].field[0] & IF_SMASK)
77 | #define _itemp_armask(idx) (insns_flags[(idx)].field[0] & IF_ARMASK)
78 | #define _itemp_arg(idx) ((_itemp_armask(idx) >> IF_AR0) - 1)
79 |
80 | #define itemp_smask(itemp) _itemp_smask((itemp)->iflag_idx)
81 | #define itemp_arg(itemp) _itemp_arg((itemp)->iflag_idx)
82 | #define itemp_armask(itemp) _itemp_armask((itemp)->iflag_idx)
83 |
84 | /*
85 | * IF_ANY is the highest CPU level by definition
86 | */
87 | #define IF_PLEVEL IF_ANY /* Default CPU level */
88 | #define IF_CPU_LEVEL_MASK ((IFM_ANY << 1) - 1)
89 |
90 | static inline int iflag_cmp_cpu(const iflag_t *a, const iflag_t *b)
91 | {
92 | return ifcomp(a->field[IF_CPU_FIELD], b->field[IF_CPU_FIELD]);
93 | }
94 |
95 | static inline uint32_t _iflag_cpu_level(const iflag_t *a)
96 | {
97 | return a->field[IF_CPU_FIELD] & IF_CPU_LEVEL_MASK;
98 | }
99 |
100 | static inline int iflag_cmp_cpu_level(const iflag_t *a, const iflag_t *b)
101 | {
102 | return ifcomp(_iflag_cpu_level(a), _iflag_cpu_level(b));
103 | }
104 |
105 | /* Returns true if the CPU level is at least a certain value */
106 | static inline bool iflag_cpu_level_ok(const iflag_t *a, unsigned int bit)
107 | {
108 | return _iflag_cpu_level(a) >= IF_GENBIT(bit);
109 | }
110 |
111 | static inline void iflag_set_all_features(iflag_t *a)
112 | {
113 | uint32_t *p = &a->field[IF_FEATURE_FIELD];
114 |
115 | memset(p, -1, IF_FEATURE_NFIELDS * sizeof(uint32_t));
116 | }
117 |
118 | static inline void iflag_set_cpu(iflag_t *a, unsigned int cpu)
119 | {
120 | a->field[0] = 0; /* Not applicable to the CPU type */
121 | iflag_set_all_features(a); /* All feature masking bits set for now */
122 | a->field[IF_CPU_FIELD] &= ~IF_CPU_LEVEL_MASK;
123 | iflag_set(a, cpu);
124 | }
125 |
126 | static inline void iflag_set_default_cpu(iflag_t *a)
127 | {
128 | iflag_set_cpu(a, IF_PLEVEL);
129 | }
130 |
131 | static inline iflag_t _iflag_pfmask(const iflag_t *a)
132 | {
133 | iflag_t r;
134 |
135 | iflag_clear_all(&r);
136 |
137 | if (iflag_test(a, IF_CYRIX))
138 | iflag_set(&r, IF_CYRIX);
139 | if (iflag_test(a, IF_AMD))
140 | iflag_set(&r, IF_AMD);
141 |
142 | return r;
143 | }
144 |
145 | #define iflag_pfmask(itemp) _iflag_pfmask(&insns_flags[(itemp)->iflag_idx])
146 |
147 | #endif /* NASM_IFLAG_H */
148 |
--------------------------------------------------------------------------------
/arch/x86/ilog2.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | #ifndef ILOG2_H
35 | #define ILOG2_H
36 |
37 | #include "compiler.h"
38 |
39 | extern inline unsigned int ilog2_32(uint32_t v)
40 | {
41 | unsigned int n;
42 |
43 | __asm__("bsrl %1,%0"
44 | : "=r" (n)
45 | : "rm" (v), "0" (0));
46 | return n;
47 | }
48 |
49 |
50 | extern inline unsigned int ilog2_64(uint64_t v)
51 | {
52 | uint64_t n;
53 |
54 | __asm__("bsrq %1,%0"
55 | : "=r" (n)
56 | : "rm" (v), "0" (UINT64_C(0)));
57 | return n;
58 | }
59 |
60 | #endif /* ILOG2_H */
61 |
--------------------------------------------------------------------------------
/arch/x86/insns.h:
--------------------------------------------------------------------------------
1 | /* insns.h header file for insns.c
2 | *
3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 | * Julian Hall. All rights reserved. The software is
5 | * redistributable under the license given in the file "LICENSE"
6 | * distributed in the NASM archive.
7 | */
8 |
9 | #ifndef NASM_INSNS_H
10 | #define NASM_INSNS_H
11 |
12 | #include "nasm.h"
13 | #include "tokens.h"
14 | #include "iflag.h"
15 |
16 | /* if changed, ITEMPLATE_END should be also changed accordingly */
17 | struct itemplate {
18 | enum opcode opcode; /* the token, passed from "parser.c" */
19 | int operands; /* number of operands */
20 | opflags_t opd[MAX_OPERANDS]; /* bit flags for operand types */
21 | decoflags_t deco[MAX_OPERANDS]; /* bit flags for operand decorators */
22 | const uint8_t *code; /* the code it assembles to */
23 | uint32_t iflag_idx; /* some flags referenced by index */
24 | };
25 |
26 | /* Use this helper to test instruction template flags */
27 | static inline bool itemp_has(const struct itemplate *itemp, unsigned int bit)
28 | {
29 | return iflag_test(&insns_flags[itemp->iflag_idx], bit);
30 | }
31 |
32 | /* Disassembler table structure */
33 |
34 | /*
35 | * If n == -1, then p points to another table of 256
36 | * struct disasm_index, otherwise p points to a list of n
37 | * struct itemplates to consider.
38 | */
39 | struct disasm_index {
40 | const void *p;
41 | int n;
42 | };
43 |
44 | /* Tables for the assembler and disassembler, respectively */
45 | extern const struct itemplate * const nasm_instructions[];
46 | extern const struct disasm_index itable[256];
47 | extern const struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4];
48 |
49 | /* Common table for the byte codes */
50 | extern const uint8_t nasm_bytecodes[];
51 |
52 | /*
53 | * this define is used to signify the end of an itemplate
54 | */
55 | #define ITEMPLATE_END {I_none,0,{0,},{0,},NULL,0}
56 |
57 | /*
58 | * Pseudo-op tests
59 | */
60 | /* DB-type instruction (DB, DW, ...) */
61 | static inline bool const_func opcode_is_db(enum opcode opcode)
62 | {
63 | return opcode >= I_DB && opcode < I_RESB;
64 | }
65 |
66 | /* RESB-type instruction (RESB, RESW, ...) */
67 | static inline bool const_func opcode_is_resb(enum opcode opcode)
68 | {
69 | return opcode >= I_RESB && opcode < I_INCBIN;
70 | }
71 |
72 | /* Width of Dx and RESx instructions */
73 |
74 | /*
75 | * initialized data bytes length from opcode
76 | */
77 | static inline int const_func db_bytes(enum opcode opcode)
78 | {
79 | switch (opcode) {
80 | case I_DB:
81 | return 1;
82 | case I_DW:
83 | return 2;
84 | case I_DD:
85 | return 4;
86 | case I_DQ:
87 | return 8;
88 | case I_DT:
89 | return 10;
90 | case I_DO:
91 | return 16;
92 | case I_DY:
93 | return 32;
94 | case I_DZ:
95 | return 64;
96 | case I_none:
97 | return -1;
98 | default:
99 | return 0;
100 | }
101 | }
102 |
103 | /*
104 | * Uninitialized data bytes length from opcode
105 | */
106 | static inline int const_func resb_bytes(enum opcode opcode)
107 | {
108 | switch (opcode) {
109 | case I_RESB:
110 | return 1;
111 | case I_RESW:
112 | return 2;
113 | case I_RESD:
114 | return 4;
115 | case I_RESQ:
116 | return 8;
117 | case I_REST:
118 | return 10;
119 | case I_RESO:
120 | return 16;
121 | case I_RESY:
122 | return 32;
123 | case I_RESZ:
124 | return 64;
125 | case I_none:
126 | return -1;
127 | default:
128 | return 0;
129 | }
130 | }
131 |
132 | #endif /* NASM_INSNS_H */
133 |
--------------------------------------------------------------------------------
/arch/x86/labels.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * labels.h header file for labels.c
36 | */
37 |
38 | #ifndef LABELS_H
39 | #define LABELS_H
40 |
41 | #include "compiler.h"
42 | #include
43 |
44 | enum mangle_index {
45 | LM_LPREFIX, /* Local variable prefix */
46 | LM_LSUFFIX, /* Local variable suffix */
47 | LM_GPREFIX, /* Global variable prefix */
48 | LM_GSUFFIX /* GLobal variable suffix */
49 | };
50 |
51 | enum label_type {
52 | LBL_none = -1, /* No label */
53 | LBL_LOCAL = 0, /* Must be zero */
54 | LBL_STATIC,
55 | LBL_GLOBAL,
56 | LBL_EXTERN,
57 | LBL_REQUIRED, /* Like extern but emit even if unused */
58 | LBL_COMMON,
59 | LBL_SPECIAL, /* Magic symbols like ..start */
60 | LBL_BACKEND /* Backend-defined symbols like ..got */
61 | };
62 |
63 | enum label_type lookup_label(const char *label, int32_t *segment, int64_t *offset);
64 | static inline bool is_extern(enum label_type type)
65 | {
66 | return type == LBL_EXTERN || type == LBL_REQUIRED;
67 | }
68 | void define_label(const char *label, int32_t segment, int64_t offset,
69 | bool normal);
70 | void backend_label(const char *label, int32_t segment, int64_t offset);
71 | bool declare_label(const char *label, enum label_type type,
72 | const char *special);
73 | void set_label_mangle(enum mangle_index which, const char *what);
74 | int init_labels(void);
75 | void cleanup_labels(void);
76 | const char *local_scope(const char *label);
77 |
78 | extern uint64_t global_offset_changed;
79 |
80 | #endif /* LABELS_H */
81 |
--------------------------------------------------------------------------------
/arch/x86/nasmint.h:
--------------------------------------------------------------------------------
1 | /*
2 | * nasmint.h
3 | *
4 | * Small ersatz subset of , deriving the types from
5 | * .
6 | *
7 | * Important: the preprocessor may truncate numbers too large for it.
8 | * Therefore, test the signed types only ... truncation won't generate
9 | * a 01111111... bit pattern.
10 | */
11 |
12 | #ifndef NASM_NASMINT_H
13 | #define NASM_NASMINT_H
14 |
15 | #include
16 |
17 | /*** 64-bit type: __int64, long or long long ***/
18 |
19 | /* Some old versions of gcc omit LLONG_MAX */
20 | #ifndef LLONG_MAX
21 | # ifdef __LONG_LONG_MAX__
22 | # define LLONG_MAX __LONG_LONG_MAX__
23 | # else
24 | # define LLONG_MAX 0 /* Assume long long is unusable */
25 | # endif
26 | #endif
27 |
28 | #ifndef _I64_MAX
29 | # ifdef _MSC_VER
30 | # define _I64_MAX 9223372036854775807
31 | # else
32 | # define _I64_MAX 0
33 | # endif
34 | #endif
35 |
36 | #if _I64_MAX == 9223372036854775807
37 |
38 | /* Windows-based compiler: use __int64 */
39 | typedef signed __int64 int64_t;
40 | typedef unsigned __int64 uint64_t;
41 | #define _scn64 "I64"
42 | #define _pri64 "I64"
43 | #define INT64_C(x) x ## i64
44 | #define UINT64_C(x) x ## ui64
45 |
46 | #elif LONG_MAX == 9223372036854775807L
47 |
48 | /* long is 64 bits */
49 | //typedef signed long int64_t;
50 | //typedef unsigned long uint64_t;
51 | #define _scn64 "l"
52 | #define _pri64 "l"
53 | #define INT64_C(x) x ## L
54 | #define UINT64_C(x) x ## UL
55 |
56 | #elif LLONG_MAX == 9223372036854775807LL
57 |
58 | /* long long is 64 bits */
59 | typedef signed long long int64_t;
60 | typedef unsigned long long uint64_t;
61 | #define _scn64 "ll"
62 | #define _pri64 "ll"
63 | #define INT64_C(x) x ## LL
64 | #define UINT64_C(x) x ## ULL
65 |
66 | #else
67 |
68 | #error "Neither long nor long long is 64 bits in size"
69 |
70 | #endif
71 |
72 | /*** 32-bit type: int or long ***/
73 |
74 | #if INT_MAX == 2147483647
75 |
76 | /* int is 32 bits */
77 | typedef signed int int32_t;
78 | typedef unsigned int uint32_t;
79 | #define _scn32 ""
80 | #define _pri32 ""
81 | #define INT32_C(x) x
82 | #define UINT32_C(x) x ## U
83 |
84 | #elif LONG_MAX == 2147483647L
85 |
86 | /* long is 32 bits */
87 | typedef signed long int32_t;
88 | typedef unsigned long uint32_t;
89 | #define _scn32 "l"
90 | #define _pri32 "l"
91 | #define INT32_C(x) x ## L
92 | #define UINT32_C(x) x ## UL
93 |
94 | #else
95 |
96 | #error "Neither int nor long is 32 bits in size"
97 |
98 | #endif
99 |
100 | /*** 16-bit size: int or short ***/
101 |
102 | #if INT_MAX == 32767
103 |
104 | /* int is 16 bits */
105 | typedef signed int int16_t;
106 | typedef unsigned int uint16_t;
107 | #define _scn16 ""
108 | #define _pri16 ""
109 | #define INT16_C(x) x
110 | #define UINT16_C(x) x ## U
111 |
112 | #elif SHRT_MAX == 32767
113 |
114 | /* short is 16 bits */
115 | typedef signed short int16_t;
116 | typedef unsigned short uint16_t;
117 | #define _scn16 "h"
118 | #define _pri16 ""
119 | #define INT16_C(x) x
120 | #define UINT16_C(x) x ## U
121 |
122 | #else
123 |
124 | #error "Neither short nor int is 16 bits in size"
125 |
126 | #endif
127 |
128 | /*** 8-bit size: char ***/
129 |
130 | #if SCHAR_MAX == 127
131 |
132 | /* char is 8 bits */
133 | typedef signed char int8_t;
134 | typedef unsigned char uint8_t;
135 | #define _scn8 "hh"
136 | #define _pri8 ""
137 | #define INT8_C(x) x
138 | #define UINT8_C(x) x ## U
139 |
140 | #else
141 |
142 | #error "char is not 8 bits in size"
143 |
144 | #endif
145 |
146 | /* The rest of this is common to all models */
147 |
148 | #define PRId8 _pri8 "d"
149 | #define PRId16 _pri16 "d"
150 | #define PRId32 _pri32 "d"
151 | #define PRId64 _pri64 "d"
152 |
153 | #define PRIi8 _pri8 "i"
154 | #define PRIi16 _pri16 "i"
155 | #define PRIi32 _pri32 "i"
156 | #define PRIi64 _pri64 "i"
157 |
158 | #define PRIo8 _pri8 "o"
159 | #define PRIo16 _pri16 "o"
160 | #define PRIo32 _pri32 "o"
161 | #define PRIo64 _pri64 "o"
162 |
163 | #define PRIu8 _pri8 "u"
164 | #define PRIu16 _pri16 "u"
165 | #define PRIu32 _pri32 "u"
166 | #define PRIu64 _pri64 "u"
167 |
168 | #define PRIx8 _pri8 "x"
169 | #define PRIx16 _pri16 "x"
170 | #define PRIx32 _pri32 "x"
171 | #define PRIx64 _pri64 "x"
172 |
173 | #define PRIX8 _pri8 "X"
174 | #define PRIX16 _pri16 "X"
175 | #define PRIX32 _pri32 "X"
176 | #define PRIX64 _pri64 "X"
177 |
178 | #define SCNd8 _scn8 "d"
179 | #define SCNd16 _scn16 "d"
180 | #define SCNd32 _scn32 "d"
181 | #define SCNd64 _scn64 "d"
182 |
183 | #define SCNi8 _scn8 "i"
184 | #define SCNi16 _scn16 "i"
185 | #define SCNi32 _scn32 "i"
186 | #define SCNi64 _scn64 "i"
187 |
188 | #define SCNo8 _scn8 "o"
189 | #define SCNo16 _scn16 "o"
190 | #define SCNo32 _scn32 "o"
191 | #define SCNo64 _scn64 "o"
192 |
193 | #define SCNu8 _scn8 "u"
194 | #define SCNu16 _scn16 "u"
195 | #define SCNu32 _scn32 "u"
196 | #define SCNu64 _scn64 "u"
197 |
198 | #define SCNx8 _scn8 "x"
199 | #define SCNx16 _scn16 "x"
200 | #define SCNx32 _scn32 "x"
201 | #define SCNx64 _scn64 "x"
202 |
203 | #define INT8_MIN INT8_C(-128)
204 | #define INT8_MAX INT8_C(127)
205 | #define UINT8_MAX UINT8_C(255)
206 |
207 | #define INT16_MIN INT16_C(-32768)
208 | #define INT16_MAX INT16_C(32767)
209 | #define UINT16_MAX UINT16_C(65535)
210 |
211 | #define INT32_MIN INT32_C(-2147483648)
212 | #define INT32_MAX INT32_C(2147483647)
213 | #define UINT32_MAX UINT32_C(4294967295)
214 |
215 | #define INT64_MIN INT64_C(-9223372036854775808)
216 | #define INT64_MAX INT64_C(9223372036854775807)
217 | #define UINT64_MAX UINT64_C(18446744073709551615)
218 |
219 | #endif /* NASM_NASMINT_H */
220 |
--------------------------------------------------------------------------------
/arch/x86/nctype.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | #include "nctype.h"
35 | #include
36 |
37 | /*
38 | * Table of tolower() results. This avoids function calls
39 | * on some platforms.
40 | */
41 | unsigned char nasm_tolower_tab[256];
42 |
43 | static void tolower_tab_init(void)
44 | {
45 | int i;
46 |
47 | for (i = 0; i < 256; i++)
48 | nasm_tolower_tab[i] = tolower(i);
49 | }
50 |
51 | /*
52 | * Table of character type flags; some are simply ,
53 | * some are NASM-specific.
54 | */
55 |
56 | uint16_t nasm_ctype_tab[256];
57 |
58 | #if !defined(HAVE_ISCNTRL) && !defined(iscntrl)
59 | # define iscntrl(x) ((x) < 32)
60 | #endif
61 | #if !defined(HAVE_ISASCII) && !defined(isascii)
62 | # define isascii(x) ((x) < 128)
63 | #endif
64 |
65 | static void ctype_tab_init(void)
66 | {
67 | int i;
68 |
69 | for (i = 0; i < 256; i++) {
70 | enum nasm_ctype ct = 0;
71 |
72 | if (iscntrl(i))
73 | ct |= NCT_CTRL;
74 |
75 | if (isascii(i))
76 | ct |= NCT_ASCII;
77 |
78 | if (isspace(i) && i != '\n')
79 | ct |= NCT_SPACE;
80 |
81 | if (isalpha(i)) {
82 | ct |= (nasm_tolower(i) == i) ? NCT_LOWER : NCT_UPPER;
83 | ct |= NCT_ID|NCT_IDSTART;
84 | }
85 |
86 | if (isdigit(i))
87 | ct |= NCT_DIGIT|NCT_ID;
88 |
89 | if (isxdigit(i))
90 | ct |= NCT_HEX;
91 |
92 | /* Non-ASCII character, but no ctype returned (e.g. Unicode) */
93 | if (!ct && !ispunct(i))
94 | ct |= NCT_ID|NCT_IDSTART;
95 |
96 | nasm_ctype_tab[i] = ct;
97 | }
98 |
99 | nasm_ctype_tab['-'] |= NCT_MINUS;
100 | nasm_ctype_tab['$'] |= NCT_DOLLAR|NCT_ID;
101 | nasm_ctype_tab['_'] |= NCT_UNDER|NCT_ID|NCT_IDSTART;
102 | nasm_ctype_tab['.'] |= NCT_ID|NCT_IDSTART;
103 | nasm_ctype_tab['@'] |= NCT_ID|NCT_IDSTART;
104 | nasm_ctype_tab['?'] |= NCT_ID|NCT_IDSTART;
105 | nasm_ctype_tab['#'] |= NCT_ID;
106 | nasm_ctype_tab['~'] |= NCT_ID;
107 | nasm_ctype_tab['\''] |= NCT_QUOTE;
108 | nasm_ctype_tab['\"'] |= NCT_QUOTE;
109 | nasm_ctype_tab['`'] |= NCT_QUOTE;
110 | }
111 |
112 | void nasm_ctype_init(void)
113 | {
114 | tolower_tab_init();
115 | ctype_tab_init();
116 | }
117 |
--------------------------------------------------------------------------------
/arch/x86/nctype.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * ctype-like functions specific to NASM
36 | */
37 | #ifndef NASM_NCTYPE_H
38 | #define NASM_NCTYPE_H
39 |
40 | #include "compiler.h"
41 |
42 | void nasm_ctype_init(void);
43 |
44 | extern unsigned char nasm_tolower_tab[256];
45 | static inline char nasm_tolower(char x)
46 | {
47 | return nasm_tolower_tab[(unsigned char)x];
48 | }
49 |
50 | /*
51 | * NASM ctype table
52 | */
53 | enum nasm_ctype {
54 | NCT_CTRL = 0x0001,
55 | NCT_SPACE = 0x0002,
56 | NCT_ASCII = 0x0004,
57 | NCT_LOWER = 0x0008, /* isalpha(x) && tolower(x) == x */
58 | NCT_UPPER = 0x0010, /* isalpha(x) && tolower(x) != x */
59 | NCT_DIGIT = 0x0020,
60 | NCT_HEX = 0x0040,
61 | NCT_ID = 0x0080,
62 | NCT_IDSTART = 0x0100,
63 | NCT_MINUS = 0x0200, /* - */
64 | NCT_DOLLAR = 0x0400, /* $ */
65 | NCT_UNDER = 0x0800, /* _ */
66 | NCT_QUOTE = 0x1000 /* " ' ` */
67 | };
68 |
69 | extern uint16_t nasm_ctype_tab[256];
70 | static inline bool nasm_ctype(unsigned char x, enum nasm_ctype mask)
71 | {
72 | return (nasm_ctype_tab[x] & mask) != 0;
73 | }
74 |
75 | static inline bool nasm_isspace(char x)
76 | {
77 | return nasm_ctype(x, NCT_SPACE);
78 | }
79 |
80 | static inline bool nasm_isalpha(char x)
81 | {
82 | return nasm_ctype(x, NCT_LOWER|NCT_UPPER);
83 | }
84 |
85 | static inline bool nasm_isdigit(char x)
86 | {
87 | return nasm_ctype(x, NCT_DIGIT);
88 | }
89 | static inline bool nasm_isalnum(char x)
90 | {
91 | return nasm_ctype(x, NCT_LOWER|NCT_UPPER|NCT_DIGIT);
92 | }
93 | static inline bool nasm_isxdigit(char x)
94 | {
95 | return nasm_ctype(x, NCT_HEX);
96 | }
97 | static inline bool nasm_isidstart(char x)
98 | {
99 | return nasm_ctype(x, NCT_IDSTART);
100 | }
101 | static inline bool nasm_isidchar(char x)
102 | {
103 | return nasm_ctype(x, NCT_ID);
104 | }
105 | static inline bool nasm_isbrcchar(char x)
106 | {
107 | return nasm_ctype(x, NCT_ID|NCT_MINUS);
108 | }
109 | static inline bool nasm_isnumstart(char x)
110 | {
111 | return nasm_ctype(x, NCT_DIGIT|NCT_DOLLAR);
112 | }
113 | static inline bool nasm_isnumchar(char x)
114 | {
115 | return nasm_ctype(x, NCT_DIGIT|NCT_LOWER|NCT_UPPER|NCT_UNDER);
116 | }
117 | static inline bool nasm_isquote(char x)
118 | {
119 | return nasm_ctype(x, NCT_QUOTE);
120 | }
121 |
122 | static inline void nasm_ctype_tasm_mode(void)
123 | {
124 | /* No differences at the present moment */
125 | }
126 |
127 | #endif /* NASM_NCTYPE_H */
128 |
--------------------------------------------------------------------------------
/arch/x86/perfhash.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 2017 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | #ifndef PERFHASH_H
35 | #define PERFHASH_H 1
36 |
37 | #include "compiler.h"
38 | #include "nasmlib.h" /* For invalid_enum_str() */
39 |
40 | struct perfect_hash {
41 | uint64_t crcinit;
42 | uint32_t hashmask;
43 | uint32_t tbllen;
44 | int tbloffs;
45 | int errval;
46 | const int16_t *hashvals;
47 | const char * const *strings;
48 | };
49 |
50 | int perfhash_find(const struct perfect_hash *, const char *);
51 |
52 | #endif /* PERFHASH_H */
53 |
--------------------------------------------------------------------------------
/arch/x86/preproc.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * preproc.h header file for preproc.c
36 | */
37 |
38 | #ifndef NASM_PREPROC_H
39 | #define NASM_PREPROC_H
40 |
41 | #include "nasmlib.h"
42 | #include "pptok.h"
43 |
44 | extern const char * const pp_directives[];
45 | extern const uint8_t pp_directives_len[];
46 | extern bool pp_noline;
47 |
48 | /* Pointer to a macro chain */
49 | typedef const unsigned char macros_t;
50 |
51 | enum preproc_token pp_token_hash(const char *token);
52 |
53 | /* Opens an include file or input file. This uses the include path. */
54 | FILE *pp_input_fopen(const char *filename, enum file_flags mode);
55 |
56 | #endif
57 |
--------------------------------------------------------------------------------
/arch/x86/readnum.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * nasmlib.c library routines for the Netwide Assembler
36 | */
37 |
38 | #include "compiler.h"
39 |
40 | #include "nctype.h"
41 |
42 | #include "nasmlib.h"
43 | #include "error.h"
44 | #include "nasm.h" /* For globalbits */
45 |
46 | #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
47 |
48 | static int radix_letter(char c)
49 | {
50 | switch (c) {
51 | case 'b': case 'B':
52 | case 'y': case 'Y':
53 | return 2; /* Binary */
54 | case 'o': case 'O':
55 | case 'q': case 'Q':
56 | return 8; /* Octal */
57 | case 'h': case 'H':
58 | case 'x': case 'X':
59 | return 16; /* Hexadecimal */
60 | case 'd': case 'D':
61 | case 't': case 'T':
62 | return 10; /* Decimal */
63 | default:
64 | return 0; /* Not a known radix letter */
65 | }
66 | }
67 |
68 | int64_t readnum(const char *str, bool *error)
69 | {
70 | const char *r = str, *q;
71 | int32_t pradix, sradix, radix;
72 | int plen, slen, len;
73 | uint64_t result, checklimit;
74 | int digit, last;
75 | bool warn = false;
76 | int sign = 1;
77 |
78 | *error = false;
79 |
80 | while (nasm_isspace(*r))
81 | r++; /* find start of number */
82 |
83 | /*
84 | * If the number came from make_tok_num (as a result of an %assign), it
85 | * might have a '-' built into it (rather than in a preceeding token).
86 | */
87 | if (*r == '-') {
88 | r++;
89 | sign = -1;
90 | }
91 |
92 | q = r;
93 |
94 | while (lib_isnumchar(*q))
95 | q++; /* find end of number */
96 |
97 | len = q-r;
98 | if (!len) {
99 | /* Not numeric */
100 | *error = true;
101 | return 0;
102 | }
103 |
104 | /*
105 | * Handle radix formats:
106 | *
107 | * 0
108 | * $ (hexadecimal)
109 | *
110 | */
111 | pradix = sradix = 0;
112 | plen = slen = 0;
113 |
114 | if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
115 | plen = 2;
116 | else if (len > 1 && *r == '$')
117 | pradix = 16, plen = 1;
118 |
119 | if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
120 | slen = 1;
121 |
122 | if (pradix > sradix) {
123 | radix = pradix;
124 | r += plen;
125 | } else if (sradix > pradix) {
126 | radix = sradix;
127 | q -= slen;
128 | } else {
129 | /* Either decimal, or invalid -- if invalid, we'll trip up
130 | further down. */
131 | radix = 10;
132 | }
133 |
134 | /*
135 | * `checklimit' must be 2**64 / radix. We can't do that in
136 | * 64-bit arithmetic, which we're (probably) using, so we
137 | * cheat: since we know that all radices we use are even, we
138 | * can divide 2**63 by radix/2 instead.
139 | */
140 | checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
141 |
142 | /*
143 | * Calculate the highest allowable value for the last digit of a
144 | * 64-bit constant... in radix 10, it is 6, otherwise it is 0
145 | */
146 | last = (radix == 10 ? 6 : 0);
147 |
148 | result = 0;
149 | while (*r && r < q) {
150 | if (*r != '_') {
151 | if (*r < '0' || (*r > '9' && *r < 'A')
152 | || (digit = numvalue(*r)) >= radix) {
153 | *error = true;
154 | return 0;
155 | }
156 | if (result > checklimit ||
157 | (result == checklimit && digit >= last)) {
158 | warn = true;
159 | }
160 |
161 | result = radix * result + digit;
162 | }
163 | r++;
164 | }
165 |
166 | if (warn) {
167 | /*!
168 | *!number-overflow [on] numeric constant does not fit
169 | *! covers warnings about numeric constants which
170 | *! don't fit in 64 bits.
171 | */
172 | nasm_warn(WARN_NUMBER_OVERFLOW,
173 | "numeric constant %s does not fit in 64 bits",
174 | str);
175 | }
176 |
177 | return result * sign;
178 | }
179 |
--------------------------------------------------------------------------------
/arch/x86/regdis.c:
--------------------------------------------------------------------------------
1 | /* automatically generated from ./x86/regs.dat - do not edit */
2 |
3 | #include "regdis.h"
4 |
5 | const enum reg_enum nasm_rd_bndreg [ 4] = {R_BND0,R_BND1,R_BND2,R_BND3};
6 | const enum reg_enum nasm_rd_creg [16] = {R_CR0,R_CR1,R_CR2,R_CR3,R_CR4,R_CR5,R_CR6,R_CR7,R_CR8,R_CR9,R_CR10,R_CR11,R_CR12,R_CR13,R_CR14,R_CR15};
7 | const enum reg_enum nasm_rd_dreg [16] = {R_DR0,R_DR1,R_DR2,R_DR3,R_DR4,R_DR5,R_DR6,R_DR7,R_DR8,R_DR9,R_DR10,R_DR11,R_DR12,R_DR13,R_DR14,R_DR15};
8 | const enum reg_enum nasm_rd_fpureg [ 8] = {R_ST0,R_ST1,R_ST2,R_ST3,R_ST4,R_ST5,R_ST6,R_ST7};
9 | const enum reg_enum nasm_rd_mmxreg [ 8] = {R_MM0,R_MM1,R_MM2,R_MM3,R_MM4,R_MM5,R_MM6,R_MM7};
10 | const enum reg_enum nasm_rd_opmaskreg[ 8] = {R_K0,R_K1,R_K2,R_K3,R_K4,R_K5,R_K6,R_K7};
11 | const enum reg_enum nasm_rd_reg16 [16] = {R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,R_R8W,R_R9W,R_R10W,R_R11W,R_R12W,R_R13W,R_R14W,R_R15W};
12 | const enum reg_enum nasm_rd_reg32 [16] = {R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,R_R8D,R_R9D,R_R10D,R_R11D,R_R12D,R_R13D,R_R14D,R_R15D};
13 | const enum reg_enum nasm_rd_reg64 [16] = {R_RAX,R_RCX,R_RDX,R_RBX,R_RSP,R_RBP,R_RSI,R_RDI,R_R8,R_R9,R_R10,R_R11,R_R12,R_R13,R_R14,R_R15};
14 | const enum reg_enum nasm_rd_reg8 [ 8] = {R_AL,R_CL,R_DL,R_BL,R_AH,R_CH,R_DH,R_BH};
15 | const enum reg_enum nasm_rd_reg8_rex[16] = {R_AL,R_CL,R_DL,R_BL,R_SPL,R_BPL,R_SIL,R_DIL,R_R8B,R_R9B,R_R10B,R_R11B,R_R12B,R_R13B,R_R14B,R_R15B};
16 | const enum reg_enum nasm_rd_sreg [ 8] = {R_ES,R_CS,R_SS,R_DS,R_FS,R_GS,R_SEGR6,R_SEGR7};
17 | const enum reg_enum nasm_rd_treg [ 8] = {R_TR0,R_TR1,R_TR2,R_TR3,R_TR4,R_TR5,R_TR6,R_TR7};
18 | const enum reg_enum nasm_rd_xmmreg [32] = {R_XMM0,R_XMM1,R_XMM2,R_XMM3,R_XMM4,R_XMM5,R_XMM6,R_XMM7,R_XMM8,R_XMM9,R_XMM10,R_XMM11,R_XMM12,R_XMM13,R_XMM14,R_XMM15,R_XMM16,R_XMM17,R_XMM18,R_XMM19,R_XMM20,R_XMM21,R_XMM22,R_XMM23,R_XMM24,R_XMM25,R_XMM26,R_XMM27,R_XMM28,R_XMM29,R_XMM30,R_XMM31};
19 | const enum reg_enum nasm_rd_ymmreg [32] = {R_YMM0,R_YMM1,R_YMM2,R_YMM3,R_YMM4,R_YMM5,R_YMM6,R_YMM7,R_YMM8,R_YMM9,R_YMM10,R_YMM11,R_YMM12,R_YMM13,R_YMM14,R_YMM15,R_YMM16,R_YMM17,R_YMM18,R_YMM19,R_YMM20,R_YMM21,R_YMM22,R_YMM23,R_YMM24,R_YMM25,R_YMM26,R_YMM27,R_YMM28,R_YMM29,R_YMM30,R_YMM31};
20 | const enum reg_enum nasm_rd_zmmreg [32] = {R_ZMM0,R_ZMM1,R_ZMM2,R_ZMM3,R_ZMM4,R_ZMM5,R_ZMM6,R_ZMM7,R_ZMM8,R_ZMM9,R_ZMM10,R_ZMM11,R_ZMM12,R_ZMM13,R_ZMM14,R_ZMM15,R_ZMM16,R_ZMM17,R_ZMM18,R_ZMM19,R_ZMM20,R_ZMM21,R_ZMM22,R_ZMM23,R_ZMM24,R_ZMM25,R_ZMM26,R_ZMM27,R_ZMM28,R_ZMM29,R_ZMM30,R_ZMM31};
21 |
--------------------------------------------------------------------------------
/arch/x86/regdis.h:
--------------------------------------------------------------------------------
1 | /* automatically generated from ./x86/regs.dat - do not edit */
2 |
3 | #ifndef NASM_REGDIS_H
4 | #define NASM_REGDIS_H
5 |
6 | #include "regs.h"
7 |
8 | extern const enum reg_enum nasm_rd_bndreg [ 4];
9 | extern const enum reg_enum nasm_rd_creg [16];
10 | extern const enum reg_enum nasm_rd_dreg [16];
11 | extern const enum reg_enum nasm_rd_fpureg [ 8];
12 | extern const enum reg_enum nasm_rd_mmxreg [ 8];
13 | extern const enum reg_enum nasm_rd_opmaskreg[ 8];
14 | extern const enum reg_enum nasm_rd_reg16 [16];
15 | extern const enum reg_enum nasm_rd_reg32 [16];
16 | extern const enum reg_enum nasm_rd_reg64 [16];
17 | extern const enum reg_enum nasm_rd_reg8 [ 8];
18 | extern const enum reg_enum nasm_rd_reg8_rex[16];
19 | extern const enum reg_enum nasm_rd_sreg [ 8];
20 | extern const enum reg_enum nasm_rd_treg [ 8];
21 | extern const enum reg_enum nasm_rd_xmmreg [32];
22 | extern const enum reg_enum nasm_rd_ymmreg [32];
23 | extern const enum reg_enum nasm_rd_zmmreg [32];
24 |
25 | #endif /* NASM_REGDIS_H */
26 |
--------------------------------------------------------------------------------
/arch/x86/regs.c:
--------------------------------------------------------------------------------
1 | /* automatically generated from ./x86/regs.dat - do not edit */
2 |
3 | #include "tables.h"
4 |
5 | const char * const nasm_reg_names[] = {
6 | "ah",
7 | "al",
8 | "ax",
9 | "bh",
10 | "bl",
11 | "bnd0",
12 | "bnd1",
13 | "bnd2",
14 | "bnd3",
15 | "bp",
16 | "bpl",
17 | "bx",
18 | "ch",
19 | "cl",
20 | "cr0",
21 | "cr1",
22 | "cr10",
23 | "cr11",
24 | "cr12",
25 | "cr13",
26 | "cr14",
27 | "cr15",
28 | "cr2",
29 | "cr3",
30 | "cr4",
31 | "cr5",
32 | "cr6",
33 | "cr7",
34 | "cr8",
35 | "cr9",
36 | "cs",
37 | "cx",
38 | "dh",
39 | "di",
40 | "dil",
41 | "dl",
42 | "dr0",
43 | "dr1",
44 | "dr10",
45 | "dr11",
46 | "dr12",
47 | "dr13",
48 | "dr14",
49 | "dr15",
50 | "dr2",
51 | "dr3",
52 | "dr4",
53 | "dr5",
54 | "dr6",
55 | "dr7",
56 | "dr8",
57 | "dr9",
58 | "ds",
59 | "dx",
60 | "eax",
61 | "ebp",
62 | "ebx",
63 | "ecx",
64 | "edi",
65 | "edx",
66 | "es",
67 | "esi",
68 | "esp",
69 | "fs",
70 | "gs",
71 | "k0",
72 | "k1",
73 | "k2",
74 | "k3",
75 | "k4",
76 | "k5",
77 | "k6",
78 | "k7",
79 | "mm0",
80 | "mm1",
81 | "mm2",
82 | "mm3",
83 | "mm4",
84 | "mm5",
85 | "mm6",
86 | "mm7",
87 | "r10",
88 | "r10b",
89 | "r10d",
90 | "r10w",
91 | "r11",
92 | "r11b",
93 | "r11d",
94 | "r11w",
95 | "r12",
96 | "r12b",
97 | "r12d",
98 | "r12w",
99 | "r13",
100 | "r13b",
101 | "r13d",
102 | "r13w",
103 | "r14",
104 | "r14b",
105 | "r14d",
106 | "r14w",
107 | "r15",
108 | "r15b",
109 | "r15d",
110 | "r15w",
111 | "r8",
112 | "r8b",
113 | "r8d",
114 | "r8w",
115 | "r9",
116 | "r9b",
117 | "r9d",
118 | "r9w",
119 | "rax",
120 | "rbp",
121 | "rbx",
122 | "rcx",
123 | "rdi",
124 | "rdx",
125 | "rsi",
126 | "rsp",
127 | "segr6",
128 | "segr7",
129 | "si",
130 | "sil",
131 | "sp",
132 | "spl",
133 | "ss",
134 | "st0",
135 | "st1",
136 | "st2",
137 | "st3",
138 | "st4",
139 | "st5",
140 | "st6",
141 | "st7",
142 | "tr0",
143 | "tr1",
144 | "tr2",
145 | "tr3",
146 | "tr4",
147 | "tr5",
148 | "tr6",
149 | "tr7",
150 | "xmm0",
151 | "xmm1",
152 | "xmm10",
153 | "xmm11",
154 | "xmm12",
155 | "xmm13",
156 | "xmm14",
157 | "xmm15",
158 | "xmm16",
159 | "xmm17",
160 | "xmm18",
161 | "xmm19",
162 | "xmm2",
163 | "xmm20",
164 | "xmm21",
165 | "xmm22",
166 | "xmm23",
167 | "xmm24",
168 | "xmm25",
169 | "xmm26",
170 | "xmm27",
171 | "xmm28",
172 | "xmm29",
173 | "xmm3",
174 | "xmm30",
175 | "xmm31",
176 | "xmm4",
177 | "xmm5",
178 | "xmm6",
179 | "xmm7",
180 | "xmm8",
181 | "xmm9",
182 | "ymm0",
183 | "ymm1",
184 | "ymm10",
185 | "ymm11",
186 | "ymm12",
187 | "ymm13",
188 | "ymm14",
189 | "ymm15",
190 | "ymm16",
191 | "ymm17",
192 | "ymm18",
193 | "ymm19",
194 | "ymm2",
195 | "ymm20",
196 | "ymm21",
197 | "ymm22",
198 | "ymm23",
199 | "ymm24",
200 | "ymm25",
201 | "ymm26",
202 | "ymm27",
203 | "ymm28",
204 | "ymm29",
205 | "ymm3",
206 | "ymm30",
207 | "ymm31",
208 | "ymm4",
209 | "ymm5",
210 | "ymm6",
211 | "ymm7",
212 | "ymm8",
213 | "ymm9",
214 | "zmm0",
215 | "zmm1",
216 | "zmm10",
217 | "zmm11",
218 | "zmm12",
219 | "zmm13",
220 | "zmm14",
221 | "zmm15",
222 | "zmm16",
223 | "zmm17",
224 | "zmm18",
225 | "zmm19",
226 | "zmm2",
227 | "zmm20",
228 | "zmm21",
229 | "zmm22",
230 | "zmm23",
231 | "zmm24",
232 | "zmm25",
233 | "zmm26",
234 | "zmm27",
235 | "zmm28",
236 | "zmm29",
237 | "zmm3",
238 | "zmm30",
239 | "zmm31",
240 | "zmm4",
241 | "zmm5",
242 | "zmm6",
243 | "zmm7",
244 | "zmm8",
245 | "zmm9"
246 | };
247 |
--------------------------------------------------------------------------------
/arch/x86/srcfile.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * These functions are used to keep track of the source code file and name.
36 | */
37 | #ifndef ASM_SRCFILE_H
38 | #define ASM_SRCFILE_H
39 |
40 | #include "compiler.h"
41 |
42 | struct src_location {
43 | const char *filename;
44 | int32_t lineno;
45 | };
46 | extern struct src_location _src_here;
47 |
48 | void src_init(void);
49 | void src_free(void);
50 | const char *src_set_fname(const char *newname);
51 | static inline const char *src_get_fname(void)
52 | {
53 | return _src_here.filename;
54 | }
55 | static inline int32_t src_set_linnum(int32_t newline)
56 | {
57 | int32_t oldline = _src_here.lineno;
58 | _src_here.lineno = newline;
59 | return oldline;
60 | }
61 | static inline int32_t src_get_linnum(void)
62 | {
63 | return _src_here.lineno;
64 | }
65 | /* Can be used when there is no need for the old information */
66 | void src_set(int32_t line, const char *filename);
67 |
68 | /*
69 | * src_get gets both the source file name and line.
70 | * It is also used if you maintain private status about the source location
71 | * It return 0 if the information was the same as the last time you
72 | * checked, -2 if the name changed and (new-old) if just the line changed.
73 | *
74 | * xname must point to a filename string previously returned from any
75 | * function of this subsystem or be NULL; another string value will
76 | * not work.
77 | */
78 | static inline int32_t src_get(int32_t *xline, const char **xname)
79 | {
80 | const char *xn = *xname;
81 | int32_t xl = *xline;
82 |
83 | *xline = _src_here.lineno;
84 | *xname = _src_here.filename;
85 |
86 | /* The return value is expected to be optimized out almost everywhere */
87 | if (!xn || xn != _src_here.filename)
88 | return -2;
89 | else
90 | return _src_here.lineno - xl;
91 | }
92 |
93 | /*
94 | * Returns and sets/returns the current information as a structure.
95 | */
96 | static inline struct src_location src_where(void)
97 | {
98 | return _src_here;
99 | }
100 | struct src_location src_update(struct src_location);
101 |
102 | #endif /* ASM_SRCFILE_H */
103 |
--------------------------------------------------------------------------------
/arch/x86/strlist.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * strlist.h - list of unique, ordered strings
36 | */
37 |
38 | #ifndef NASM_STRLIST_H
39 | #define NASM_STRLIST_H
40 |
41 | #include "compiler.h"
42 | #include "nasmlib.h"
43 | #include "hashtbl.h"
44 |
45 | struct strlist_entry {
46 | struct strlist_entry *next;
47 | size_t offset;
48 | size_t size;
49 | intorptr pvt;
50 | char str[1];
51 | };
52 |
53 | struct strlist {
54 | struct hash_table hash;
55 | struct strlist_entry *head, **tailp;
56 | size_t nstr, size;
57 | bool uniq;
58 | };
59 |
60 | static inline const struct strlist_entry *
61 | strlist_head(const struct strlist *list)
62 | {
63 | return list ? list->head : NULL;
64 | }
65 | static inline struct strlist_entry *strlist_tail(struct strlist *list)
66 | {
67 | if (!list || !list->head)
68 | return NULL;
69 | return container_of(list->tailp, struct strlist_entry, next);
70 | }
71 | static inline size_t strlist_count(const struct strlist *list)
72 | {
73 | return list ? list->nstr : 0;
74 | }
75 | static inline size_t strlist_size(const struct strlist *list)
76 | {
77 | return list ? list->size : 0;
78 | }
79 |
80 | struct strlist safe_alloc *strlist_alloc(bool uniq);
81 | const struct strlist_entry * never_null strlist_add(struct strlist *list, const char *str);
82 | const struct strlist_entry * printf_func(2, 3) never_null
83 | strlist_printf(struct strlist *list, const char *fmt, ...);
84 | const struct strlist_entry * never_null
85 | strlist_vprintf(struct strlist *list, const char *fmt, va_list ap);
86 | const struct strlist_entry *
87 | strlist_find(const struct strlist *list, const char *str);
88 | void * safe_alloc strlist_linearize(const struct strlist *list, char sep);
89 | void strlist_write(const struct strlist *list, const char *sep, FILE *f);
90 | void strlist_free(struct strlist **listp);
91 | #define strlist_for_each(p,h) list_for_each((p), strlist_head(h))
92 |
93 | #endif /* NASM_STRLIST_H */
94 |
--------------------------------------------------------------------------------
/arch/x86/sync.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * sync.c the Netwide Disassembler synchronisation processing module
36 | */
37 |
38 | #include "compiler.h"
39 |
40 |
41 | #include "nasmlib.h"
42 | #include "sync.h"
43 |
44 | #define SYNC_MAX_SHIFT 31
45 | #define SYNC_MAX_SIZE (1U << SYNC_MAX_SHIFT)
46 |
47 | /* initial # of sync points (*must* be power of two)*/
48 | #define SYNC_INITIAL_CHUNK (1U << 12)
49 |
50 | /*
51 | * This lot manages the current set of sync points by means of a
52 | * heap (priority queue) structure.
53 | */
54 |
55 | static struct Sync {
56 | uint64_t pos;
57 | uint32_t length;
58 | } *synx;
59 |
60 | static uint32_t max_synx, nsynx;
61 |
62 | static inline void swap_sync(uint32_t dst, uint32_t src)
63 | {
64 | struct Sync t = synx[dst];
65 | synx[dst] = synx[src];
66 | synx[src] = t;
67 | }
68 |
69 | void init_sync(void)
70 | {
71 | max_synx = SYNC_INITIAL_CHUNK;
72 | synx = nasm_malloc((max_synx + 1) * sizeof(*synx));
73 | nsynx = 0;
74 | }
75 |
76 | void add_sync(uint64_t pos, uint32_t length)
77 | {
78 | uint32_t i;
79 |
80 | if (nsynx >= max_synx) {
81 | if (max_synx >= SYNC_MAX_SIZE) /* too many sync points! */
82 | return;
83 | max_synx = (max_synx << 1);
84 | synx = nasm_realloc(synx, (max_synx + 1) * sizeof(*synx));
85 | }
86 |
87 | nsynx++;
88 | synx[nsynx].pos = pos;
89 | synx[nsynx].length = length;
90 |
91 | for (i = nsynx; i > 1; i /= 2) {
92 | if (synx[i / 2].pos > synx[i].pos)
93 | swap_sync(i / 2, i);
94 | }
95 | }
96 |
97 | uint64_t next_sync(uint64_t position, uint32_t *length)
98 | {
99 | while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
100 | uint32_t i, j;
101 |
102 | swap_sync(nsynx, 1);
103 | nsynx--;
104 |
105 | i = 1;
106 | while (i * 2 <= nsynx) {
107 | j = i * 2;
108 | if (synx[j].pos < synx[i].pos &&
109 | (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
110 | swap_sync(j, i);
111 | i = j;
112 | } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
113 | swap_sync(j + 1, i);
114 | i = j + 1;
115 | } else
116 | break;
117 | }
118 | }
119 |
120 | if (nsynx > 0) {
121 | if (length)
122 | *length = synx[1].length;
123 | return synx[1].pos;
124 | } else {
125 | if (length)
126 | *length = 0L;
127 | return 0;
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/arch/x86/sync.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * sync.h header file for sync.c
36 | */
37 |
38 | #ifndef NASM_SYNC_H
39 | #define NASM_SYNC_H
40 |
41 | #include
42 |
43 | void init_sync(void);
44 | void add_sync(uint64_t position, uint32_t length);
45 | uint64_t next_sync(uint64_t position, uint32_t *length);
46 |
47 | #endif
48 |
--------------------------------------------------------------------------------
/arch/x86/tables.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * tables.h
36 | *
37 | * Declarations for auto-generated tables
38 | */
39 |
40 | #ifndef NASM_TABLES_H
41 | #define NASM_TABLES_H
42 |
43 | #include
44 | #include "compiler.h"
45 | #include "insnsi.h" /* For enum opcode */
46 |
47 | /* --- From standard.mac via macros.pl: --- */
48 |
49 | /* macros.c */
50 | extern const unsigned char nasm_stdmac_tasm[];
51 | extern const unsigned char nasm_stdmac_nasm[];
52 | extern const unsigned char nasm_stdmac_version[];
53 |
54 | struct use_package {
55 | const char *package;
56 | const unsigned char *macros;
57 | int index;
58 | };
59 | extern const struct use_package *nasm_find_use_package(const char *);
60 | extern const int use_package_count;
61 |
62 | /* --- From insns.dat via insns.pl: --- */
63 |
64 | /* insnsn.c */
65 | extern const char * const nasm_insn_names[];
66 |
67 | /* --- From regs.dat via regs.pl: --- */
68 |
69 | /* regs.c */
70 | extern const char * const nasm_reg_names[];
71 | /* regflags.c */
72 | typedef uint64_t opflags_t;
73 | typedef uint16_t decoflags_t;
74 | extern const opflags_t nasm_reg_flags[];
75 | /* regvals.c */
76 | extern const int nasm_regvals[];
77 |
78 | #endif /* NASM_TABLES_H */
79 |
--------------------------------------------------------------------------------
/arch/x86/tokens.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is generated from insns.dat, regs.dat and token.dat
3 | * by tokhash.pl; do not edit.
4 | */
5 |
6 | #ifndef NASM_TOKENS_H
7 | #define NASM_TOKENS_H
8 |
9 | #define MAX_KEYWORD 17 /* length of longest keyword */
10 |
11 | #endif /* NASM_TOKENS_H */
12 |
--------------------------------------------------------------------------------
/arch/x86/ver.c:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | #include "ver.h"
35 | #include "version.h"
36 |
37 | /* This is printed when entering nasm -v */
38 | const char nasm_version[] = NASM_VER;
39 | const char nasm_date[] = __DATE__;
40 | const char nasm_compile_options[] = ""
41 | #ifdef DEBUG
42 | " with -DDEBUG"
43 | #endif
44 | ;
45 |
46 | /* These are used by some backends. */
47 | static const char __nasm_comment[] =
48 | "The Netwide Assembler " NASM_VER;
49 |
50 | static const char __nasm_signature[] =
51 | "NASM " NASM_VER;
52 |
53 | /* These are constant so we could pass regression tests */
54 | static const char __nasm_comment_const[] ="The Netwide Assembler CONST";
55 | static const char __nasm_signature_const[] = "NASM CONST";
56 |
57 | int nasm_test_run(void)
58 | {
59 | return getenv("NASM_TEST_RUN") ? 1 : 0;
60 | }
61 |
62 | const char *nasm_comment(void)
63 | {
64 | if (!nasm_test_run())
65 | return __nasm_comment;
66 | return __nasm_comment_const;
67 | }
68 |
69 | size_t nasm_comment_len(void)
70 | {
71 | if (!nasm_test_run())
72 | return strlen(__nasm_comment);
73 | return strlen(__nasm_comment_const);
74 | }
75 |
76 | const char *nasm_signature(void)
77 | {
78 | if (!nasm_test_run())
79 | return __nasm_signature;
80 | return __nasm_signature_const;
81 | }
82 |
83 | size_t nasm_signature_len(void)
84 | {
85 | if (!nasm_test_run())
86 | return strlen(__nasm_signature);
87 | return strlen(__nasm_signature_const);
88 | }
89 |
--------------------------------------------------------------------------------
/arch/x86/ver.h:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------- *
2 | *
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 | * See the file AUTHORS included with the NASM distribution for
5 | * the specific copyright holders.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following
9 | * conditions are met:
10 | *
11 | * * Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * * Redistributions in binary form must reproduce the above
14 | * copyright notice, this list of conditions and the following
15 | * disclaimer in the documentation and/or other materials provided
16 | * with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | *
32 | * ----------------------------------------------------------------------- */
33 |
34 | /*
35 | * NASM version strings, defined in ver.c
36 | */
37 |
38 | #ifndef NASM_VER_H
39 | #define NASM_VER_H
40 |
41 | #include "compiler.h"
42 |
43 | extern const char nasm_version[];
44 | extern const char nasm_date[];
45 | extern const char nasm_compile_options[];
46 |
47 | extern const char *nasm_comment(void);
48 | extern size_t nasm_comment_len(void);
49 |
50 | extern const char *nasm_signature(void);
51 | extern size_t nasm_signature_len(void);
52 |
53 | extern int nasm_test_run(void);
54 |
55 | #endif /* NASM_VER_H */
56 |
--------------------------------------------------------------------------------
/arch/x86/version.h:
--------------------------------------------------------------------------------
1 | #ifndef NASM_VERSION_H
2 | #define NASM_VERSION_H
3 | #define NASM_MAJOR_VER 2
4 | #define NASM_MINOR_VER 14
5 | #define NASM_SUBMINOR_VER 99
6 | #define NASM_PATCHLEVEL_VER 90
7 | #define NASM_VERSION_ID 0x020e635a
8 | #define NASM_VER "2.15rc0"
9 | #endif /* NASM_VERSION_H */
10 |
--------------------------------------------------------------------------------
/arch/x86/warnings.c:
--------------------------------------------------------------------------------
1 | #include "error.h"
2 |
3 | const char * const warning_name[34] = {
4 | NULL,
5 | "bnd",
6 | "environment",
7 | "float-denorm",
8 | "float-overflow",
9 | "float-toolong",
10 | "float-underflow",
11 | "hle",
12 | "label-orphan",
13 | "label-redef",
14 | "label-redef-late",
15 | "lock",
16 | "macro-defaults",
17 | "macro-params-multi",
18 | "macro-params-single",
19 | "negative-rep",
20 | "number-overflow",
21 | "obsolete-nop",
22 | "obsolete-removed",
23 | "obsolete-valid",
24 | "phase",
25 | "pragma-bad",
26 | "pragma-empty",
27 | "pragma-na",
28 | "pragma-unknown",
29 | "ptr",
30 | "regsize",
31 | "unknown-warning",
32 | "user",
33 | "warn-stack-empty",
34 | "zeroing",
35 | "zext-reloc",
36 | "other",
37 | "all"
38 | };
39 |
40 | const struct warning_alias warning_alias[37] = {
41 | { "all", WARN_IDX_ALL },
42 | { "bad-pragma", WARN_IDX_PRAGMA_BAD },
43 | { "bnd", WARN_IDX_BND },
44 | { "environment", WARN_IDX_ENVIRONMENT },
45 | { "float-denorm", WARN_IDX_FLOAT_DENORM },
46 | { "float-overflow", WARN_IDX_FLOAT_OVERFLOW },
47 | { "float-toolong", WARN_IDX_FLOAT_TOOLONG },
48 | { "float-underflow", WARN_IDX_FLOAT_UNDERFLOW },
49 | { "hle", WARN_IDX_HLE },
50 | { "label-orphan", WARN_IDX_LABEL_ORPHAN },
51 | { "label-redef", WARN_IDX_LABEL_REDEF },
52 | { "label-redef-late", WARN_IDX_LABEL_REDEF_LATE },
53 | { "lock", WARN_IDX_LOCK },
54 | { "macro-defaults", WARN_IDX_MACRO_DEFAULTS },
55 | { "macro-params-multi", WARN_IDX_MACRO_PARAMS_MULTI },
56 | { "macro-params-single", WARN_IDX_MACRO_PARAMS_SINGLE },
57 | { "negative-rep", WARN_IDX_NEGATIVE_REP },
58 | { "not-my-pragma", WARN_IDX_PRAGMA_NA },
59 | { "number-overflow", WARN_IDX_NUMBER_OVERFLOW },
60 | { "obsolete-nop", WARN_IDX_OBSOLETE_NOP },
61 | { "obsolete-removed", WARN_IDX_OBSOLETE_REMOVED },
62 | { "obsolete-valid", WARN_IDX_OBSOLETE_VALID },
63 | { "orphan-labels", WARN_IDX_LABEL_ORPHAN },
64 | { "other", WARN_IDX_OTHER },
65 | { "phase", WARN_IDX_PHASE },
66 | { "pragma-bad", WARN_IDX_PRAGMA_BAD },
67 | { "pragma-empty", WARN_IDX_PRAGMA_EMPTY },
68 | { "pragma-na", WARN_IDX_PRAGMA_NA },
69 | { "pragma-unknown", WARN_IDX_PRAGMA_UNKNOWN },
70 | { "ptr", WARN_IDX_PTR },
71 | { "regsize", WARN_IDX_REGSIZE },
72 | { "unknown-pragma", WARN_IDX_PRAGMA_UNKNOWN },
73 | { "unknown-warning", WARN_IDX_UNKNOWN_WARNING },
74 | { "user", WARN_IDX_USER },
75 | { "warn-stack-empty", WARN_IDX_WARN_STACK_EMPTY },
76 | { "zeroing", WARN_IDX_ZEROING },
77 | { "zext-reloc", WARN_IDX_ZEXT_RELOC }
78 | };
79 |
80 | const char * const warning_help[34] = {
81 | NULL,
82 | "invalid BND prefixes",
83 | "nonexistent environment variable",
84 | "floating point denormal",
85 | "floating point overflow",
86 | "too many digits in floating-point number",
87 | "floating point underflow",
88 | "invalid HLE prefixes",
89 | "labels alone on lines without trailing `:\'",
90 | "label redefined to an identical value",
91 | "label (re)defined during code generation",
92 | "LOCK prefix on unlockable instructions",
93 | "macros with more default than optional parameters",
94 | "multi-line macro calls with wrong parameter count",
95 | "single-line macro calls with wrong parameter count",
96 | "regative %rep count",
97 | "numeric constant does not fit",
98 | "instruction obsolete and is a noop on the target CPU",
99 | "instruction obsolete and removed on the target CPU",
100 | "instruction obsolete but valid on the target CPU",
101 | "phase error during stabilization",
102 | "malformed %pragma",
103 | "empty %pragma directive",
104 | "%pragma not applicable to this compilation",
105 | "unknown %pragma facility or directive",
106 | "non-NASM keyword used in other assemblers",
107 | "register size specification ignored",
108 | "unknown warning in -W/-w or warning directive",
109 | "%warning directives",
110 | "warning stack empty",
111 | "RESx in initialized section becomes zero",
112 | "relocation zero-extended to match output format",
113 | "any warning not specifially mentioned above",
114 | "all possible warnings"
115 | };
116 |
117 | const uint8_t warning_default[33] = {
118 | WARN_INIT_ON,
119 | WARN_INIT_ON,
120 | WARN_INIT_ON,
121 | WARN_INIT_OFF,
122 | WARN_INIT_ON,
123 | WARN_INIT_ON,
124 | WARN_INIT_OFF,
125 | WARN_INIT_ON,
126 | WARN_INIT_ON,
127 | WARN_INIT_OFF,
128 | WARN_INIT_ERR,
129 | WARN_INIT_ON,
130 | WARN_INIT_ON,
131 | WARN_INIT_ON,
132 | WARN_INIT_ON,
133 | WARN_INIT_ON,
134 | WARN_INIT_ON,
135 | WARN_INIT_ON,
136 | WARN_INIT_ON,
137 | WARN_INIT_ON,
138 | WARN_INIT_OFF,
139 | WARN_INIT_OFF,
140 | WARN_INIT_OFF,
141 | WARN_INIT_OFF,
142 | WARN_INIT_OFF,
143 | WARN_INIT_ON,
144 | WARN_INIT_ON,
145 | WARN_INIT_OFF,
146 | WARN_INIT_ON,
147 | WARN_INIT_ON,
148 | WARN_INIT_ON,
149 | WARN_INIT_ON,
150 | WARN_INIT_ON
151 | };
152 |
153 | uint8_t warning_state[33]; /* Current state */
154 |
--------------------------------------------------------------------------------
/arch/x86/warnings.h:
--------------------------------------------------------------------------------
1 | #ifndef NASM_WARNINGS_H
2 | #define NASM_WARNINGS_H
3 |
4 | #ifndef WARN_SHR
5 | # error "warnings.h should only be included from within error.h"
6 | #endif
7 |
8 | enum warn_index {
9 | WARN_IDX_NONE = 0, /* not suppressible */
10 | WARN_IDX_BND = 1, /* invalid BND prefixes */
11 | WARN_IDX_ENVIRONMENT = 2, /* nonexistent environment variable */
12 | WARN_IDX_FLOAT_DENORM = 3, /* floating point denormal */
13 | WARN_IDX_FLOAT_OVERFLOW = 4, /* floating point overflow */
14 | WARN_IDX_FLOAT_TOOLONG = 5, /* too many digits in floating-point number */
15 | WARN_IDX_FLOAT_UNDERFLOW = 6, /* floating point underflow */
16 | WARN_IDX_HLE = 7, /* invalid HLE prefixes */
17 | WARN_IDX_LABEL_ORPHAN = 8, /* labels alone on lines without trailing `:' */
18 | WARN_IDX_LABEL_REDEF = 9, /* label redefined to an identical value */
19 | WARN_IDX_LABEL_REDEF_LATE = 10, /* label (re)defined during code generation */
20 | WARN_IDX_LOCK = 11, /* LOCK prefix on unlockable instructions */
21 | WARN_IDX_MACRO_DEFAULTS = 12, /* macros with more default than optional parameters */
22 | WARN_IDX_MACRO_PARAMS_MULTI = 13, /* multi-line macro calls with wrong parameter count */
23 | WARN_IDX_MACRO_PARAMS_SINGLE = 14, /* single-line macro calls with wrong parameter count */
24 | WARN_IDX_NEGATIVE_REP = 15, /* regative %rep count */
25 | WARN_IDX_NUMBER_OVERFLOW = 16, /* numeric constant does not fit */
26 | WARN_IDX_OBSOLETE_NOP = 17, /* instruction obsolete and is a noop on the target CPU */
27 | WARN_IDX_OBSOLETE_REMOVED = 18, /* instruction obsolete and removed on the target CPU */
28 | WARN_IDX_OBSOLETE_VALID = 19, /* instruction obsolete but valid on the target CPU */
29 | WARN_IDX_PHASE = 20, /* phase error during stabilization */
30 | WARN_IDX_PRAGMA_BAD = 21, /* malformed %pragma */
31 | WARN_IDX_PRAGMA_EMPTY = 22, /* empty %pragma directive */
32 | WARN_IDX_PRAGMA_NA = 23, /* %pragma not applicable to this compilation */
33 | WARN_IDX_PRAGMA_UNKNOWN = 24, /* unknown %pragma facility or directive */
34 | WARN_IDX_PTR = 25, /* non-NASM keyword used in other assemblers */
35 | WARN_IDX_REGSIZE = 26, /* register size specification ignored */
36 | WARN_IDX_UNKNOWN_WARNING = 27, /* unknown warning in -W/-w or warning directive */
37 | WARN_IDX_USER = 28, /* %warning directives */
38 | WARN_IDX_WARN_STACK_EMPTY = 29, /* warning stack empty */
39 | WARN_IDX_ZEROING = 30, /* RESx in initialized section becomes zero */
40 | WARN_IDX_ZEXT_RELOC = 31, /* relocation zero-extended to match output format */
41 | WARN_IDX_OTHER = 32, /* any warning not specifially mentioned above */
42 | WARN_IDX_ALL = 33 /* all possible warnings */
43 | };
44 |
45 | enum warn_const {
46 | WARN_NONE = 0 << WARN_SHR,
47 | WARN_BND = 1 << WARN_SHR,
48 | WARN_ENVIRONMENT = 2 << WARN_SHR,
49 | WARN_FLOAT_DENORM = 3 << WARN_SHR,
50 | WARN_FLOAT_OVERFLOW = 4 << WARN_SHR,
51 | WARN_FLOAT_TOOLONG = 5 << WARN_SHR,
52 | WARN_FLOAT_UNDERFLOW = 6 << WARN_SHR,
53 | WARN_HLE = 7 << WARN_SHR,
54 | WARN_LABEL_ORPHAN = 8 << WARN_SHR,
55 | WARN_LABEL_REDEF = 9 << WARN_SHR,
56 | WARN_LABEL_REDEF_LATE = 10 << WARN_SHR,
57 | WARN_LOCK = 11 << WARN_SHR,
58 | WARN_MACRO_DEFAULTS = 12 << WARN_SHR,
59 | WARN_MACRO_PARAMS_MULTI = 13 << WARN_SHR,
60 | WARN_MACRO_PARAMS_SINGLE = 14 << WARN_SHR,
61 | WARN_NEGATIVE_REP = 15 << WARN_SHR,
62 | WARN_NUMBER_OVERFLOW = 16 << WARN_SHR,
63 | WARN_OBSOLETE_NOP = 17 << WARN_SHR,
64 | WARN_OBSOLETE_REMOVED = 18 << WARN_SHR,
65 | WARN_OBSOLETE_VALID = 19 << WARN_SHR,
66 | WARN_PHASE = 20 << WARN_SHR,
67 | WARN_PRAGMA_BAD = 21 << WARN_SHR,
68 | WARN_PRAGMA_EMPTY = 22 << WARN_SHR,
69 | WARN_PRAGMA_NA = 23 << WARN_SHR,
70 | WARN_PRAGMA_UNKNOWN = 24 << WARN_SHR,
71 | WARN_PTR = 25 << WARN_SHR,
72 | WARN_REGSIZE = 26 << WARN_SHR,
73 | WARN_UNKNOWN_WARNING = 27 << WARN_SHR,
74 | WARN_USER = 28 << WARN_SHR,
75 | WARN_WARN_STACK_EMPTY = 29 << WARN_SHR,
76 | WARN_ZEROING = 30 << WARN_SHR,
77 | WARN_ZEXT_RELOC = 31 << WARN_SHR,
78 | WARN_OTHER = 32 << WARN_SHR
79 | };
80 |
81 | struct warning_alias {
82 | const char *name;
83 | enum warn_index warning;
84 | };
85 |
86 | #define NUM_WARNING_ALIAS 37
87 | extern const char * const warning_name[34];
88 | extern const char * const warning_help[34];
89 | extern const struct warning_alias warning_alias[NUM_WARNING_ALIAS];
90 | extern const uint8_t warning_default[33];
91 | extern uint8_t warning_state[33];
92 |
93 | #endif /* NASM_WARNINGS_H */
94 |
--------------------------------------------------------------------------------
/be.c:
--------------------------------------------------------------------------------
1 | // BE: INFOSEC BINARY HEX EDITOR WITH DASM
2 | // Synrc Research (c) 2022-2023
3 | // 5HT DHARMA License
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | #include "editor.h"
12 | #include "term/terminal.h"
13 | #include "dasm/dasm.h"
14 |
15 | volatile sig_atomic_t resizeflag;
16 |
17 | static void editor_exit() {
18 | struct editor* e = editor();
19 | editor_free(e);
20 | clear_screen();
21 | disable_raw_mode();
22 | term_state_restore();
23 | }
24 |
25 | static void print_help(const char* explanation) {
26 | fprintf(stderr,
27 | "%s"\
28 | "Usage: be [-vhdbao] \n"\
29 | "\n"
30 | "Options:\n"
31 | " -v Get version information\n"
32 | " -h Print usage info and exits\n"
33 | " -d Launch ASM view by default\n"
34 | " -b bitness CPU Bitness\n"
35 | " -a arch 1:EM64T, 2:ARM64, 3:RISC-V, 4:PPC, 5:SH-4, 6:M68K, 7:MIPS, 8:PDP-11, 9:nVidia\n"
36 | " -o octets Octets per screen for HEX view\n"
37 | "\n"
38 | "Report bugs to \n", explanation);
39 | }
40 |
41 | void print_version() {
42 | printf("BE version %s codename %s released %s\n", XT_VERSION, XT_RELEASE_CODENAME, XT_RELEASE_DATE);
43 | }
44 |
45 | static void handle_term_resize(int sig) {
46 | (void)(sig);
47 | resizeflag = 1;
48 | }
49 |
50 | static void resize_term() {
51 | struct editor *e = editor();
52 | clear_screen();
53 | get_window_size(&(e->screen_rows), &(e->screen_cols));
54 | }
55 |
56 | int main(int argc, char* argv[]) {
57 | char* file = NULL;
58 | int ch = 0, bitness = 64, opl = 24, view = 0, arch = ARCH_INTEL;
59 | while ((ch = getopt(argc, argv, "vhdb:o:a:")) != -1) {
60 | switch (ch) {
61 | case 'v': print_version(); return 0;
62 | case 'h': print_help(""); exit(0); break;
63 | case 'o': opl = str2int(optarg, 16, 64, 16); break;
64 | case 'b': bitness = str2int(optarg, 16, 128, 16); break;
65 | case 'a': arch = (enum dasm_arch)str2int(optarg, 0, 10, 1); break;
66 | case 'd': view = VIEW_ASM; break;
67 | default: print_help(""); exit(1); break;
68 | }
69 | }
70 |
71 | if (optind >= argc) {
72 | print_help("Error: filename is expected.\n");
73 | exit(1);
74 | }
75 |
76 | file = argv[optind];
77 |
78 | struct sigaction act;
79 | memset(&act, 0, sizeof(struct sigaction));
80 | act.sa_handler = handle_term_resize;
81 | sigaction(SIGWINCH, &act, NULL);
82 | resizeflag = 0;
83 |
84 | editor_init();
85 | struct editor* e = editor();
86 |
87 | editor_openfile(e, file);
88 | enable_raw_mode();
89 | term_state_save();
90 | atexit(editor_exit);
91 | clear_screen();
92 | e->octets_per_line = opl;
93 | e->seg_size = bitness;
94 | e->arch = arch;
95 | editor_setview(e, view ? VIEW_ASM : VIEW_HEX);
96 | nasm_init(e);
97 |
98 | while (true) {
99 | editor_refresh_screen(e);
100 | editor_process_keypress(e);
101 | if (resizeflag == 1) {
102 | resize_term();
103 | resizeflag = 0;
104 | }
105 | }
106 |
107 | editor_free(e);
108 | return 0;
109 | }
110 |
--------------------------------------------------------------------------------
/dasm/dasm.h:
--------------------------------------------------------------------------------
1 | #ifndef XT_DASM_H
2 | #define XT_DASM_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #include "../editor.h"
15 |
16 | void nasm_init();
17 | void editor_render_dasm(struct editor* e, struct charbuf* b);
18 | void editor_move_cursor_dasm(struct editor* e, int dir, int amount);
19 | void editor_replace_byte_dasm(struct editor* e, char x);
20 | void editor_insert_byte_dasm(struct editor* e, char x, bool after);
21 | void editor_scroll_dasm(struct editor* e, int units);
22 |
23 | #endif
24 |
--------------------------------------------------------------------------------
/editor.h:
--------------------------------------------------------------------------------
1 | #ifndef XT_EDITOR_H
2 | #define XT_EDITOR_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include "term/buffer.h"
8 |
9 | enum editor_view {
10 | VIEW_HEX = 1,
11 | VIEW_ASM = 2,
12 | };
13 |
14 | enum dasm_arch {
15 | ARCH_INTEL = 1,
16 | ARCH_ARM = 2,
17 | ARCH_RISCV = 3,
18 | ARCH_PPC = 4,
19 | ARCH_SH4 = 5,
20 | ARCH_M68K = 6,
21 | ARCH_MIPS = 7,
22 | ARCH_PDP11 = 8,
23 | ARCH_NVIDIA = 9,
24 | };
25 |
26 | enum editor_mode {
27 | MODE_APPEND = 1 << 0,
28 | MODE_APPEND_ASCII = 1 << 1,
29 | MODE_REPLACE_ASCII = 1 << 2,
30 | MODE_NORMAL = 1 << 3,
31 | MODE_INSERT = 1 << 4,
32 | MODE_INSERT_ASCII = 1 << 5,
33 | MODE_REPLACE = 1 << 6,
34 | MODE_COMMAND = 1 << 7,
35 | MODE_SEARCH = 1 << 8,
36 | };
37 |
38 | enum search_direction {
39 | SEARCH_FORWARD,
40 | SEARCH_BACKWARD,
41 | };
42 |
43 | enum status_severity {
44 | STATUS_INFO,
45 | STATUS_WARNING,
46 | STATUS_ERROR,
47 | };
48 |
49 | #define INPUT_BUF_SIZE 80
50 |
51 | struct editor {
52 | int octets_per_line;
53 | int grouping;
54 | int line;
55 | int cursor_x;
56 | int cursor_y;
57 | int hex_x;
58 | int hex_y;
59 | int screen_rows;
60 | int screen_cols;
61 | enum editor_mode mode;
62 | bool dirty;
63 | char* filename;
64 | char* contents;
65 | unsigned long offset_dasm;
66 | unsigned long offset_hex;
67 | enum editor_view view;
68 | enum dasm_arch arch;
69 | unsigned int content_length;
70 | enum status_severity status_severity;
71 | char status_message[120];
72 | char inputbuffer[INPUT_BUF_SIZE];
73 | int inputbuffer_index;
74 | char searchstr[INPUT_BUF_SIZE];
75 | int seg_size;
76 | };
77 |
78 | int hexstr_idx_inc();
79 | int hexstr_idx_set(int value);
80 | int hexstr_idx();
81 | char *hexstr();
82 | char hexstr_set(int pos, int value);
83 | char hexstr_get(int pos);
84 |
85 | struct editor* editor();
86 | struct editor* editor_init();
87 | void editor_free(struct editor* e);
88 | void editor_openfile(struct editor* e, const char* filename);
89 | void editor_refresh_screen(struct editor* e);
90 | void editor_setmode(struct editor *e, enum editor_mode mode);
91 | void editor_setview(struct editor *e, enum editor_view view);
92 | int editor_statusmessage(struct editor* e, enum status_severity s, const char* fmt, ...);
93 | void editor_writefile(struct editor* e);
94 |
95 | void editor_move_cursor(struct editor* e, int dir, int amount);
96 | void editor_process_keypress(struct editor* e);
97 | void editor_scroll(struct editor* e, int units);
98 |
99 | #endif
100 |
--------------------------------------------------------------------------------
/hex/hex.h:
--------------------------------------------------------------------------------
1 | #ifndef XT_HEX_H
2 | #define XT_HEX_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #include "../editor.h"
15 |
16 | void editor_cursor_at_offset(struct editor* e, int offset, int* x, int* y);
17 | int editor_offset_at_cursor(struct editor* e);
18 | void editor_scroll_to_offset(struct editor* e, unsigned int offset);
19 |
20 | void editor_move_cursor_hex(struct editor* e, int dir, int amount);
21 | void editor_scroll_hex(struct editor* e, int units);
22 |
23 | void editor_replace_byte_hex(struct editor* e, char x);
24 | void editor_insert_byte_hex(struct editor* e, char x, bool after);
25 | void editor_insert_byte_at_offset(struct editor* e, unsigned int offset, char x, bool after);
26 |
27 | void editor_render_ascii(struct editor* e, int rownum, unsigned int start_offset, struct charbuf* b);
28 | void editor_render_hex(struct editor* e, struct charbuf* b);
29 |
30 | #endif
31 |
--------------------------------------------------------------------------------
/img/be.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BitEdits/be/fd6849748c34a0087cc9284c03afecded89de570/img/be.png
--------------------------------------------------------------------------------
/licenses/armadillo.txt:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2019, Justin Sherman
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/licenses/hx.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Kevin Pors
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/licenses/nasm.txt:
--------------------------------------------------------------------------------
1 | NASM is now licensed under the 2-clause BSD license, also known as the
2 | simplified BSD license.
3 |
4 | Copyright 1996-2010 the NASM Authors - All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following
8 | conditions are met:
9 |
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above
13 | copyright notice, this list of conditions and the following
14 | disclaimer in the documentation and/or other materials provided
15 | with the distribution.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/licenses/nv.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2009-2011 Marcelina Kościelnicka
2 | All Rights Reserved.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a
5 | copy of this software and associated documentation files (the "Software"),
6 | to deal in the Software without restriction, including without limitation
7 | the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 | and/or sell copies of the Software, and to permit persons to whom the
9 | Software is furnished to do so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice (including the next
12 | paragraph) shall be included in all copies or substantial portions of the
13 | Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 | OTHER DEALINGS IN THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/licenses/riscv.txt:
--------------------------------------------------------------------------------
1 | RISC-V Disassembler
2 |
3 | Copyright (c) 2016-2017 Michael Clark
4 | Copyright (c) 2017-2018 SiFive, Inc.
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/licenses/sh4.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017, 2019, 2020 snickerbockers
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 |
7 | Redistributions of source code must retain the above copyright notice, this
8 | list of conditions and the following disclaimer.
9 |
10 | Redistributions in binary form must reproduce the above copyright notice,
11 | this list of conditions and the following disclaimer in the documentation
12 | and/or other materials provided with the distribution.
13 |
14 | Neither the name of the copyright holder nor the names of its
15 | contributors may be used to endorse or promote products derived from
16 | this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 | POSSIBILITY OF SUCH DAMAGE.
29 |
--------------------------------------------------------------------------------
/licenses/vdappc.txt:
--------------------------------------------------------------------------------
1 | Simple PowerPC file and memory disassembler.
2 | Copyright (c) 1998-2001 Frank Wille
3 |
4 | vdappc is freeware and part of the portable and retargetable ANSI C
5 | compiler vbcc, copyright (c) 1995-2001 by Volker Barthelmann.
6 | vdappc may be freely redistributed as long as no modifications are
7 | made and nothing is charged for it. Non-commercial usage is allowed
8 | without any restrictions.
9 |
10 | EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
11 | SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
--------------------------------------------------------------------------------
/term/buffer.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #include "buffer.h"
9 |
10 | struct charbuf* charbuf_create() {
11 | struct charbuf* b = malloc(sizeof(struct charbuf));
12 | if (b) {
13 | b->contents = NULL;
14 | b->len = 0;
15 | b->cap = 0;
16 | return b;
17 | } else {
18 | perror("Unable to allocate size for struct charbuf");
19 | exit(1);
20 | }
21 | }
22 |
23 | void charbuf_free(struct charbuf* buf) {
24 | free(buf->contents);
25 | free(buf);
26 | }
27 |
28 | void charbuf_append(struct charbuf* buf, const char* what, size_t len) {
29 | assert(what != NULL);
30 |
31 | if ((int)(buf->len + len) >= buf->cap) {
32 | buf->cap += len;
33 | buf->cap *= 2;
34 | buf->contents = realloc(buf->contents, buf->cap);
35 | if (buf->contents == NULL) { perror("Unable to realloc charbuf"); exit(1); }
36 | }
37 |
38 | memcpy(buf->contents + buf->len, what, len);
39 | buf->len += len;
40 | }
41 |
42 | int charbuf_appendf(struct charbuf* buf, const char* fmt, ...) {
43 | assert(strlen(fmt) < CHARBUF_APPENDF_SIZE);
44 |
45 | char buffer[CHARBUF_APPENDF_SIZE];
46 | va_list ap;
47 | va_start(ap, fmt);
48 | int len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
49 | va_end(ap);
50 |
51 | charbuf_append(buf, buffer, len);
52 | return len;
53 | }
54 |
55 | void charbuf_draw(struct charbuf* buf) {
56 | if (write(STDOUT_FILENO, buf->contents, buf->len) == -1) {
57 | perror("Can't write charbuf");
58 | exit(1);
59 |
60 | }
61 | }
62 |
63 |
--------------------------------------------------------------------------------
/term/buffer.h:
--------------------------------------------------------------------------------
1 | #ifndef HX_BUFFER_H
2 | #define HX_BUFFER_H
3 |
4 | #include // size_t
5 |
6 | static const unsigned int CHARBUF_APPENDF_SIZE = 1024;
7 | struct charbuf { char* contents; int len; int cap; };
8 |
9 | struct charbuf* charbuf_create();
10 | void charbuf_free(struct charbuf* buf);
11 | void charbuf_append(struct charbuf* buf, const char* what, size_t len);
12 | int charbuf_appendf(struct charbuf* buf, const char* what, ...);
13 | void charbuf_draw(struct charbuf* buf);
14 |
15 | #endif // HX_CHARBUF_H
16 |
--------------------------------------------------------------------------------
/term/terminal.h:
--------------------------------------------------------------------------------
1 | #ifndef XT_TERMINAL_H
2 | #define XT_TERMINAL_H
3 |
4 | #define XT_RELEASE_CODENAME "Cutler"
5 | #define XT_RELEASE_DATE "2.5.2023"
6 | #define XT_VERSION "1.5.0"
7 |
8 | #include
9 | #include
10 |
11 | enum key_codes {
12 | KEY_NULL = 0,
13 | KEY_CTRL_D = 0x04,
14 | KEY_CTRL_H = 0x08,
15 | KEY_CTRL_Q = 0x11, // DC1, to exit the program.
16 | KEY_CTRL_R = 0x12, // DC2, to redo an action.
17 | KEY_CTRL_S = 0x13, // DC3, to save the current buffer.
18 | KEY_CTRL_U = 0x15,
19 | KEY_ESC = 0x1b, // ESC, for things like keys up, down, left, right, delete, ...
20 | KEY_ENTER = 0x0d,
21 | KEY_BACKSPACE = 0x7f,
22 | KEY_TAB = 0x09,
23 | KEY_CTRL_O = 1000,
24 | KEY_INSERT,
25 | KEY_DELETE,
26 | KEY_F1,
27 | KEY_F2,
28 | KEY_F3,
29 | KEY_F4,
30 | KEY_F5,
31 | KEY_F6,
32 | KEY_F7,
33 | KEY_F8,
34 | KEY_F9,
35 | KEY_F10,
36 | KEY_UP,
37 | KEY_DOWN,
38 | KEY_RIGHT,
39 | KEY_LEFT,
40 | KEY_DEL,
41 | KEY_HOME,
42 | KEY_END,
43 | KEY_PGUP,
44 | KEY_PGDOWN,
45 | KEY_CTRL_LEFT,
46 | KEY_CTRL_RIGHT,
47 | KEY_CTRL_ENTER,
48 | KEY_SHIFT_LEFT,
49 | KEY_SHIFT_RIGHT,
50 | KEY_SHIFT_ENTER,
51 | KEY_ESC_SHIFT_ENTER,
52 | KEY_ESC_SHIFT_LEFT_BRACKET,
53 | KEY_ESC_SHIFT_RIGHT_BRACKET
54 | };
55 |
56 | enum parse_errors {
57 | PARSE_SUCCESS,
58 | PARSE_INCOMPLETE_BACKSLASH, // "...\"
59 | PARSE_INCOMPLETE_HEX, // "...\x" or "...\xA"
60 | PARSE_INVALID_HEX, // "...\xXY..." and X or Y not in [a-zA-Z0-9]
61 | PARSE_INVALID_ESCAPE, // "...\a..." and a is not '\' or 'x'
62 | };
63 |
64 | void term_state_save();
65 | void term_state_restore();
66 | void enable_raw_mode();
67 | void disable_raw_mode();
68 | void clear_screen();
69 | int read_key();
70 | int hex2bin(const char* s);
71 | void gotoxy(int rows, int cols);
72 | bool get_window_size(int* rows, int* cols);
73 | bool is_pos_num(const char* s);
74 | bool is_hex(const char* s);
75 | int hex2int(const char* s);
76 | int clampi(int i, int min, int max);
77 | int str2int(const char* s, int min, int max, int def);
78 |
79 | #endif
80 |
--------------------------------------------------------------------------------