Added a separate header for the common yasep functions

This commit is contained in:
Pierre Pronchery 2013-01-04 01:36:31 +01:00
parent e1b4b4b2b8
commit 913331e9b6
5 changed files with 189 additions and 148 deletions

View File

@ -109,6 +109,7 @@ dist:
$(PACKAGE)-$(VERSION)/src/arch/sparc.h \
$(PACKAGE)-$(VERSION)/src/arch/sparc.ins \
$(PACKAGE)-$(VERSION)/src/arch/sparc.reg \
$(PACKAGE)-$(VERSION)/src/arch/yasep.h \
$(PACKAGE)-$(VERSION)/src/arch/yasep.ins \
$(PACKAGE)-$(VERSION)/src/arch/yasep.reg \
$(PACKAGE)-$(VERSION)/src/arch/project.conf \

View File

@ -199,13 +199,13 @@ sparc.o: sparc.c common.ins null.ins sparc.h sparc.ins sparc.reg
sparc64.o: sparc64.c common.ins null.ins sparc.h sparc.ins sparc.reg
$(CC) $(sparc64_CFLAGS) -c sparc64.c
yasep.o: yasep.c common.ins null.ins yasep.ins yasep.reg
yasep.o: yasep.c common.ins null.ins yasep.h yasep.ins yasep.reg
$(CC) $(yasep_CFLAGS) -c yasep.c
yasep16.o: yasep16.c common.ins null.ins yasep.ins yasep.reg
yasep16.o: yasep16.c common.ins null.ins yasep.c yasep.h yasep.ins yasep.reg
$(CC) $(yasep16_CFLAGS) -c yasep16.c
yasep32.o: yasep32.c common.ins null.ins yasep.ins yasep.reg
yasep32.o: yasep32.c common.ins null.ins yasep.c yasep.h yasep.ins yasep.reg
$(CC) $(yasep32_CFLAGS) -c yasep32.c
clean:

View File

@ -2,7 +2,7 @@ targets=amd64,arm,armeb,armel,dalvik,i386,i386_real,i486,i586,i686,java,mips,mip
cppflags_force=-I ../../include
cflags_force=-W `pkg-config --cflags libSystem`
cflags=-Wall -g -O2 -fPIC -pedantic
dist=Makefile,amd64.ins,amd64.reg,arm.h,arm.ins,arm.reg,common.ins,dalvik.ins,dalvik.reg,i386.h,i386.ins,i386.reg,i486.ins,i586.ins,i686.ins,i686.reg,mips.h,mips.ins,mips.reg,null.ins,sparc.h,sparc.ins,sparc.reg,yasep.ins,yasep.reg
dist=Makefile,amd64.ins,amd64.reg,arm.h,arm.ins,arm.reg,common.ins,dalvik.ins,dalvik.reg,i386.h,i386.ins,i386.reg,i486.ins,i586.ins,i686.ins,i686.reg,mips.h,mips.ins,mips.reg,null.ins,sparc.h,sparc.ins,sparc.reg,yasep.h,yasep.ins,yasep.reg
[amd64]
type=plugin
@ -138,7 +138,7 @@ sources=yasep.c
install=$(LIBDIR)/Asm/arch
[yasep.c]
depends=common.ins,null.ins,yasep.ins,yasep.reg
depends=common.ins,null.ins,yasep.h,yasep.ins,yasep.reg
[yasep16]
type=plugin
@ -146,7 +146,7 @@ sources=yasep16.c
install=$(LIBDIR)/Asm/arch
[yasep16.c]
depends=common.ins,null.ins,yasep.ins,yasep.reg
depends=common.ins,null.ins,yasep.c,yasep.h,yasep.ins,yasep.reg
[yasep32]
type=plugin
@ -154,4 +154,4 @@ sources=yasep32.c
install=$(LIBDIR)/Asm/arch
[yasep32.c]
depends=common.ins,null.ins,yasep.ins,yasep.reg
depends=common.ins,null.ins,yasep.c,yasep.h,yasep.ins,yasep.reg

View File

