diff --git a/Makefile b/Makefile index b28524c..9ce264e 100644 --- a/Makefile +++ b/Makefile @@ -49,10 +49,12 @@ dist: $(PACKAGE)-$(VERSION)/src/parser.h \ $(PACKAGE)-$(VERSION)/src/token.h \ $(PACKAGE)-$(VERSION)/src/project.conf \ + $(PACKAGE)-$(VERSION)/src/arch/i386.c \ $(PACKAGE)-$(VERSION)/src/arch/sparc.c \ $(PACKAGE)-$(VERSION)/src/arch/Makefile \ $(PACKAGE)-$(VERSION)/src/arch/common.ins \ - $(PACKAGE)-$(VERSION)/src/arch/common.reg \ + $(PACKAGE)-$(VERSION)/src/arch/i386.ins \ + $(PACKAGE)-$(VERSION)/src/arch/i386.reg \ $(PACKAGE)-$(VERSION)/src/arch/null.ins \ $(PACKAGE)-$(VERSION)/src/arch/sparc.ins \ $(PACKAGE)-$(VERSION)/src/arch/sparc.reg \ @@ -64,6 +66,7 @@ dist: $(PACKAGE)-$(VERSION)/src/format/pe.c \ $(PACKAGE)-$(VERSION)/src/format/Makefile \ $(PACKAGE)-$(VERSION)/src/format/project.conf \ + $(PACKAGE)-$(VERSION)/test/i386.S \ $(PACKAGE)-$(VERSION)/test/sparc.S \ $(PACKAGE)-$(VERSION)/test/Makefile \ $(PACKAGE)-$(VERSION)/test/project.conf \ diff --git a/src/arch/Makefile b/src/arch/Makefile index a0152e4..06d74ee 100644 --- a/src/arch/Makefile +++ b/src/arch/Makefile @@ -1,4 +1,4 @@ -TARGETS = sparc.so +TARGETS = i386.so sparc.so PREFIX = /usr/local DESTDIR = LIBDIR = $(PREFIX)/lib @@ -18,6 +18,13 @@ INSTALL = install all: $(TARGETS) +i386_OBJS = i386.o +i386_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +i386_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +i386.so: $(i386_OBJS) + $(LD) -o i386.so $(i386_OBJS) $(i386_LDFLAGS) + sparc_OBJS = sparc.o sparc_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) sparc_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) @@ -25,20 +32,26 @@ sparc_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) sparc.so: $(sparc_OBJS) $(LD) -o sparc.so $(sparc_OBJS) $(sparc_LDFLAGS) -sparc.o: sparc.c common.ins common.reg null.ins sparc.ins sparc.reg +i386.o: i386.c common.ins null.ins i386.ins i386.reg + $(CC) $(i386_CFLAGS) -c i386.c + +sparc.o: sparc.c common.ins null.ins sparc.ins sparc.reg $(CC) $(sparc_CFLAGS) -c sparc.c clean: - $(RM) -- $(sparc_OBJS) + $(RM) -- $(i386_OBJS) $(sparc_OBJS) distclean: clean $(RM) -- $(TARGETS) install: $(TARGETS) + $(MKDIR) $(DESTDIR)$(LIBDIR)/asm/arch + $(INSTALL) -m 0644 -- i386.so $(DESTDIR)$(LIBDIR)/asm/arch/i386.so $(MKDIR) $(DESTDIR)$(LIBDIR)/asm/arch $(INSTALL) -m 0644 -- sparc.so $(DESTDIR)$(LIBDIR)/asm/arch/sparc.so uninstall: + $(RM) -- $(DESTDIR)$(LIBDIR)/asm/arch/i386.so $(RM) -- $(DESTDIR)$(LIBDIR)/asm/arch/sparc.so .PHONY: all clean distclean install uninstall diff --git a/src/arch/common.reg b/src/arch/common.reg deleted file mode 100644 index b8a0c2c..0000000 --- a/src/arch/common.reg +++ /dev/null @@ -1,13 +0,0 @@ -AO_NONE = _AO_NONE, -AO_OP = _AO_OP, -AO_IMM = _AO_IMM, -AO_REG = _AO_REG, -AO_DREG = _AO_DREG, -AO_OP_ = _AO_OP_, -AO_IMM_ = _AO_IMM_, -AO_REG_ = _AO_REG_, -AO_DREG_= _AO_DREG_, -AO_OP__ = _AO_OP__, -AO_IMM__= _AO_IMM__, -AO_REG__= _AO_REG__, -AO_DREG__= _AO_DREG__, diff --git a/src/arch/i386.c b/src/arch/i386.c new file mode 100644 index 0000000..bc81c69 --- /dev/null +++ b/src/arch/i386.c @@ -0,0 +1,47 @@ +/* $Id$ */ +/* Copyright (c) 2011 Pierre Pronchery */ +/* This file is part of DeforaOS Devel asm */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include "Asm/arch.h" + + +/* i386 */ +/* private */ +/* variables */ +#define REG(name, size, id) { "" # name, size, id }, +static ArchRegister _i386_registers[] = +{ +#include "i386.reg" + { NULL, 0, 0 } +}; + +static ArchInstruction _i386_instructions[] = +{ +#include "i386.ins" +#include "common.ins" +#include "null.ins" +}; + +ArchPlugin arch_plugin = +{ + "i386", + "elf", + NULL, + _i386_registers, + _i386_instructions, + NULL +}; diff --git a/src/arch/i386.ins b/src/arch/i386.ins new file mode 100644 index 0000000..787d939 --- /dev/null +++ b/src/arch/i386.ins @@ -0,0 +1,4 @@ +/* helpers */ +#define OP1F (1 << AOD_SIZE) + +{ "nop", 0x90, OP1F, AOT_NONE, AOT_NONE, AOT_NONE }, diff --git a/src/arch/i386.reg b/src/arch/i386.reg new file mode 100644 index 0000000..d4e078c --- /dev/null +++ b/src/arch/i386.reg @@ -0,0 +1,40 @@ +REG(ax, 16, 0x00) +REG(ah, 8, 0x01) +REG(al, 8, 0x02) +REG(bx, 16, 0x03) +REG(bh, 8, 0x04) +REG(bl, 8, 0x05) +REG(cx, 16, 0x06) +REG(ch, 8, 0x07) +REG(cl, 8, 0x08) +REG(dx, 16, 0x09) +REG(dh, 8, 0x0a) +REG(dl, 8, 0x0b) +REG(sp, 16, 0x0c) +REG(bp, 16, 0x0d) +REG(si, 16, 0x0e) +REG(di, 16, 0x0f) +REG(cs, 16, 0x10) +REG(ss, 16, 0x11) +REG(ds, 16, 0x12) +REG(es, 16, 0x13) +REG(fs, 16, 0x14) +REG(gs, 16, 0x15) +#ifndef ARCH_i386_real +REG(eax,32, 0x16) +REG(ebx,32, 0x17) +REG(ecx,32, 0x18) +REG(edx,32, 0x19) +REG(esi,32, 0x1a) +REG(edi,32, 0x1b) +REG(esp,32, 0x1c) +REG(ebp,32, 0x1d) +#endif /* !ARCH_i386_real */ +REG(st0,32, 0x1f) +REG(st1,32, 0x20) +REG(st2,32, 0x21) +REG(st3,32, 0x22) +REG(st4,32, 0x23) +REG(st5,32, 0x24) +REG(st6,32, 0x25) +REG(st7,32, 0x26) diff --git a/src/arch/project.conf b/src/arch/project.conf index 6cc6314..1745cd1 100644 --- a/src/arch/project.conf +++ b/src/arch/project.conf @@ -1,8 +1,16 @@ -targets=sparc +targets=i386,sparc cppflags_force=-I ../../include cflags_force=-W cflags=-Wall -fPIC -pedantic -dist=Makefile,common.ins,common.reg,null.ins,sparc.ins,sparc.reg +dist=Makefile,common.ins,i386.ins,i386.reg,null.ins,sparc.ins,sparc.reg + +[i386] +type=plugin +sources=i386.c +install=$(LIBDIR)/asm/arch + +[i386.c] +depends=common.ins,null.ins,i386.ins,i386.reg [sparc] type=plugin @@ -10,4 +18,4 @@ sources=sparc.c install=$(LIBDIR)/asm/arch [sparc.c] -depends=common.ins,common.reg,null.ins,sparc.ins,sparc.reg +depends=common.ins,null.ins,sparc.ins,sparc.reg diff --git a/src/code.c b/src/code.c index d5686c0..297eae6 100644 --- a/src/code.c +++ b/src/code.c @@ -287,11 +287,23 @@ static int _instruction_fixed_register(Code * code, ArchOperand operand, static int _instruction_variable(Code * code, ArchInstruction * ai, AsOperand ** operands, size_t operands_cnt) { + size_t size; + char buf[8]; + #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif - /* FIXME implement */ - return -1; + if((size = AO_GET_SIZE(ai->opcode)) > sizeof(buf)) + return -1; /* XXX report error */ + if(size > 0) + { + memcpy(buf, &ai->value, size); + if(fwrite(&buf, size, 1, code->fp) != 1) + return -error_set_code(1, "%s: %s", code->filename, + strerror(errno)); + } + /* FIXME implement operands */ + return 0; } #if 0 switch(AO_GET_SIZE(ai->opcode)) diff --git a/test/Makefile b/test/Makefile index aebadae..650078f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,4 +1,4 @@ -TARGETS = sparc.o +TARGETS = i386.o sparc.o PREFIX = /usr/local DESTDIR = BINDIR = $(PREFIX)/bin @@ -12,14 +12,20 @@ INSTALL = install all: $(TARGETS) +i386.o_OBJS = i386.o +i386.o_ASFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(ASFLAGS) -a i386 -f flat + sparc.o_OBJS = sparc.o -sparc.o_ASFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(ASFLAGS) -a sparc +sparc.o_ASFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(ASFLAGS) -a sparc -f flat + +i386.o: i386.S ../src/asm + $(AS) $(i386.o_ASFLAGS) -o i386.o i386.S sparc.o: sparc.S ../src/asm $(AS) $(sparc.o_ASFLAGS) -o sparc.o sparc.S clean: - $(RM) -- $(sparc.o_OBJS) + $(RM) -- $(i386.o_OBJS) $(sparc.o_OBJS) distclean: clean $(RM) -- $(TARGETS) diff --git a/test/i386.S b/test/i386.S new file mode 100644 index 0000000..d1836f2 --- /dev/null +++ b/test/i386.S @@ -0,0 +1,2 @@ +.text + nop diff --git a/test/project.conf b/test/project.conf index a487313..230b7ef 100644 --- a/test/project.conf +++ b/test/project.conf @@ -1,11 +1,19 @@ -targets=sparc.o +targets=i386.o,sparc.o as=../src/asm dist=Makefile +[i386.o] +type=object +sources=i386.S + +[i386.S] +asflags=-a i386 -f flat +depends=../src/asm + [sparc.o] type=object sources=sparc.S [sparc.S] -asflags=-a sparc +asflags=-a sparc -f flat depends=../src/asm