Skip to content

Commit

Permalink
Merge branch 'add-rv64a'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyuria committed Jun 2, 2024
2 parents 8db0a2d + 4089843 commit 74e81bf
Show file tree
Hide file tree
Showing 31 changed files with 891 additions and 504 deletions.
22 changes: 11 additions & 11 deletions h/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
#include <stdbool.h>

#include "elf/output.h"
#include "form/generic.h"
#include "form/instructions.h"

struct instruction_t {
struct formation_t formation;
struct args_t args;
struct sectionpos_t position;
struct instruction {
struct formation formation;
struct args args;
struct sectionpos position;
size_t line;
};

struct rawdata_t {
struct rawdata {
void *data;
size_t size;
struct sectionpos_t position;
struct sectionpos position;
size_t line;
};

int add_instruction(struct instruction_t);
int add_data(struct rawdata_t);
int add_instruction(struct instruction);
int add_data(struct rawdata);

int write_all(void);

int write_all_instructions(void);
int write_instruction(struct instruction_t);
int write_instruction(struct instruction);

int write_all_data(void);
int write_data(struct rawdata_t);
int write_data(struct rawdata);

void free_instructions(void);
70 changes: 35 additions & 35 deletions h/elf/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@

#define ELF_IDENTSIZE 16

struct elf64header_t {
unsigned char ident[ELF_IDENTSIZE];
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry;
uint64_t phoffset;
uint64_t shoffset;
uint32_t flags;
uint16_t headersize;
uint16_t phentrysize;
uint16_t phcount;
uint16_t shentrysize;
uint16_t shcount;
uint16_t shstrindex;
struct elf64header {
unsigned char ident[ELF_IDENTSIZE];
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry;
uint64_t phoffset;
uint64_t shoffset;
uint32_t flags;
uint16_t headersize;
uint16_t phentrysize;
uint16_t phcount;
uint16_t shentrysize;
uint16_t shcount;
uint16_t shstrindex;
};

struct elf64sectionheader_t {
uint32_t name;
uint32_t type;
uint64_t flags;
uint64_t addr;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t addralign;
uint64_t entrysize;
struct elf64sectionheader {
uint32_t name;
uint32_t type;
uint64_t flags;
uint64_t addr;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t addralign;
uint64_t entrysize;
};

struct elf64header_t new_elf64header(void);
struct elf64sectionheader_t new_elf64sectionheader(void);
struct elf64header new_elf64header(void);
struct elf64sectionheader new_elf64sectionheader(void);

struct elf64sym_t {
uint32_t name;
unsigned char info;
unsigned char other;
uint16_t shndx;
uint64_t value;
uint64_t size;
struct elf64sym {
uint32_t name;
unsigned char info;
unsigned char other;
uint16_t shndx;
uint64_t value;
uint64_t size;
};
44 changes: 22 additions & 22 deletions h/elf/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
#include <stdint.h>
#include <stdio.h>

enum sections_e {
section_null,
section_strtab,
section_text,
section_data,
section_symtab,
SECTION_COUNT
enum sections {
SECTION_NULL,
SECTION_STRTAB,
SECTION_TEXT,
SECTION_DATA,
SECTION_SYMTAB,
SECTION_COUNT
};

struct sectionpos_t {
enum sections_e section;
size_t offset;
struct sectionpos {
enum sections section;
size_t offset;
};

struct section_t {
size_t offset;
size_t size;
char *contents;
uint32_t nameoffset;
struct section {
size_t offset;
size_t size;
char *contents;
uint32_t nameoffset;
};

extern enum sections_e outputsection;
extern enum sections outputsection;

void change_output(enum sections_e);
void change_output(enum sections);

struct sectionpos_t get_outputpos(void);
void inc_outputsize(enum sections_e, size_t);
void set_section(enum sections_e);
struct sectionpos get_outputpos(void);
void inc_outputsize(enum sections, size_t);
void set_section(enum sections);

size_t calc_fileoffset(struct sectionpos_t);
size_t calc_fileoffset(struct sectionpos);

void calc_strtab(void);
int fill_strtab(void);
Expand All @@ -42,6 +42,6 @@ int fill_symtab(void);

int alloc_output(void);

size_t write_sectiondata(const void *, size_t, struct sectionpos_t);
size_t write_sectiondata(const void *, size_t, struct sectionpos);

int flush_output(FILE *);
5 changes: 5 additions & 0 deletions h/form/atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include "form/instructions.h"

extern const struct formation rv32a[];
extern const struct formation rv64a[];
58 changes: 26 additions & 32 deletions h/form/rv32i.h → h/form/base.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
#pragma once
#include "form/instructions.h"

extern const struct formation_t rv32i[];
extern const struct formation rv32i[];
extern const struct formation rv64i[];

enum load_pseudo {
load_imm,
load_addr,
LOAD_IMM,
LOAD_ADDR,
};
enum math_pseudo {
math_mv,
math_not,
math_neg,
math_negw,
math_sextw,
MATH_MV,
MATH_NOT,
MATH_NEG,
MATH_NEGW,
MATH_SEXTW,
};
enum setif_pseudo {
setif_eqz,
setif_nez,
setif_ltz,
setif_gtz,
SETIF_EQZ,
SETIF_NEZ,
SETIF_LTZ,
SETIF_GTZ,
};
enum branchifz_pseudo {
branchifz_eqz,
branchifz_nez,
branchifz_lez,
branchifz_gez,
branchifz_ltz,
branchifz_gtz,
BRANCHIFZ_EQZ,
BRANCHIFZ_NEZ,
BRANCHIFZ_LEZ,
BRANCHIFZ_GEZ,
BRANCHIFZ_LTZ,
BRANCHIFZ_GTZ,
};
enum branchifr_pseudo {
branchifr_gt,
branchifr_le,
branchifr_gtu,
branchifr_leu,
BRANCHIFR_GT = 0x4,
BRANCHIFR_LE = 0x5,
BRANCHIFR_GTU = 0x6,
BRANCHIFR_LEU = 0x7,
};
enum jump_pseudo {
jump_j,
jump_jr,
jump_ret,
JUMP_J,
JUMP_JR,
JUMP_RET,
};

/* shortcut instructions bytecode generation */
Expand All @@ -52,13 +53,6 @@ form_handler form_jr;
form_handler form_ret;

/* basic integer instruction type bytecode generation */
form_handler form_rtype;
form_handler form_itype;
form_handler form_itype2;
form_handler form_stype;
form_handler form_btype;
form_handler form_utype;
form_handler form_jtype;
form_handler form_syscall;

/* individual instruction bytecode generation */
Expand Down
17 changes: 13 additions & 4 deletions h/form/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define OP_SYSTEM 0x73
#define OP_LUI 0x37
#define OP_AUIPC 0x17
#define OP_AMO 0x2F

#define END_FORMATION \
{ \
Expand All @@ -26,14 +27,22 @@
} \
}

struct bytecode_t {
struct bytecode {
size_t size;
unsigned char *data;
};

extern const struct bytecode_t error_bytecode;
extern const struct bytecode error_bytecode;

/* helper functions */
struct bytecode_t form_empty_bytecode(void);
struct bytecode form_empty_bytecode(void);

int32_t calc_symbol_offset(const struct symbol_t *, size_t);
int32_t calc_symbol_offset(const struct symbol *, size_t);

form_handler form_rtype;
form_handler form_itype;
form_handler form_itype2;
form_handler form_stype;
form_handler form_btype;
form_handler form_utype;
form_handler form_jtype;
24 changes: 12 additions & 12 deletions h/form/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
#include <stdint.h>
#include <stdlib.h>

struct formation_t;
struct args_t;
struct idata_t;
typedef struct bytecode_t(form_handler)(const char *, struct idata_t,
struct args_t, size_t);
typedef struct args_t arg_parser(char *);
struct formation;
struct args;
struct idata;
typedef struct bytecode(form_handler)(const char *, struct idata, struct args,
size_t);
typedef struct args arg_parser(char *);

struct formation_t {
struct formation {
const char *name;
form_handler *form_handler;
arg_parser *arg_handler;
struct idata_t {
struct idata {
size_t sz;
uint8_t opcode;
uint8_t funct3;
uint8_t funct7;
} idata;
};

struct args_t {
struct args {
uint8_t rd;
uint8_t rs1;
uint8_t rs2;
int32_t imm;
struct symbol_t *sym;
struct symbol *sym;
};

extern const struct args_t empty_args;
extern const struct args empty_args;

struct formation_t parse_form(const char *instruction);
struct formation parse_form(const char *instruction);
4 changes: 0 additions & 4 deletions h/form/rv64i.h

This file was deleted.

4 changes: 2 additions & 2 deletions h/generation.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* general instruction generation */
void parse_file(FILE *, FILE *);
int parse_line(char *, struct sectionpos_t);
int parse_line(char *, struct sectionpos);

int symbol_forward_declare(char *);
int parse_label(char *, struct sectionpos_t);
int parse_label(char *, struct sectionpos);
6 changes: 4 additions & 2 deletions h/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "elf/output.h"
#include "form/instructions.h"

extern const struct args_t empty_args;
extern const struct args empty_args;

arg_parser parse_rtype;
arg_parser parse_itype;
Expand All @@ -24,5 +24,7 @@ arg_parser parse_la;
arg_parser parse_j;
arg_parser parse_jr;
arg_parser parse_ftso;
arg_parser parse_al;
arg_parser parse_as;

int parse_asm(const char *, struct sectionpos_t);
int parse_asm(const char *, struct sectionpos);
Loading

0 comments on commit 74e81bf

Please sign in to comment.