@ -27,13 +27,6 @@
/* yasep */
/* private */
/* types */
struct _AsmArchPlugin
{
AsmArchPluginHelper * helper;
};
/* variables */
/* plug-in */
static AsmArchDescription _yasep_description =
@ -85,137 +78,4 @@ AsmArchPluginDefinition arch_plugin =
/* private */
/* functions */
/* plug-in */
/* yasep_init */
static AsmArchPlugin * _yasep_init(AsmArchPluginHelper * helper)
{
AsmArchPlugin * plugin;
if((plugin = object_new(sizeof(*plugin))) == NULL)
return NULL;
plugin->helper = helper;
return plugin;
}
/* yasep_destroy */
static void _yasep_destroy(AsmArchPlugin * plugin)
{
object_delete(plugin);
}
/* yasep_encode */
static int _encode_16(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call);
static int _encode_32(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call);
static int _yasep_encode(AsmArchPlugin * plugin,
AsmArchInstruction * instruction, AsmArchInstructionCall * call)
{
return (instruction->opcode & 0x1)
? _encode_32(plugin, instruction, call)
: _encode_16(plugin, instruction, call);
}
static int _encode_16(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint16_t u16 = instruction->opcode;
AsmArchRegister * ar;
size_t size;
char const * name;
if((instruction->opcode & 0x03) == 0x0) /* RR */
{
name = call->operands[0].value._register.name;
size = AO_GET_SIZE(instruction->op1);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 12;
name = call->operands[1].value._register.name;
size = AO_GET_SIZE(instruction->op2);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 8;
}
else if((instruction->opcode & 0x03) == 0x2) /* IR */
{
u16 |= call->operands[0].value.immediate.value << 12;
name = call->operands[1].value._register.name;
size = AO_GET_SIZE(instruction->op2);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 8;
}
u16 = _htol16(u16);
if(helper->write(helper->arch, &u16, sizeof(u16)) != sizeof(u16))
return -1;
return 0;
}
static int _encode_32(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint32_t opcode = instruction->opcode;
opcode = _htol32(opcode);
if(helper->write(helper->arch, &opcode, sizeof(opcode))
!= sizeof(opcode))
return -1;
return 0;
}
/* yasep_decode */
static int _yasep_decode(AsmArchPlugin * plugin, AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint16_t u16;
uint16_t opcode;
AsmArchInstruction * ai;
AsmArchRegister * ar;
if(helper->read(helper->arch, &u16, sizeof(u16)) != sizeof(u16))
return -1;
u16 = _htol16(u16);
opcode = u16 & 0x00ff;
if((ai = helper->get_instruction_by_opcode(helper->arch, 16, opcode))
== NULL)
return -1;
call->name = ai->name;
if((opcode & 0x3) == 0x0) /* RR */
{
call->operands[0].definition = ai->op1;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0xf000) >> 12) & 0xf,
AO_GET_SIZE(ai->op1))) == NULL)
return -1;
call->operands[0].value._register.name = ar->name;
call->operands[1].definition = ai->op2;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0x0f00) >> 8) & 0xf,
AO_GET_SIZE(ai->op2))) == NULL)
return -1;
call->operands[1].value._register.name = ar->name;
call->operands_cnt = 2;
}
else if((opcode & 0x03) == 0x2) /* IR */
{
call->operands[0].definition = ai->op1;
call->operands[0].value.immediate.value = (u16 & 0xf000) >> 12;
call->operands[1].definition = ai->op2;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0x0f00) >> 8) & 0xf,
AO_GET_SIZE(ai->op2))) == NULL)
return -1;
call->operands[1].value._register.name = ar->name;
call->operands_cnt = 2;
}
return 0;
}
#include "yasep.h"

180
src/arch/yasep.h Normal file
View File

