mirror of
https://github.com/mikpe/pdp10-tools.git
synced 2026-03-10 04:44:31 +00:00
as: add tunit.{h,c} files :-(
This commit is contained in:
185
as/tunit.c
Normal file
185
as/tunit.c
Normal file
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* tunit.c
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "hashtab.h"
|
||||
#include "tunit.h"
|
||||
|
||||
/*
|
||||
* String hash function, used for both section names and symbol names.
|
||||
*/
|
||||
static uintptr_t string_hash(const char *string)
|
||||
{
|
||||
const unsigned char *s;
|
||||
uintptr_t h;
|
||||
unsigned char c;
|
||||
|
||||
s = (const unsigned char*)string;
|
||||
h = 0;
|
||||
|
||||
while ((c = *s++) != '\0')
|
||||
h = (h << 5) + h + c;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sections hash table.
|
||||
*/
|
||||
static struct section *section_from_hashnode(const struct hashnode *hashnode)
|
||||
{
|
||||
/* hashnode is first in section, so no need to mess with offsetof */
|
||||
return (struct section*)hashnode;
|
||||
}
|
||||
|
||||
static int section_eq(const struct hashnode *hashnode, const void *data)
|
||||
{
|
||||
return strcmp(section_from_hashnode(hashnode)->name, (const char*)data) == 0;
|
||||
}
|
||||
|
||||
void section_init(struct section *section, const char *name)
|
||||
{
|
||||
section->hashnode.hashval = 0;
|
||||
section->hashnode.next = NULL;
|
||||
|
||||
section->name = name;
|
||||
section->head = NULL;
|
||||
section->tailptr = §ion->head;
|
||||
section->dot = 0;
|
||||
section->image_words = NULL;
|
||||
section->st_shndx = 0;
|
||||
section->sh_name = 0;
|
||||
section->sh_type = SHT_NULL;
|
||||
section->sh_flags = 0;
|
||||
section->sh_offset = 0;
|
||||
section->sh_link = 0;
|
||||
section->sh_addralign = 1;
|
||||
section->sh_entsize = 0;
|
||||
}
|
||||
|
||||
struct section *tunit_section_enter(struct tunit *tunit, const char *name)
|
||||
{
|
||||
struct section *section;
|
||||
uintptr_t hashval;
|
||||
|
||||
hashval = string_hash(name);
|
||||
section = section_from_hashnode(hashtab_lookup(&tunit->sections, hashval, name));
|
||||
if (section)
|
||||
return section;
|
||||
|
||||
section = malloc(sizeof *section);
|
||||
if (!section) {
|
||||
fprintf(stderr, "%s: %s: malloc(%zu) failed: %s\n", tunit->progname, __FUNCTION__, sizeof *section, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
section_init(section, name);
|
||||
|
||||
section->hashnode.hashval = hashval;
|
||||
if (hashtab_insert(&tunit->sections, §ion->hashnode) < 0)
|
||||
return NULL;
|
||||
|
||||
return section;
|
||||
}
|
||||
|
||||
static int sections_init(struct tunit *tunit)
|
||||
{
|
||||
struct section *section;
|
||||
|
||||
if (hashtab_init(&tunit->sections, 2, section_eq) < 0)
|
||||
return -1;
|
||||
|
||||
section = tunit_section_enter(tunit, ".text");
|
||||
if (!section)
|
||||
return -1;
|
||||
|
||||
section->sh_type = SHT_PROGBITS;
|
||||
section->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
|
||||
section->sh_addralign = 4; /* XXX: PDP10-specific */
|
||||
|
||||
tunit->cursect = section;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Symbols hash table.
|
||||
*/
|
||||
static struct symbol *symbol_from_hashnode(const struct hashnode *hashnode)
|
||||
{
|
||||
/* hashnode is first in symbol, so no need to mess with offsetof */
|
||||
return (struct symbol*)hashnode;
|
||||
}
|
||||
|
||||
static int symbol_eq(const struct hashnode *hashnode, const void *data)
|
||||
{
|
||||
return strcmp(symbol_from_hashnode(hashnode)->name, (const char*)data) == 0;
|
||||
}
|
||||
|
||||
struct symbol *tunit_symbol_lookup(struct tunit *tunit, const char *name)
|
||||
{
|
||||
uintptr_t hashval;
|
||||
struct symbol *symbol;
|
||||
|
||||
hashval = string_hash(name);
|
||||
symbol = symbol_from_hashnode(hashtab_lookup(&tunit->symbols, hashval, name));
|
||||
if (!symbol)
|
||||
fprintf(stderr, "%s: %s: symbol %s not found\n", tunit->progname, __FUNCTION__, name);
|
||||
return symbol;
|
||||
}
|
||||
|
||||
struct symbol *tunit_symbol_enter(struct tunit *tunit, const char *name)
|
||||
{
|
||||
uintptr_t hashval;
|
||||
struct symbol *symbol;
|
||||
|
||||
hashval = string_hash(name);
|
||||
symbol = symbol_from_hashnode(hashtab_lookup(&tunit->symbols, hashval, name));
|
||||
if (symbol)
|
||||
return symbol;
|
||||
|
||||
symbol = malloc(sizeof *symbol);
|
||||
if (!symbol) {
|
||||
fprintf(stderr, "%s: %s: malloc(%zu) failed: %s\n", tunit->progname, __FUNCTION__, sizeof *symbol, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
symbol->hashnode.hashval = hashval;
|
||||
symbol->name = name;
|
||||
symbol->section = NULL;
|
||||
symbol->defined = 0;
|
||||
symbol->st_value = 0;
|
||||
symbol->st_size = 0;
|
||||
symbol->st_info = 0;
|
||||
symbol->st_name = 0;
|
||||
|
||||
if (hashtab_insert(&tunit->symbols, &symbol->hashnode) < 0)
|
||||
return NULL;
|
||||
|
||||
return symbol;
|
||||
}
|
||||
|
||||
static int symbols_init(struct tunit *tunit)
|
||||
{
|
||||
return hashtab_init(&tunit->symbols, 8, symbol_eq);
|
||||
}
|
||||
|
||||
/*
|
||||
* Translation unit init and fini ops.
|
||||
*/
|
||||
int tunit_init(struct tunit *tunit, const char *progname)
|
||||
{
|
||||
tunit->progname = progname;
|
||||
if (sections_init(tunit) < 0)
|
||||
return -1;
|
||||
if (symbols_init(tunit) < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tunit_fini(struct tunit *tunit)
|
||||
{
|
||||
}
|
||||
95
as/tunit.h
Normal file
95
as/tunit.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* tunit.h
|
||||
*/
|
||||
#ifndef TUNIT_H
|
||||
#define TUNIT_H
|
||||
|
||||
#include "pdp10-elf36.h"
|
||||
#include "hashtab.h"
|
||||
|
||||
/*
|
||||
* A directive, label, or instruction is parsed to a statement, which is
|
||||
* either interpreted immediately or appended to the representation of the
|
||||
* current section.
|
||||
*/
|
||||
|
||||
enum stmt_tag {
|
||||
/* directives */
|
||||
S_DOT_FILE,
|
||||
S_DOT_GLOBL,
|
||||
S_DOT_TEXT,
|
||||
S_DOT_TYPE_FUNCTION,
|
||||
/* non-directives */
|
||||
S_LABEL,
|
||||
S_INSN,
|
||||
};
|
||||
|
||||
struct stmt {
|
||||
struct stmt *next;
|
||||
enum stmt_tag tag;
|
||||
union {
|
||||
struct { /* S_DOT_FILE */
|
||||
const char *text; /* XXX: should be pdp10_uint9_t* */
|
||||
} string;
|
||||
struct { /* S_DOT_GLOBL, S_LABEL, S_DOT_TYPE_FUNCTION */
|
||||
const char *name;
|
||||
} symbol;
|
||||
struct { /* S_INSN */
|
||||
unsigned int opcode;
|
||||
unsigned int accumulator;
|
||||
int at;
|
||||
unsigned int address; /* XXX: relocatable expr */
|
||||
unsigned int indexreg;
|
||||
} insn;
|
||||
} u;
|
||||
};
|
||||
|
||||
struct section {
|
||||
struct hashnode hashnode;
|
||||
const char *name;
|
||||
struct stmt *head, **tailptr;
|
||||
unsigned long dot;
|
||||
pdp10_uint36_t *image_words; /* assigned during assembly */
|
||||
Elf36_Word st_shndx; /* assigned during output */
|
||||
Elf36_Word sh_name; /* assigned during output */
|
||||
Elf36_Word sh_type;
|
||||
Elf36_Word sh_flags;
|
||||
Elf36_Word sh_offset; /* assigned during output */
|
||||
Elf36_Word sh_link; /* assigned during output */
|
||||
Elf36_Word sh_addralign;
|
||||
Elf36_Word sh_entsize; /* assigned during output */
|
||||
};
|
||||
|
||||
void section_init(struct section *section, const char *name);
|
||||
|
||||
struct symbol {
|
||||
struct hashnode hashnode;
|
||||
const char *name;
|
||||
struct section *section; /* NULL if UNDEF or ABS, otherwise non-NULL */
|
||||
unsigned char defined;
|
||||
Elf36_Addr st_value;
|
||||
Elf36_Word st_size;
|
||||
Elf36_Uchar st_info;
|
||||
Elf36_Word st_name; /* assigned during output */
|
||||
};
|
||||
|
||||
/*
|
||||
* The translation unit object is the top-level container for the
|
||||
* representation of the sections, other information collected from
|
||||
* the input, and information synthesized during assembly.
|
||||
*/
|
||||
|
||||
struct tunit {
|
||||
const char *progname;
|
||||
struct hashtab sections;
|
||||
struct section *cursect;
|
||||
struct hashtab symbols;
|
||||
};
|
||||
|
||||
int tunit_init(struct tunit *tunit, const char *progname);
|
||||
void tunit_fini(struct tunit *tunit);
|
||||
struct section *tunit_section_enter(struct tunit *tunit, const char *name);
|
||||
struct symbol *tunit_symbol_lookup(struct tunit *tunit, const char *name);
|
||||
struct symbol *tunit_symbol_enter(struct tunit *tunit, const char *name);
|
||||
|
||||
#endif /* TUNIT_H */
|
||||
Reference in New Issue
Block a user