@ -0,0 +1,180 @@
/* $Id$ */
/* Copyright (c) 2013 Pierre Pronchery <khorben@defora.org> */
/* 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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef ASM_ARCH_YASEP_H
# define ASM_ARCH_YASEP_H
# include <System.h>
/* yasep */
/* private */
/* types */
struct _AsmArchPlugin
{
AsmArchPluginHelper * helper;
};
/* prototypes */
/* plug-in */
static AsmArchPlugin * _yasep_init(AsmArchPluginHelper * helper);
static void _yasep_destroy(AsmArchPlugin * plugin);
static int _yasep_encode(AsmArchPlugin * plugin,
AsmArchInstruction * instruction,
AsmArchInstructionCall * call);
static int _yasep_decode(AsmArchPlugin * plugin, AsmArchInstructionCall * call);
/* functions */
/* plug-in */
/* yasep_init */
static AsmArchPlugin * _yasep_init(AsmArchPluginHelper * helper)
{
AsmArchPlugin * plugin;
if((plugin = object_new(sizeof(*plugin))) == NULL)
return NULL;
plugin->helper = helper;
return plugin;
}
/* yasep_destroy */
static void _yasep_destroy(AsmArchPlugin * plugin)
{
object_delete(plugin);
}
/* yasep_encode */
static int _encode_16(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call);
static int _encode_32(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call);
static int _yasep_encode(AsmArchPlugin * plugin,
AsmArchInstruction * instruction, AsmArchInstructionCall * call)
{
return (instruction->opcode & 0x1)
? _encode_32(plugin, instruction, call)
: _encode_16(plugin, instruction, call);
}
static int _encode_16(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint16_t u16 = instruction->opcode;
AsmArchRegister * ar;
size_t size;
char const * name;
if((instruction->opcode & 0x03) == 0x0) /* RR */
{
name = call->operands[0].value._register.name;
size = AO_GET_SIZE(instruction->op1);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 12;
name = call->operands[1].value._register.name;
size = AO_GET_SIZE(instruction->op2);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 8;
}
else if((instruction->opcode & 0x03) == 0x2) /* IR */
{
u16 |= call->operands[0].value.immediate.value << 12;
name = call->operands[1].value._register.name;
size = AO_GET_SIZE(instruction->op2);
if((ar = helper->get_register_by_name_size(helper->arch, name,
size)) == NULL)
return -1;
u16 |= ar->id << 8;
}
u16 = _htol16(u16);
if(helper->write(helper->arch, &u16, sizeof(u16)) != sizeof(u16))
return -1;
return 0;
}
static int _encode_32(AsmArchPlugin * plugin, AsmArchInstruction * instruction,
AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint32_t opcode = instruction->opcode;
opcode = _htol32(opcode);
if(helper->write(helper->arch, &opcode, sizeof(opcode))
!= sizeof(opcode))
return -1;
return 0;
}
/* yasep_decode */
static int _yasep_decode(AsmArchPlugin * plugin, AsmArchInstructionCall * call)
{
AsmArchPluginHelper * helper = plugin->helper;
uint16_t u16;
uint16_t opcode;
AsmArchInstruction * ai;
AsmArchRegister * ar;
if(helper->read(helper->arch, &u16, sizeof(u16)) != sizeof(u16))
return -1;
u16 = _htol16(u16);
opcode = u16 & 0x00ff;
if((ai = helper->get_instruction_by_opcode(helper->arch, 16, opcode))
== NULL)
return -1;
call->name = ai->name;
if((opcode & 0x3) == 0x0) /* RR */
{
call->operands[0].definition = ai->op1;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0xf000) >> 12) & 0xf,
AO_GET_SIZE(ai->op1))) == NULL)
return -1;
call->operands[0].value._register.name = ar->name;
call->operands[1].definition = ai->op2;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0x0f00) >> 8) & 0xf,
AO_GET_SIZE(ai->op2))) == NULL)
return -1;
call->operands[1].value._register.name = ar->name;
call->operands_cnt = 2;
}
else if((opcode & 0x03) == 0x2) /* IR */
{
call->operands[0].definition = ai->op1;
call->operands[0].value.immediate.value = (u16 & 0xf000) >> 12;
call->operands[1].definition = ai->op2;
if((ar = helper->get_register_by_id_size(helper->arch,
((u16 & 0x0f00) >> 8) & 0xf,
AO_GET_SIZE(ai->op2))) == NULL)
return -1;
call->operands[1].value._register.name = ar->name;
call->operands_cnt = 2;
}
return 0;
}
#endif /* ASM_ARCH_YASEP_H */