Initial revision

This commit is contained in:
kaashoek 1987-11-20 11:12:07 +00:00
parent 9947059dcc
commit 75e0c5f7ed
36 changed files with 2660 additions and 0 deletions

View file

@ -0,0 +1,12 @@
EM=/proj/em/Work
OFILES = dist.o error.o
IFILES = -I$(EM)/h -I$(EM)/modules/h
CC = cc
.c.o :
$(CC) $(IFILES) -c $<
em_parser : $(OFILES)
touch em_parser

View file

@ -0,0 +1,3 @@
#define STRING 0
#define ARITH 1
#define INT 2

View file

@ -0,0 +1,11 @@
#include "em.h"
arith cur_pos;
back_patch()
{
}
save_output()
{
}

View file

@ -0,0 +1,66 @@
#define SEGBSS 0
#define SEGHOL 1
#define SEGCON 2
#define SEGROM 3
#define SEGTXT 4
#define NOSEG 5
#define UNKNOWN (-1)
#define TRUE 1
#define FALSE 0
#define STRING 0
#define LABEL 1
#define ARITH 2
#define INT 3
#define MAX_ARGS 4
#define NO_ARGS 0
#define C_ARG 1
#define D_ARG 2
#define F_ARG 3
#define L_ARG 4
#define N_ARG 5
#define O_ARG 6
#define R_ARG 7
#define S_ARG 8
#define Z_ARG 9
#define W_ARG 10
#define EXTERNAL 11
#define EXTERNAL_DNAM 12
#define EXTERNAL_DLB 13
#define P_ARG 14
#define B_ARG 15
#define STRING_ARITH 16
#define BSS_EXT_DNAM 17
#define BSS_EXT_DLB 18
#define BSS_STR_AR 19
#define BSS_W_ARG 20
#define BSS_PNAM 21
#define BSS_ILB 22
#define ID_N_ARG 23
#define ARITH_ARITH 24
#define EXT_DNAM 25
#define EXT_DLB 26
#define ONE_INT 27
#define _STRING 28
typedef struct {
char *header;
int arg_type[MAX_ARGS]; /* Zoals ze eruit gaan!! */
char *arg_id[MAX_ARGS], *arg_conv[MAX_ARGS];
} t_C_info;
typedef struct {
char *lexeme;
int C_instr_type;
} t_token;
extern t_C_info C_info[], *C_instr_info;
extern int storage_default[4][3], segment;

View file

@ -0,0 +1,10 @@
#define SEGBSS 0
#define SEGHOL 1
#define SEGCON 2
#define SEGROM 3
#define SEGTXT 4
#define STRING 0
#define LABEL 1
#define ARITH 2
#define INT 3

View file

@ -0,0 +1,10 @@
#include <system.h>
error( fmt, argv)
char *fmt;
int argv;
{
fprint( STDERR, "!! ERROR : ");
doprnt( STDERR, fmt, &argv);
fprint( STDERR, " !!\n");
}

View file

@ -0,0 +1,436 @@
#include "decl.h"
t_C_info C_info[] = {
/* NO_ARGS */ { "()\n{\n",
{ 0, 0, 0, 0},
{ 0, 0, 0, 0},
{ 0, 0, 0, 0}},
/* C_ARG */ { "( c)\narith c;\n{\n",
{ ARITH, 0, 0, 0},
{ "c", 0, 0, 0},
{ "c", 0, 0, 0}},
/* D_ARG */ { "( d)\narith d;\n{\n",
{ ARITH, 0, 0, 0},
{ "d", 0, 0, 0},
{ "d", 0, 0, 0}},
/* F_ARG */ { "( f)\narith f;\n{\n",
{ ARITH, 0, 0, 0},
{ "f", 0, 0, 0},
{ "f", 0, 0, 0}},
/* L_ARG */ { "( l)\narith l;\n{\n",
{ ARITH, 0, 0, 0},
{ "l", 0, 0, 0},
{ "(l >= 0 ? l + EM_BSIZE : l)", 0, 0, 0}},
/* N_ARG */ { "( n)\narith n;\n{\n",
{ ARITH, 0, 0, 0},
{ "n", 0, 0, 0},
{ "n", 0, 0, 0}},
/* O_ARG */ { "( o)\narith o;\n{\n",
{ ARITH, 0, 0, 0},
{ "o", 0, 0, 0},
{ "o", 0, 0, 0}},
/* R_ARG */ { "( r)\narith r;\n{\n",
{ ARITH, 0, 0, 0},
{ "r", 0, 0, 0},
{ "r", 0, 0, 0}},
/* S_ARG */ { "( s)\narith s;\n{\n",
{ ARITH, 0, 0, 0},
{ "s", 0, 0, 0},
{ "s", 0, 0, 0}},
/* Z_ARG */ { "( z)\narith z;\n{\n",
{ ARITH, 0, 0, 0},
{ "z", 0, 0, 0},
{ "z", 0, 0, 0}},
/* W_ARG */ { "( w)\narith w;\n{\n",
{ ARITH, 0, 0, 0},
{ "w", 0, 0, 0},
{ "w", 0, 0, 0}},
/* EXTERNAL */ { "( g)\narith g;\n{\n",
{ STRING, ARITH, 0, 0},
{ "FAKE-ARGUMENT", "g", 0, 0},
{ "extnd_hol( holno)", "g", 0, 0}},
/* EXTERNAL_DNAM*/ { "( g, o)\nchar *g;\narith o;\n{\n",
{ STRING, ARITH, 0, 0},
{ "g", "o", 0, 0},
{ "extnd_dnam( g)", "o", 0, 0}},
/* EXTERNAL_DLB */ { "( g, o)\nchar *g;\narith o;\n{\n",
{ STRING, ARITH, 0, 0},
{ "g", "o", 0, 0},
{ "extnd_dlb( g)", "o", 0, 0}},
/* P_ARG */ { "( p)\nchar *p;\n{\n",
{ STRING, 0, 0, 0},
{ "p", 0, 0, 0},
{ "extnd_name( p)", 0, 0, 0}},
/* B_ARG */ { "( b)\nlabel b;\n{\n",
{ STRING, 0, 0, 0},
{ "b", 0, 0, 0},
{ "extnd_ilb( b)", 0, 0, 0}},
/* STRING+ARITH */ { "( val, sz)\nchar *val;\narith sz;\n{\n",
{ STRING, ARITH, 0, 0},
{ "val", "sz", 0, 0},
{ "val", "sz", 0, 0}},
/* BSS_EXT_DNAM */ { "( n, g, o, i)\nchar *g;\narith n, o;\nint i;\n{\n",
{ ARITH, STRING, ARITH, INT},
{ "n", "g", "o", "i"},
{ "n", "extnd_dnam( g)", "o", "i"}},
/* BSS_EXT_DLB */ { "( n, g, o, i)\nchar *g;\narith n, o;\nint i;\n{\n",
{ ARITH, STRING, ARITH, INT},
{ "n", "g", "o", "i"},
{ "n", "extnd_dlb( g)", "o", "i"}},
/* BSS_STR+AR */ { "( n, val, sz, i)\nchar *val;\narith n, sz;\nint i;\n{\n",
{ ARITH, STRING, ARITH, INT},
{ "n", "val", "sz", "i"},
{ "n", "val", "sz", "i"}},
/* BSS_W_ARG */ { "( n, w, i)\narith n, w;\nint i;\n{\n",
{ ARITH, ARITH, INT, 0},
{ "n", "w", "i", 0},
{ "n", "w", "i", 0}},
/* BSS_PNAM */ { "( n, p, i)\narith n;\nchar *p;int i;\n\n{\n",
{ ARITH, STRING, INT, 0},
{ "n", "p", "i", 0},
{ "n", "extnd_name( p)", "i", 0}},
/* BSS_ILB */ { "( n, b, i)\narith n;\nlabel b;int i;\n\n{\n",
{ ARITH, STRING, INT, 0},
{ "n", "b", "i", 0},
{ "n", "extnd_ilb( b)", "i", 0}},
/* ID+N_ARG */ { "( p, n)\nchar *p;\narith n;\n{\n",
{ STRING, ARITH, 0, 0},
{ "p", "n", 0, 0},
{ "extnd_name( p)", "n", 0, 0}},
/* ARITH+ARITH */ { "( n, w)\narith n, w;\n{\n",
{ ARITH, ARITH, 0, 0},
{ "n", "w", 0, 0},
{ "n", "w", 0, 0}},
/* EXT_DNAM */ { "( g)\nchar *g;\n{\n",
{ STRING, 0, 0, 0},
{ "g", 0, 0, 0},
{ "extnd_dnam( g)", 0, 0, 0}},
/* EXT_DLB */ { "( g)\nchar *g;\n{\n",
{ STRING, 0, 0, 0},
{ "g", 0, 0, 0},
{ "extnd_dlb( g)", 0, 0, 0}},
/* ONE_INT */ { "( i)\nint i;\n{\n",
{ INT, 0, 0, 0},
{ "i", 0, 0, 0},
{ "i", 0, 0, 0}},
/* _STRING */ { "( str)\nchar *str;\n{\n",
{ STRING, 0, 0, 0},
{ "str", 0, 0, 0},
{ "str", 0, 0, 0}}
}, *C_instr_info;
#define N_INSTR 269
struct { char *name; int class, segment;}
EM_instr[ N_INSTR ] = {
{ "C_aar", W_ARG, SEGTXT},
{ "C_aar_narg", NO_ARGS, SEGTXT},
{ "C_adf", W_ARG, SEGTXT},
{ "C_adf_narg", NO_ARGS, SEGTXT},
{ "C_adi", W_ARG, SEGTXT},
{ "C_adi_narg", NO_ARGS, SEGTXT},
{ "C_adp", F_ARG, SEGTXT},
{ "C_ads", W_ARG, SEGTXT},
{ "C_ads_narg", NO_ARGS, SEGTXT},
{ "C_adu", W_ARG, SEGTXT},
{ "C_adu_narg", NO_ARGS, SEGTXT},
{ "C_and", W_ARG, SEGTXT},
{ "C_and_narg", NO_ARGS, SEGTXT},
{ "C_asp", F_ARG, SEGTXT},
{ "C_ass", W_ARG, SEGTXT},
{ "C_ass_narg", NO_ARGS, SEGTXT},
{ "C_beq", B_ARG, SEGTXT},
{ "C_bge", B_ARG, SEGTXT},
{ "C_bgt", B_ARG, SEGTXT},
{ "C_ble", B_ARG, SEGTXT},
{ "C_blm", Z_ARG, SEGTXT},
{ "C_bls", W_ARG, SEGTXT},
{ "C_bls_narg", NO_ARGS, SEGTXT},
{ "C_blt", B_ARG, SEGTXT},
{ "C_bne", B_ARG, SEGTXT},
{ "C_bra", B_ARG, SEGTXT},
{ "C_bss_cst", BSS_W_ARG, SEGBSS},
{ "C_bss_dlb", BSS_EXT_DLB, SEGBSS},
{ "C_bss_dnam", BSS_EXT_DNAM, SEGBSS},
{ "C_bss_fcon", BSS_STR_AR, SEGBSS},
{ "C_bss_icon", BSS_STR_AR, SEGBSS},
{ "C_bss_ilb", BSS_ILB, SEGBSS},
{ "C_bss_pnam", BSS_PNAM, SEGBSS},
{ "C_bss_ucon", BSS_STR_AR, SEGBSS},
{ "C_cai", NO_ARGS, SEGTXT},
{ "C_cal", P_ARG, SEGTXT},
{ "C_cff", NO_ARGS, SEGTXT},
{ "C_cfi", NO_ARGS, SEGTXT},
{ "C_cfu", NO_ARGS, SEGTXT},
{ "C_cif", NO_ARGS, SEGTXT},
{ "C_cii", NO_ARGS, SEGTXT},
{ "C_ciu", NO_ARGS, SEGTXT},
{ "C_close", NO_ARGS, NOSEG},
{ "C_cmf", W_ARG, SEGTXT},
{ "C_cmf_narg", NO_ARGS, SEGTXT},
{ "C_cmi", W_ARG, SEGTXT},
{ "C_cmi_narg", NO_ARGS, SEGTXT},
{ "C_cmp", NO_ARGS, SEGTXT},
{ "C_cms", W_ARG, SEGTXT},
{ "C_cms_narg", NO_ARGS, SEGTXT},
{ "C_cmu", W_ARG, SEGTXT},
{ "C_cmu_narg", NO_ARGS, SEGTXT},
{ "C_com", W_ARG, SEGTXT},
{ "C_com_narg", NO_ARGS, SEGTXT},
{ "C_con_cst", C_ARG, SEGCON},
{ "C_con_dlb", EXTERNAL_DLB, SEGCON},
{ "C_con_dnam", EXTERNAL_DNAM, SEGCON},
{ "C_con_fcon", STRING_ARITH, SEGCON},
{ "C_con_icon", STRING_ARITH, SEGCON},
{ "C_con_ilb", B_ARG, SEGCON},
{ "C_con_pnam", P_ARG, SEGCON},
{ "C_con_scon", STRING_ARITH, SEGCON},
{ "C_con_ucon", STRING_ARITH, SEGCON},
{ "C_csa", W_ARG, SEGTXT},
{ "C_csa_narg", NO_ARGS, SEGTXT},
{ "C_csb", W_ARG, SEGTXT},
{ "C_csb_narg", NO_ARGS, SEGTXT},
{ "C_cst", C_ARG, NOSEG},
{ "C_cuf", NO_ARGS, SEGTXT},
{ "C_cui", NO_ARGS, SEGTXT},
{ "C_cuu", NO_ARGS, SEGTXT},
{ "C_dch", NO_ARGS, SEGTXT},
{ "C_dec", NO_ARGS, SEGTXT},
{ "C_dee", EXTERNAL, SEGTXT},
{ "C_dee_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_dee_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_del", L_ARG, SEGTXT},
{ "C_df_dlb", EXT_DLB, NOSEG},
{ "C_df_dnam", EXT_DNAM, NOSEG},
{ "C_df_ilb", B_ARG, SEGTXT},
{ "C_dlb", STRING_ARITH, SEGTXT},
{ "C_dnam", STRING_ARITH, SEGTXT},
{ "C_dup", S_ARG, SEGTXT},
{ "C_dus", W_ARG, SEGTXT},
{ "C_dus_narg", NO_ARGS, SEGTXT},
{ "C_dvf", W_ARG, SEGTXT},
{ "C_dvf_narg", NO_ARGS, SEGTXT},
{ "C_dvi", W_ARG, SEGTXT},
{ "C_dvi_narg", NO_ARGS, SEGTXT},
{ "C_dvu", W_ARG, SEGTXT},
{ "C_dvu_narg", NO_ARGS, SEGTXT},
{ "C_end", Z_ARG, SEGTXT},
{ "C_end_narg", NO_ARGS, SEGTXT},
{ "C_exa_dlb", EXT_DLB, NOSEG},
{ "C_exa_dnam", EXT_DNAM, NOSEG},
{ "C_exc", ARITH_ARITH, NOSEG},
{ "C_exg", W_ARG, SEGTXT},
{ "C_exg_narg", NO_ARGS, SEGTXT},
{ "C_exp", P_ARG, SEGTXT},
{ "C_fcon", STRING_ARITH, SEGTXT},
{ "C_fef", W_ARG, SEGTXT},
{ "C_fef_narg", NO_ARGS, SEGTXT},
{ "C_fif", W_ARG, SEGTXT},
{ "C_fif_narg", NO_ARGS, SEGTXT},
{ "C_fil", EXTERNAL, SEGTXT},
{ "C_fil_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_fil_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_gto", EXTERNAL, SEGTXT},
{ "C_gto_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_gto_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_hol_cst", BSS_W_ARG, SEGHOL},
{ "C_hol_dlb", BSS_EXT_DLB, SEGHOL},
{ "C_hol_dnam", BSS_EXT_DNAM, SEGHOL},
{ "C_hol_fcon", BSS_STR_AR, SEGHOL},
{ "C_hol_icon", BSS_STR_AR, SEGHOL},
{ "C_hol_ilb", BSS_ILB, SEGHOL},
{ "C_hol_pnam", BSS_PNAM, SEGHOL},
{ "C_hol_ucon", BSS_STR_AR, SEGHOL},
{ "C_icon", STRING_ARITH, SEGTXT},
{ "C_ilb", B_ARG, SEGTXT},
{ "C_ina_dlb", EXT_DLB, SEGTXT},
{ "C_ina_dnam", EXT_DNAM, SEGTXT},
{ "C_inc", NO_ARGS, SEGTXT},
{ "C_ine", EXTERNAL, SEGTXT},
{ "C_ine_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_ine_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_init", ARITH_ARITH, SEGTXT},
{ "C_inl", L_ARG, SEGTXT},
{ "C_inn", W_ARG, SEGTXT},
{ "C_inn_narg", NO_ARGS, SEGTXT},
{ "C_inp", P_ARG, NOSEG},
{ "C_ior", W_ARG, SEGTXT},
{ "C_ior_narg", NO_ARGS, SEGTXT},
{ "C_lae", EXTERNAL, SEGTXT},
{ "C_lae_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_lae_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_lal", L_ARG, SEGTXT},
{ "C_lar", W_ARG, SEGTXT},
{ "C_lar_narg", NO_ARGS, SEGTXT},
{ "C_ldc", D_ARG, SEGTXT},
{ "C_lde", EXTERNAL, SEGTXT},
{ "C_lde_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_lde_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_ldf", F_ARG, SEGTXT},
{ "C_ldl", L_ARG, SEGTXT},
{ "C_lfr", S_ARG, SEGTXT},
{ "C_lil", L_ARG, SEGTXT},
{ "C_lim", NO_ARGS, SEGTXT},
{ "C_lin", N_ARG, SEGTXT},
{ "C_lni", NO_ARGS, SEGTXT},
{ "C_loc", C_ARG, SEGTXT},
{ "C_loe", EXTERNAL, SEGTXT},
{ "C_loe_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_loe_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_lof", F_ARG, SEGTXT},
{ "C_loi", O_ARG, SEGTXT},
{ "C_lol", L_ARG, SEGTXT},
{ "C_lor", R_ARG, SEGTXT},
{ "C_los", W_ARG, SEGTXT},
{ "C_los_narg", NO_ARGS, SEGTXT},
{ "C_lpb", NO_ARGS, SEGTXT},
{ "C_lpi", P_ARG, SEGTXT},
{ "C_lxa", N_ARG, SEGTXT},
{ "C_lxl", N_ARG, SEGTXT},
{ "C_magic", NO_ARGS, NOSEG},
{ "C_mes_begin", ONE_INT, NOSEG},
{ "C_mes_end", NO_ARGS, NOSEG},
{ "C_mlf", W_ARG, SEGTXT},
{ "C_mlf_narg", NO_ARGS, SEGTXT},
{ "C_mli", W_ARG, SEGTXT},
{ "C_mli_narg", NO_ARGS, SEGTXT},
{ "C_mlu", W_ARG, SEGTXT},
{ "C_mlu_narg", NO_ARGS, SEGTXT},
{ "C_mon", NO_ARGS, SEGTXT},
{ "C_ngf", W_ARG, SEGTXT},
{ "C_ngf_narg", NO_ARGS, SEGTXT},
{ "C_ngi", W_ARG, SEGTXT},
{ "C_ngi_narg", NO_ARGS, SEGTXT},
{ "C_nop", NO_ARGS, SEGTXT},
{ "C_open", _STRING, NOSEG},
{ "C_pnam", P_ARG, NOSEG},
{ "C_pro", P_ARG, SEGTXT},
{ "C_pro_narg", NO_ARGS, SEGTXT},
{ "C_rck", W_ARG, SEGTXT},
{ "C_rck_narg", NO_ARGS, SEGTXT},
{ "C_ret", Z_ARG, SEGTXT},
{ "C_rmi", W_ARG, SEGTXT},
{ "C_rmi_narg", NO_ARGS, SEGTXT},
{ "C_rmu", W_ARG, SEGTXT},
{ "C_rmu_narg", NO_ARGS, SEGTXT},
{ "C_rol", W_ARG, SEGTXT},
{ "C_rol_narg", NO_ARGS, SEGTXT},
{ "C_rom_cst", C_ARG, SEGROM},
{ "C_rom_dlb", EXTERNAL_DLB, SEGROM},
{ "C_rom_dnam", EXTERNAL_DNAM, SEGROM},
{ "C_rom_fcon", STRING_ARITH, SEGROM},
{ "C_rom_icon", STRING_ARITH, SEGROM},
{ "C_rom_ilb", B_ARG, SEGROM},
{ "C_rom_pnam", P_ARG, SEGROM},
{ "C_rom_scon", STRING_ARITH, SEGROM},
{ "C_rom_ucon", STRING_ARITH, SEGROM},
{ "C_ror", W_ARG, SEGTXT},
{ "C_ror_narg", NO_ARGS, SEGTXT},
{ "C_rtt", NO_ARGS, SEGTXT},
{ "C_sar", W_ARG, SEGTXT},
{ "C_sar_narg", NO_ARGS, SEGTXT},
{ "C_sbf", W_ARG, SEGTXT},
{ "C_sbf_narg", NO_ARGS, SEGTXT},
{ "C_sbi", W_ARG, SEGTXT},
{ "C_sbi_narg", NO_ARGS, SEGTXT},
{ "C_sbs", W_ARG, SEGTXT},
{ "C_sbs_narg", NO_ARGS, SEGTXT},
{ "C_sbu", W_ARG, SEGTXT},
{ "C_sbu_narg", NO_ARGS, SEGTXT},
{ "C_scon", STRING_ARITH, NOSEG},
{ "C_sde", EXTERNAL, SEGTXT},
{ "C_sde_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_sde_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_sdf", F_ARG, SEGTXT},
{ "C_sdl", L_ARG, SEGTXT},
{ "C_set", W_ARG, SEGTXT},
{ "C_set_narg", NO_ARGS, SEGTXT},
{ "C_sig", NO_ARGS, SEGTXT},
{ "C_sil", L_ARG, SEGTXT},
{ "C_sim", NO_ARGS, SEGTXT},
{ "C_sli", W_ARG, SEGTXT},
{ "C_sli_narg", NO_ARGS, SEGTXT},
{ "C_slu", W_ARG, SEGTXT},
{ "C_slu_narg", NO_ARGS, SEGTXT},
{ "C_sri", W_ARG, SEGTXT},
{ "C_sri_narg", NO_ARGS, SEGTXT},
{ "C_sru", W_ARG, SEGTXT},
{ "C_sru_narg", NO_ARGS, SEGTXT},
{ "C_ste", EXTERNAL, SEGTXT},
{ "C_ste_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_ste_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_stf", F_ARG, SEGTXT},
{ "C_sti", O_ARG, SEGTXT},
{ "C_stl", L_ARG, SEGTXT},
{ "C_str", R_ARG, SEGTXT},
{ "C_sts", W_ARG, SEGTXT},
{ "C_sts_narg", NO_ARGS, SEGTXT},
{ "C_teq", NO_ARGS, SEGTXT},
{ "C_tge", NO_ARGS, SEGTXT},
{ "C_tgt", NO_ARGS, SEGTXT},
{ "C_tle", NO_ARGS, SEGTXT},
{ "C_tlt", NO_ARGS, SEGTXT},
{ "C_tne", NO_ARGS, SEGTXT},
{ "C_trp", NO_ARGS, SEGTXT},
{ "C_ucon", STRING_ARITH, NOSEG},
{ "C_xor", W_ARG, SEGTXT},
{ "C_xor_narg", NO_ARGS, SEGTXT},
{ "C_zeq", B_ARG, SEGTXT},
{ "C_zer", W_ARG, SEGTXT},
{ "C_zer_narg", NO_ARGS, SEGTXT},
{ "C_zge", B_ARG, SEGTXT},
{ "C_zgt", B_ARG, SEGTXT},
{ "C_zle", B_ARG, SEGTXT},
{ "C_zlt", B_ARG, SEGTXT},
{ "C_zne", B_ARG, SEGTXT},
{ "C_zre", EXTERNAL, SEGTXT},
{ "C_zre_dlb", EXTERNAL_DLB, SEGTXT},
{ "C_zre_dnam", EXTERNAL_DNAM, SEGTXT},
{ "C_zrf", W_ARG, SEGTXT},
{ "C_zrf_narg", NO_ARGS, SEGTXT},
{ "C_zrl", L_ARG, SEGTXT},
{ "jump", _STRING, SEGTXT},
{ "locals", N_ARG, SEGTXT},
{ "prolog", NO_ARGS, SEGTXT}
};
set_C_instr_info( instr)
char *instr;
{
int low, high, mid, rel;
low = 0;
high = N_INSTR - 1;
while ( TRUE) {
mid = ( low + high) / 2;
rel = strcmp( instr, EM_instr[mid].name);
if ( rel == 0 )
break;
else if ( high == low) {
out( "ERROR : can't find >>%s<< !!\n", instr);
abort();
}
else if ( rel < 0)
high = mid;
else
/* pas op, mid is naar beneden afgerond !! */
low = ( mid == low ? low + 1: mid);
}
C_instr_info = &C_info[ EM_instr[mid].class];
segment = EM_instr[mid].segment;
}
int arg_type( arg)
char *arg;
/* return argument-type from $i */
{
int i;
i = *(arg+1) - '0' - 1;
return( C_instr_info->arg_type[i]);
}

View file

@ -0,0 +1,25 @@
EM=/proj/em/Work
GFILES = pars.g
OFILES = pars.o Lpars.o scan.o mylex.o default.o C_instr2.o assem.o eval.o\
action.o
IFILES = -I$(EM)/h -I$(EM)/modules/h
CC = cc
.c.o :
$(CC) $(IFILES) -c $<
rm -f ../obj_EM_pars/$*.o
rm -f ../as_EM_pars/$*.o
ln $*.o ../obj_EM_pars/$*.o
ln $*.o ../as_EM_pars/$*.o
common : dummy $(OFILES)
touch ../*/em_parser
dummy : $(GFILES)
LLgen $(LLOPT) $(GFILES)
touch dummy
Lpars.o : Lpars.h decl.h
pars.o : Lpars.h decl.h
Lpars.o : Lpars.h decl.h

View file

@ -0,0 +1,45 @@
#include "decl.h"
#include <alloc.h>
int quantum = 0, nr_instr, block_saved, first_action, last_action;
extern char *malloc(), *realloc(), *Salloc();
char **as_instructions;
init_as_block()
{
nr_instr = 0;
block_saved = TRUE;
if ( quantum == 0) {
quantum = 16;
as_instructions = (char **)malloc( quantum*sizeof( char *));
}
}
save_as( instr)
char *instr;
{
if ( nr_instr == quantum) {
quantum *= 2;
as_instructions = (char **) realloc( as_instructions,
quantum*sizeof( char *));
}
as_instructions[nr_instr++] = Salloc( instr, strlen( instr) + 1);
}
do_block_assemble()
{
int i;
if ( block_saved) {
block_assemble( as_instructions, nr_instr,
first_action, last_action);
for ( i=0; i<nr_instr; i++)
free( as_instructions[i]);
block_saved = FALSE;
}
}

View file

@ -0,0 +1,3 @@
#define STRING 0
#define ARITH 1
#define INT 2

View file

@ -0,0 +1,66 @@
#define SEGBSS 0
#define SEGHOL 1
#define SEGCON 2
#define SEGROM 3
#define SEGTXT 4
#define NOSEG 5
#define UNKNOWN (-1)
#define TRUE 1
#define FALSE 0
#define STRING 0
#define LABEL 1
#define ARITH 2
#define INT 3
#define MAX_ARGS 4
#define NO_ARGS 0
#define C_ARG 1
#define D_ARG 2
#define F_ARG 3
#define L_ARG 4
#define N_ARG 5
#define O_ARG 6
#define R_ARG 7
#define S_ARG 8
#define Z_ARG 9
#define W_ARG 10
#define EXTERNAL 11
#define EXTERNAL_DNAM 12
#define EXTERNAL_DLB 13
#define P_ARG 14
#define B_ARG 15
#define STRING_ARITH 16
#define BSS_EXT_DNAM 17
#define BSS_EXT_DLB 18
#define BSS_STR_AR 19
#define BSS_W_ARG 20
#define BSS_PNAM 21
#define BSS_ILB 22
#define ID_N_ARG 23
#define ARITH_ARITH 24
#define EXT_DNAM 25
#define EXT_DLB 26
#define ONE_INT 27
#define _STRING 28
typedef struct {
char *header;
int arg_type[MAX_ARGS]; /* Zoals ze eruit gaan!! */
char *arg_id[MAX_ARGS], *arg_conv[MAX_ARGS];
} t_C_info;
typedef struct {
char *lexeme;
int C_instr_type;
} t_token;
extern t_C_info C_info[], *C_instr_info;
extern int storage_default[4][3], segment;

View file

@ -0,0 +1,224 @@
#include "decl.h"
#include <system.h>
File *save_file;
extern File *outfile;
extern char yytext[];
int def_start, def_end, save_lineno;
extern int yylineno;
init_defaults( instr)
char *instr;
{
save_file = outfile;
save_lineno = yylineno;
sys_open( ".default", OP_WRITE, &outfile);
set_def_params( instr);
}
#define _ICON 0
#define _UCON 4
#define _FCON 8
#define C_DEE 12
#define C_FIL 15
#define C_GTO 18
#define C_INE 21
#define C_LAE 24
#define C_LDE 27
#define C_LOE 30
#define C_SDE 33
#define C_STE 36
#define C_ZRE 39
struct { char *name; int segment, generated}
def_info[] = {
/* _ICON */ { "C_con_icon", SEGCON, 0},
{ "C_rom_icon", SEGROM, 0},
{ "C_hol_icon", NOSEG, 0},
{ "C_bss_icon", NOSEG, 0},
/* _UCON */ { "C_con_ucon", SEGCON, 0},
{ "C_rom_ucon", SEGROM, 0},
{ "C_hol_ucon", NOSEG, 0},
{ "C_bss_ucon", NOSEG, 0},
/* _FCON */ { "C_con_fcon", SEGCON, 0},
{ "C_rom_fcon", SEGROM, 0},
{ "C_hol_fcon", NOSEG, 0},
{ "C_bss_fcon", NOSEG, 0},
/* C_DEE */ { "C_dee", SEGTXT, 0},
{ "C_dee_dnam", SEGTXT, 0},
{ "C_dee_dlb", SEGTXT, 0},
/* C_FIL */ { "C_fil", SEGTXT, 0},
{ "C_fil_dnam", SEGTXT, 0},
{ "C_fil_dlb", SEGTXT, 0},
/* C_GTO */ { "C_gto", SEGTXT, 0},
{ "C_gto_dnam", SEGTXT, 0},
{ "C_gto_dlb", SEGTXT, 0},
/* C_INE */ { "C_ine", SEGTXT, 0},
{ "C_ine_dnam", SEGTXT, 0},
{ "C_ine_dlb", SEGTXT, 0},
/* C_LAE */ { "C_lae", SEGTXT, 0},
{ "C_lae_dnam", SEGTXT, 0},
{ "C_lae_dlb", SEGTXT, 0},
/* C_LDE */ { "C_lde", SEGTXT, 0},
{ "C_lde_dnam", SEGTXT, 0},
{ "C_lde_dlb", SEGTXT, 0},
/* C_LOE */ { "C_loe", SEGTXT, 0},
{ "C_loe_dnam", SEGTXT, 0},
{ "C_loe_dlb", SEGTXT, 0},
/* C_SDE */ { "C_sde", SEGTXT, 0},
{ "C_sde_dnam", SEGTXT, 0},
{ "C_sde_dlb", SEGTXT, 0},
/* C_STE */ { "C_ste", SEGTXT, 0},
{ "C_ste_dnam", SEGTXT, 0},
{ "C_ste_dlb", SEGTXT, 0},
/* C_ZRE */ { "C_zre", SEGTXT, 0},
{ "C_zre_dnam", SEGTXT, 0},
{ "C_zre_dlb", SEGTXT, 0}
};
int bss_or_hol_instr( index)
int index;
{
return ( index == _ICON + 2 || index == _ICON + 3 ||
index == _UCON + 2 || index == _UCON + 3 ||
index == _FCON + 2 || index == _FCON + 3);
}
set_def_params( instr)
char *instr;
/* geef def_start en def_end een waarde */
{
int low, high, mid, rel;
if ( *instr == '.') { /* ..icon | ..ucon | ..fcon */
switch ( *(instr+2)) {
case 'i' : def_start = _ICON;
break;
case 'u' : def_start = _UCON;
break;
case 'f' : def_start = _FCON;
break;
}
def_end = def_start + 3;
}
else {
low = C_DEE;
high = C_ZRE;
while ( TRUE) {
mid = ( low + high) / 6 * 3;
rel = strncmp( instr, def_info[mid].name, 5);
if ( rel == 0 )
break;
else if ( high == low)
abort();
else if ( rel < 0)
high = mid;
else
/* pas op, mid is naar beneden afgerond !! */
low = ( mid == low ? low + 3: mid);
}
def_start = mid;
def_end = def_start + 2;
}
}
#include <stdio.h>
#include "Lpars.h"
handle_defaults()
/* Zorgen dat de lexical-analyzer nu van de .default-file leest. */
{
FILE *old, *tmp, *switch_input();
int i, old_yylineno;
extern int CD_pos;
sys_close( outfile);
outfile = save_file;
tmp = fopen( ".default", "r");
old = switch_input( tmp);
old_yylineno = yylineno;
for ( i = def_start; i <= def_end; i++) {
yylineno = save_lineno;
if ( !def_info[i].generated) {
set_C_instr_info( def_info[i].name);
segment = def_info[i].segment;
set_outfile( def_info[i].name);
header( def_info[i].name);
CD_pos = TRUE; /* zet mylex() in juiste positie! */
if ( bss_or_hol_instr( i)) {
extnd_header();
/* Zorg voor de juiste $args */
set_C_instr_info( def_info[i-2].name);
def_row();
out( "}\n\n");
}
else
def_row();
rewind( tmp);
}
}
fclose( tmp);
switch_input( old);
yylineno = old_yylineno;
}
def_admn( instr)
char *instr;
{
/* Noteer dat deze instructie gegenereerd is */
int low, high, mid, rel;
low = _ICON;
high = C_ZRE + 2;
while ( TRUE) {
mid = ( low + high) / 2;
rel = strcmp( instr, def_info[mid].name);
if ( rel == 0 )
break;
else if ( high == low)
return;
else if ( rel < 0)
high = mid;
else
/* pas op, mid is naar beneden afgerond !! */
low = ( mid == low ? low + 1: mid);
}
def_info[mid].generated = 1;
}
extnd_header()
{
out( "if ( %s == 0 ) {\n", C_instr_info->arg_id[3]);
set_segment( SEGBSS);
out( "bss( %s);\n", C_instr_info->arg_id[0]);
out( "}\nelse {\n");
set_segment( SEGCON);
out( "for ( %s = 0; %s < %s/EM_WSIZE; %s++)\n",
C_instr_info->arg_id[3], C_instr_info->arg_id[3],
C_instr_info->arg_id[0], C_instr_info->arg_id[3]);
}

View file

@ -0,0 +1,10 @@
#define SEGBSS 0
#define SEGHOL 1
#define SEGCON 2
#define SEGROM 3
#define SEGTXT 4
#define STRING 0
#define LABEL 1
#define ARITH 2
#define INT 3

View file

@ -0,0 +1,20 @@
#include <ctype.h>
#include "decl.h"
eval( str)
char *str;
{
register char c;
int i;
for ( c = *str++; c != '\0'; c= *str++)
if ( c == '$' && isdigit( *str)) {
i = *str++ - '0' - 1;
out( "%s", C_instr_info->arg_conv[i]);
}
else
out( "%c", c);
}

View file

@ -0,0 +1,272 @@
#include "Lpars.h"
#include <stdio.h>
#include <ctype.h>
#define YYTEXT 256
#define FALSE 0
#define TRUE 1
char yytext[YYTEXT], *next;
int CD_pos = FALSE; /* condition or default allowed ? */
int mylex()
{
char c, scanc(), skip_space();
static int special = FALSE; /* rule with conditions + default ? */
next = yytext;
c = *next++ = skip_space();
switch ( c) {
case EOF : next = yytext;
return( 0);
case '"' : read_string();
return( ASSEM_INSTR);
case '.' : c = scanc();
backc( c);
if ( c != '.') { /* geen ..icon o.i.d. */
if ( special)
CD_pos = TRUE;
return( '.');
}
break;
case ';' : return( ';');
case '=' : if ( arrow()) {
CD_pos = FALSE;
return( ARROW);
}
break;
case ':' : if ( equiv()) {
CD_pos = FALSE;
return( EQUIV);
}
break;
case 'd' : if ( CD_pos && _default()) {
CD_pos = FALSE;
special = FALSE;
return( DEFAULT);
}
break;
}
if ( CD_pos) {
read_condition();
CD_pos = FALSE;
special = TRUE;
return( CONDITION);
}
if ( isalpha( c)) {
read_ident();
c = skip_space();
if ( c == '(') {
*next++ = c;
read_call();
return( CALL);
}
else {
backc( c);
if ( is_DEF_C_INSTR( yytext)) {
CD_pos = TRUE;
return( DEF_C_INSTR);
}
if ( is_C_INSTR( yytext)) {
CD_pos = TRUE;
return( C_INSTR);
}
return( ERROR);
}
}
if ( c == '.') {
c = scanc();
if ( c == '.') {
*next++ = '.';
read_ident();
if ( is_DEF_C_INSTR( yytext)) {
CD_pos = TRUE;
return( DEF_C_INSTR);
}
return( ERROR);
}
else {
backc( c);
return( '.');
}
}
return( c);
}
int isletter( c)
char c;
{
return( isalpha( c) || isdigit( c) || c == '_');
}
static char skip_space()
{
char c;
while ( isspace( c = scanc()))
;
return( c);
}
/* first character has been read */
read_string()
/* de "'s eraf strippen!! */
{
next--;
while( ( *next = scanc()) != '"' || *(next-1) == '\\')
next++;
}
int arrow()
{
if ( ( *next++ = scanc()) == '=')
if ( ( *next++ = scanc()) == '>')
return( TRUE);
else
backc( *--next);
else
backc( *--next);
return( FALSE);
}
int equiv()
{
if ( ( *next++ = scanc()) == ':')
if ( ( *next++ = scanc()) == '=')
return( TRUE);
else
backc( *--next);
else
backc( *--next);
return( FALSE);
}
int _default()
{
char c;
if ( ( *next++ = scanc()) == 'e')
if ( ( *next++ = scanc()) == 'f')
if ( ( *next++ = scanc()) == 'a')
if ( ( *next++ = scanc()) == 'u')
if ( ( *next++ = scanc()) == 'l')
if ( ( *next++ = scanc()) == 't')
if ( !isletter( c = scanc())) {
backc( c);
return( TRUE);
}
else
backc( c);
else
backc( *--next);
else
backc( *--next);
else
backc( *--next);
else
backc( *--next);
else
backc( *--next);
else
backc( *--next);
return( FALSE);
}
read_ident()
{
char c;
while ( isletter( c = scanc()))
*next++ = c;
backc( c);
}
read_call()
{
int n = 1;
while ( TRUE)
switch( *next++ = scanc()) {
case EOF : return;
case '(' : n++;
break;
case ')' : n--;
if ( n == 0)
return;
break;
}
}
read_condition()
{
while ( TRUE) {
switch ( *next++ = scanc()) {
case EOF : return;
case '=' : if ( arrow()) {
backc( '>');
backc( '=');
backc( '=');
next -= 3;
return;
}
break;
case ':' : if ( equiv()) {
backc( '=');
backc( ':');
backc( ':');
next -= 3;
return;
}
break;
}
}
}
is_C_INSTR( str)
char *str;
{
if ( *str == 'C' && *(str+1) == '_')
return( TRUE);
else if ( strncmp( "locals", str, 6) == 0)
return( TRUE);
else if ( strncmp( "jump", str, 4) == 0)
return( TRUE);
else if ( strncmp( "prolog", str, 6) == 0)
return( TRUE);
else
return( FALSE);
}
is_DEF_C_INSTR( str)
char *str;
/* Er is gelezen [..][letter]* */
{
if ( *str == '.' && *(str+1) == '.')
return( next > yytext+1);
if ( ( *next++ = scanc()) == '.')
if ( ( *next++ = scanc()) == '.')
return( next > yytext+1);
else
backc( *--next);
else
backc( *--next);
return( FALSE);
}

View file

@ -0,0 +1,76 @@
#include <stdio.h>
FILE *infile = stdin;
#define BUF_SIZE 16
char buf[BUF_SIZE], /* Bufer to save backc()-characters */
*bufptr = buf; /* Pointer to space for backc()-character */
int yylineno = 1;
char nextc()
{
if ( bufptr > buf)
return( *--bufptr);
else
return( getc( infile));
}
backc( c)
char c;
{
if ( bufptr > buf + BUF_SIZE)
error( "backc(), no space in buffer left!");
else {
if ( c == '\n')
yylineno--;
*bufptr++ = c;
}
}
char scanc()
/* Get next character, but delete al C-comments */
{
char c;
c = nextc();
while ( c == '/') {
c = nextc();
if ( c == '*') { /* start of comment */
while ( nextc() != '*' || nextc() != '/')
;
c = nextc();
}
else {
backc( c);
return( '/');
}
}
if ( c == '\n')
yylineno++;
return( c);
}
FILE *switch_input( new)
FILE *new;
/* Return the current FILE, if buf[] can't be cleaned NULL will be returned */
{
char *ptr; FILE *old;
/* Clean buf[] */
for ( ptr = buf; ptr < bufptr; ptr++)
if ( ungetc( *ptr, infile) == EOF && *ptr != EOF)
return( NULL);
bufptr = buf;
old = infile;
infile = new;
return( old);
}

View file

@ -0,0 +1,12 @@
EM=/proj/em/Work
OFILES = dist.o
IFILES = -I$(EM)/h -I$(EM)/modules/h
CC = cc
.c.o :
$(CC) $(IFILES) -c $<
em_parser : $(OFILES)
touch em_parser

View file

@ -0,0 +1,3 @@
#define STRING 0
#define ARITH 1
#define INT 2

View file

@ -0,0 +1,102 @@
#include <stdio.h>
#include "em.h"
arith cur_pos = 0;
dist( lab)
char *lab;
{
out( "$%s$ - %ld", lab, cur_pos);
}
#define MAX_LABEL 10
#define TRUE 1
#define FALSE 0
struct t_label {
char *lab;
arith position;
};
struct t_label label_list[ MAX_LABEL];
int n_labs = 0;
handle_label( lab)
char *lab;
{
char *Salloc();
if ( n_labs >= MAX_LABEL)
error( "Too many labels!!\n");
else {
label_list[ n_labs].lab = Salloc( lab, strlen( lab) + 1);
label_list[ n_labs++].position = cur_pos;
}
process_label( lab);
}
relocate( f)
FILE *f;
{
char buf[256];
int i;
fscanf( f, " %[^$]$", buf);
for ( i = 0; i < n_labs; i++)
if ( strcmp( buf, label_list[i].lab) == 0) {
out( "%ld", label_list[i].position);
return;
}
error( "relocate() : can't find local label %s\n", buf);
}
#include <system.h>
File *oldout;
extern File *outfile;
back_patch()
{
FILE *save;
char c, c1;
sys_close( outfile);
outfile = oldout;
save = fopen( ".tmp", "r");
while ( ( c = getc( save)) != EOF)
if ( c == '$')
relocate( save);
else if ( c == '/') { /* skip comments */
c = getc( save);
out( "/%c", c);
if ( c == '*') {
c = getc( save);
do {
out( "%c", c);
c1 = c;
c = getc( save);
}
while ( !( c1 == '*' && c == '/'));
out( "%c", c);
}
}
else
out( "%c", c);
fclose( save);
}
save_output()
{
extern arith cur_pos;
extern int n_labs;
oldout = outfile;
if ( ! sys_open( ".tmp", OP_WRITE, &outfile))
fprint( STDERR, "can't open .tmp\n");
cur_pos = 0;
n_labs = 0; /* geheugen-ruimte teruggeven? */
}

View file

@ -0,0 +1,10 @@
#define SEGBSS 0
#define SEGHOL 1
#define SEGCON 2
#define SEGROM 3
#define SEGTXT 4
#define STRING 0
#define LABEL 1
#define ARITH 2
#define INT 3

View file

@ -0,0 +1,28 @@
EM=/proj/em/Work
GFILES = pars.g
OFILES = pars.o Lpars.o lex.yy.o help.o conversion.o
IFILES = -I$(EM)/h -I$(EM)/modules/h
LIBS = $(EM)/modules/lib/liballoc.a\
$(EM)/modules/lib/libprint.a\
$(EM)/modules/lib/libstring.a\
$(EM)/modules/lib/libsystem.a
CC = cc
.c.o :
$(CC) $(IFILES) -c $<
as_parser : dummy $(OFILES)
$(CC) -o as_parser $(OFILES) $(LIBS) -ll
lex.yy.c : table.l
lex table.l
dummy : $(GFILES)
LLgen $(LLOPT) $(GFILES)
touch dummy
Lpars.o : Lpars.h decl.h
pars.o : Lpars.h decl.h
Lpars.o : Lpars.h decl.h
lex.yy.o : Lpars.h decl.h

View file

@ -0,0 +1,5 @@
#include <em.h>
#include <system.h>
extern File *outfile;
extern arith cur_pos;

View file

@ -0,0 +1,7 @@
#define Bool int
#define TRUE 1
#define FALSE 0
#define MAX_OPERANDS 4
#define MAX_MNEMONICS 100

View file

@ -0,0 +1,97 @@
pr_text_with_conversions( str)
char *str;
{
char *ptr, *next_conversion(), *pr_conversion();
while ( ptr = next_conversion( str)) {
/* ptr points to '%'-sign */
*ptr = '\0';
out( "fprint( outfile, \"%s\");", str);
*ptr = '%';
str = pr_conversion( ptr);
}
out( "fprint( outfile, \"%s\");", str);
}
char *next_conversion( str)
char *str;
{
char *match();
while ( *str && *str != '%') {
switch ( *str++) {
case '\'' : str = match( '\'', str) + 1;
break;
case '"' : str = match( '"', str) + 1;
break;
}
}
return( *str == '%' ? str : (char *)0);
}
char *match( c, str)
char c, *str;
{
while ( *str && ( *str != c || *(str-1) == '\\'))
str++;
return( *str ? str : str-1);
}
char *match_bracket( str)
char *str;
/* find ')', but look at nesting '('-')' pairs, return position of ')'.
*/
{
int depth;
char *match();
depth = 1;
while ( *str && depth != 0) {
switch ( *str++) {
case '\'' : str = match( '\'', str+1) + 1;
break;
case '"' : str = match( '"', str+1) + 1;
break;
case '(' : depth++;
break;
case ')' : depth--;
break;
}
}
return( str-1);
}
char *find( c, str)
char c, *str;
{
while ( *str && *str != c)
str++;
return( str);
}
char *pr_conversion( str)
char *str;
/* str points to '%'-sign, returns pointer to first character AFTER the
* conversion
*/
{
char *start, *ptr, *match_bracket(), *find();
start = find( '(', str+1);
*start++ = '\0';
ptr = match_bracket( start);
*ptr = '\0';
if ( *(str+1) == '$')
out( "eval( %s);", start);
else if ( strncmp( str+1, "dist", 4) == 0)
out( "dist( %s);", start);
else
out( "fprint( outfile, \"%%%s\", %s);", str+1, start);
return( ptr+1);
}

View file

@ -0,0 +1,3 @@
#include <alloc.h>
#include <system.h>
#include "const.h"

View file

@ -0,0 +1,2 @@
eval : eval.c states.h
cc -o eval eval.c

View file

@ -0,0 +1,309 @@
#include <stdio.h>
#include <ctype.h>
#include "states.h"
#define is_letter( c) ( isalpha( c) || isdigit( c) || c == '_')
#define save_or_put(c) if(previous_state==TEXT)putchar(c);else *bufptr++=c
main()
{
char buffer[256], *bufptr = buffer;
int c, state = TEXT, previous_state = TEXT, depth = 0;
pr_header();
while ( ( c = getchar()) != EOF)
switch ( state) {
case TEXT :
switch ( c) {
case '/' : state = SLASH;
previous_state = TEXT;
save_or_put( c);
break;
case '"' : state = STRING;
previous_state = TEXT;
save_or_put( c);
break;
case '\'': state = CHAR_CONST;
previous_state = TEXT;
save_or_put( c);
break;
case '@' : state = AT;
break;
default : putchar( c);
break;
}
break;
case SLASH :
state = ( c == '*' ? COMMENT : TEXT);
save_or_put( c);
break;
case COMMENT :
if ( c == '*')
state = STAR;
save_or_put( c);
break;
case STAR :
if ( c == '/')
state = previous_state;
else if ( c != '*')
state = COMMENT;
save_or_put( c);
break;
case STRING :
if ( c == '"')
state = previous_state;
else if ( c == '\\')
state = BACKSLASH_in_STRING;
save_or_put( c);
break;
case BACKSLASH_in_STRING :
state = STRING;
save_or_put( c);
break;
case CHAR_CONST :
if ( c == '\'')
state = previous_state;
else if ( c == '\\')
state = BACKSLASH_in_CHAR_CONST;
save_or_put( c);
break;
case BACKSLASH_in_CHAR_CONST :
state = CHAR_CONST;
save_or_put( c);
break;
case AT : /* @if '(' <CONDITION> ')'
* @elsif '(' <CONDITION> ')'
* @else
* @fi
* @<IDENTIFIER> '(' <PARAM_LIST> ')'
*/
if ( is_letter( c))
*bufptr++ = c;
else {
ungetc( c, stdin);
state = WHITE_SPACE;
}
break;
case WHITE_SPACE :
if ( isspace( c))
*bufptr++ = c;
else if ( c == '(') {
*bufptr++ = c;
depth = 1;
state = CAL_or_QUEST;
}
else {
*bufptr = '\0';
pr_ELSE_or_FI( buffer);
bufptr = buffer;
ungetc( c, stdin);
state = TEXT;
}
break;
case CAL_or_QUEST : /* match ')' */
*bufptr++ =c;
switch ( c) {
case '(' : depth++;
break;
case ')' : depth--;
if ( depth == 0) {
*bufptr = '\0';
pr_CALL_or_QUEST( buffer);
bufptr = buffer;
state = TEXT;
}
break;
case '/' : state = SLASH;
previous_state = CAL_or_QUEST;
break;
case '"' : state = STRING;
previous_state = CAL_or_QUEST;
break;
case '\'': state = CHAR_CONST;
previous_state = CAL_or_QUEST;
break;
}
break;
default :
fprintf( stderr, "Unknown state : %d\n", state);
break;
}
exit( 0);
}
pr_header()
{
printf( "#include \"as_parser.h\"\n");
printf( "#line 1 \"as.c\"\n");
}
pr_ELSE_or_FI( str)
char *str;
{
if ( strncmp( str, "else", 4) == 0)
printf( "fprint( outfile, \"}\else {\");%s", str+4);
else if ( strncmp( str, "fi", 2) == 0)
printf( "fprint( outfile, \"}\");%s", str+2);
else
fprintf( stderr, "%s unexpected!!\n", str);
}
pr_CALL_or_QUEST( str)
char *str;
{
if ( strncmp( str, "if", 2) == 0 && !(is_letter( *(str+2))))
pr_if( str);
else if ( strncmp( str, "elsif", 5) == 0 && !(is_letter( *(str+5))))
pr_elsif( str);
else
pr_call( str);
}
pr_call( call)
char *call;
{
if ( strncmp( "text", call, 4) == 0 && isdigit( *(call+4)))
printf( "cur_pos += %d;", *(call+4) - '0');
else if ( strncmp( "reloc", call, 5) == 0 && isdigit( *(call+5)))
printf( "cur_pos += %d;", *(call+5) - '0');
pr_text_with_conversions( call);
printf( "fprint( outfile, \";\")");
}
pr_elsif( quest)
char *quest;
{
printf( "fprint( outfile, \"}\else if\");");
pr_text_with_conversions( quest+5);
printf( "fprint( outfile, \"{\");");
}
pr_if( quest)
char *quest;
{
pr_text_with_conversions( quest);
printf( "fprint( outfile, \"{\");");
}
pr_text_with_conversions( str)
char *str;
{
char *ptr, *next_conversion(), *pr_conversion();
while ( ptr = next_conversion( str)) {
/* ptr points to '%'-sign */
*ptr = '\0';
printf( "fprint( outfile, \"%s\");", str);
*ptr = '%';
str = pr_conversion( ptr);
}
printf( "fprint( outfile, \"%s\");", str);
}
char *next_conversion( str)
char *str;
{
char *match();
while ( *str && *str != '%') {
switch ( *str++) {
case '\'' : str = match( '\'', str) + 1;
break;
case '"' : str = match( '"', str) + 1;
break;
}
}
return( *str == '%' ? str : (char *)0);
}
char *match( c, str)
char c, *str;
{
while ( *str && ( *str != c || *(str-1) == '\\'))
str++;
return( *str ? str : str-1);
}
char *match_bracket( str)
char *str;
/* find ')', but look at nesting '('-')' pairs, return position of ')'.
*/
{
int depth;
char *match();
depth = 1;
while ( *str && depth != 0) {
switch ( *str++) {
case '\'' : str = match( '\'', str+1) + 1;
break;
case '"' : str = match( '"', str+1) + 1;
break;
case '(' : depth++;
break;
case ')' : depth--;
break;
}
}
return( str-1);
}
char *find( c, str)
char c, *str;
{
while ( *str && *str != c)
str++;
return( str);
}
char *pr_conversion( str)
char *str;
/* str points to '%'-sign, returns pointer to first character AFTER the
* conversion
*/
{
char *start, *ptr, *match_bracket(), *find();
start = find( '(', str+1);
*start++ = '\0';
ptr = match_bracket( start);
*ptr = '\0';
if ( *(str+1) == '$')
printf( "eval( %s);", start);
else if ( strncmp( str+1, "dist", 4) == 0)
printf( "dist( %s);", start);
else
printf( "fprint( outfile, \"%%%s\", %s);", str+1, start);
return( ptr+1);
}

View file

@ -0,0 +1,11 @@
#define TEXT 1
#define SLASH 2
#define COMMENT 3
#define STAR 4
#define STRING 5
#define BACKSLASH_in_STRING 6
#define CHAR_CONST 7
#define BACKSLASH_in_CHAR_CONST 8
#define AT 9
#define CAL_or_QUEST 10
#define WHITE_SPACE 11

287
util/ceg/as_parser/help.c Normal file
View file

@ -0,0 +1,287 @@
#include "decl.h"
extern char *strindex();
static struct Op_info { char *name, *type}
op_info[ MAX_OPERANDS] = { { 0, 0}};
static int n_ops = 0;
static char *assem_instr = 0;
static Bool restriction = FALSE;
File *outfile;
save_instr( instr, len)
char *instr;
int len;
{
assem_instr = Salloc( instr, len + 1);
}
save_name( name, len)
char *name;
int len;
{
op_info[ n_ops].name = Salloc( name, len + 1);
}
save_type( type, len)
char *type;
int len;
{
op_info[ n_ops].type = Salloc( type, len + 1);
restriction = TRUE;
}
pr_header()
{
out( "%s_instr", assem_instr);
param_list();
out( "{\n");
save_mnem( assem_instr);
}
param_list()
{
int i;
out( "(");
if ( n_ops > 0) {
out( " %s", op_info[0].name);
for ( i = 1; i < n_ops; i++)
out( ", %s", op_info[i].name);
}
out( ")\n");
if ( n_ops > 0) {
out( "struct t_operand *%s", op_info[0].name);
for ( i = 1; i < n_ops; i++)
out( ", *%s", op_info[i].name);
out( ";\n");
}
}
pr_restriction()
{
int i;
Bool more = FALSE;
if ( !restriction)
return;
out( "if ( ");
for ( i = 0; i < n_ops; i++)
if ( op_info[i].type != 0) {
if ( more)
out( " &&");
out( " %s( %s)", op_info[i].type, op_info[i].name);
more = TRUE;
}
out( ") ");
}
pr_warning()
{
if ( restriction)
out( "else\nerror( \"No match for %s\");\n", assem_instr);
restriction = FALSE;
}
clear_restriction()
{
restriction = FALSE;
}
char *skip_string( str)
char *str;
{
for ( str++; *str != '"' || *(str-1) == '\\'; str++);
return( str + 1);
}
pr_subroutine( str)
char *str;
{
out( "%s;\n", str);
}
#include <ctype.h>
pr_call( str)
char *str;
{
if ( strncmp( "text", str, 4) == 0 && isdigit( *(str+4)))
out( "cur_pos += %d;\n", *(str+4) - '0');
else if ( strncmp( "reloc", str, 5) == 0 && isdigit( *(str+5)))
out( "cur_pos += %d;\n", *(str+5) - '0');
pr_text_with_conversions( str);
out( "fprint( outfile, \";\");");
}
pr_end()
{
out( "fprint( outfile, \"}\\n\");");
}
pr_els()
{
out( "fprint( outfile, \"else\\n\");");
}
pr_else()
{
out( "fprint( outfile, \"else {\\n\");");
}
pr_question( quest)
char *quest;
{
out( "fprint( outfile, \"if\");");
pr_text_with_conversions( quest);
out( "fprint( outfile, \"{\\n\");");
}
init_table()
{
outfile = STDOUT;
out( "#include \"as.h\"\n");
out( "#include \"as_parser.h\"\n");
}
clean()
{
int i;
if ( assem_instr != 0) {
free( assem_instr);
assem_instr = 0;
}
for ( i = 0; i < n_ops; i++) {
free( op_info[i].name);
op_info[i].name = 0;
if ( op_info[i].type != 0) {
free( op_info[i].type);
op_info[i].type = 0;
}
}
n_ops = 0;
}
operand_clean()
/* Naam van de assembler-instr bewaren! */
{
int i;
for ( i = 0; i < n_ops; i++) {
free( op_info[i].name);
op_info[i].name = 0;
if ( op_info[i].type != 0) {
free( op_info[i].type);
op_info[i].type = 0;
}
}
n_ops = 0;
}
out( fmt, argv)
char *fmt;
int argv;
{
doprnt( outfile, fmt, &argv);
}
error( fmt, argv)
char *fmt;
int argv;
{
fprint( STDERR, "!! ERROR : ");
doprnt( STDERR, fmt, &argv);
fprint( STDERR, " !!\n");
}
inc_ops()
{
n_ops++;
}
/**********************************/
char *mnemonic[ MAX_MNEMONICS];
int n_mnems = 0;
save_mnem( mnem)
char *mnem;
{
if ( n_mnems == MAX_MNEMONICS)
error( "too many assembler instructions!! MAX_MNEMONICS = %d",
MAX_MNEMONICS);
else
mnemonic[ n_mnems++] = Salloc( mnem, strlen( mnem) + 1);
}
end_table()
/* array's wegschrijven !! */
{
int i;
quicksort( 0, n_mnems - 1);
out( "char *mnemonic[] = {\n");
for ( i = 0; i < n_mnems - 1; i++)
out( "\t\"%s\",\n", mnemonic[i]);
out( "\t\"%s\"};\n\n", mnemonic[ n_mnems-1]);
out( "int (*instruction[])() = {\n");
for ( i = 0; i < n_mnems - 1; i++)
out( "\t%s_instr,\n", mnemonic[i]);
out( "\t%s_instr};\n\n", mnemonic[ n_mnems-1]);
out( "int n_mnems = %d;\n", n_mnems);
}
quicksort( lower, upper)
int lower, upper;
/* Deze routine sorteert het array 'mnemonic' mbv. het algorithme quick_sort.
* ( zie diktaat "inleiding programmeren" voor een PASCAL versie.)
*/
{
char *key, *tmp;
int index1, index2;
if ( lower >= upper)
return;
key = mnemonic[lower];
index1 = lower;
index2 = upper+1;
while ( index1 < index2) {
do
index1++;
while (index1 <= upper && strcmp( mnemonic[index1], key) < 0 );
do
index2--;
while ( strcmp( mnemonic[index2], key) > 0);
if ( index1 < index2) {
tmp = mnemonic[index2];
mnemonic[index2] = mnemonic[index1];
mnemonic[index1] = tmp;
}
}
mnemonic[lower] = mnemonic[index2];
mnemonic[index2] = key;
quicksort( lower, index2-1);
quicksort( index2+1, upper);
}

157
util/ceg/as_parser/pars.g Normal file
View file

@ -0,0 +1,157 @@
{
#include "decl.h"
extern int yylineno, yyleng, yymorfg;
extern char yytext[];
}
%token IDENTIFIER, CALL, CONDITION, IF, ELSIF, ELSE, FI, ARROW, MORE;
%start table, table;
%lexical lex_analyzer ;
table : { init_table();}
instruction* { end_table();}
;
instruction : { clean();}
first_row
[ { operand_clean();}
extra_row
]* { pr_warning(); out( "}\n\n");}
;
first_row : mnemonic { save_instr( yytext, yyleng);}
[ decl_list]?
ARROW { pr_header(); pr_restriction();}
action_list
;
extra_row : MORE
[ decl_list]?
ARROW { out( "else "); pr_restriction();}
action_list
;
mnemonic : IDENTIFIER
;
decl_list : { clear_restriction();}
declaration
[ ',' declaration] *7
;
declaration : IDENTIFIER { save_name( yytext, yyleng);}
[ ':'
IDENTIFIER { save_type( yytext, yyleng);}
]? { inc_ops();}
;
action_list : { out( "{\n");}
[ action [ ';' action]* ]? '.' { out( "}\n");}
;
action : if_statement
| call
| subroutine
;
subroutine : IDENTIFIER { yymorfg=1;}
CONDITION { pr_subroutine( yytext);}
;
call : '@'
IDENTIFIER { yymorfg=1;}
CONDITION { pr_call( yytext);}
;
if_statement : IF
CONDITION { pr_question( yytext);}
action_list { pr_end();}
[ ELSIF { pr_els();}
CONDITION { pr_question( yytext);}
action_list { pr_end();}
]*
[ ELSE { pr_else();}
action_list { pr_end();}
]?
FI
;
{
static int saved = 0, token;
LLmessage( inserted_token)
int inserted_token;
{
if ( inserted_token == 0) {
fprint( STDERR, "Sytax error in line %d, ", yylineno);
print_token( LLsymb);
fprint( STDERR, " will be deleted!!\n");
}
else if ( inserted_token < 0) {
fprint( STDERR, "Stack overflow in line %d, fatal error!\n",
yylineno);
exit( 1);
}
else {
fprint( STDERR, "Sytax error in line %d, ", yylineno);
print_token( inserted_token);
fprint( STDERR, " will be inserted!!\n");
token = LLsymb;
saved = 1;
}
}
print_token( token)
int token;
{
switch ( token) {
case IDENTIFIER : fprint( STDERR, "IDENTIFIER %s", yytext);
break;
case CALL : fprint( STDERR, "CALL %s", yytext);
break;
case CONDITION: fprint( STDERR, "CONDITION %s", yytext);
break;
case IF : fprint( STDERR, "@if ");
break;
case ELSIF : fprint( STDERR, "@elsif ");
break;
case ELSE : fprint( STDERR, "@else ");
break;
case FI : fprint( STDERR, "@fi ");
break;
case ARROW : fprint( STDERR, "==> ");
break;
case MORE : fprint( STDERR, "... ");
break;
default : fprint( STDERR, "%c ", token);
break;
}
}
int lex_analyzer()
{
int tok;
if ( saved) {
saved = 0;
return( token);
}
else {
tok = yylex();
yytext[yyleng] = '\0'; /* strings moeten op een '\0' eindigen */
return( tok);
}
}
main()
{
table();
return( 0);
}
}

View file

@ -0,0 +1,52 @@
ws ([ \t\n]*)
arrow ("==>")
letter ([a-zA-Z_])
digit ([0-9])
identifier ({letter}({letter}|{digit})*)
%start COM CAL CON
%{
#include "Lpars.h"
#include "decl.h"
int special, n_haakjes;
%}
%%
"/*" BEGIN COM;
<COM>[^\n\*]*"*"+"/" BEGIN 0;
<COM>[^\n^\*]*"*"+ ;
<COM>[^\n\*]*"\n" ;
";" return( ';');
"." return( '.');
"," return( ',');
":" return( ':');
"@" return( '@');
"@if"{ws} return( IF);
"@elsif"{ws} return( ELSIF);
"@else"{ws} return( ELSE);
"@fi"{ws} return( FI);
"..." return( MORE);
{arrow} return( ARROW);
{identifier} return( IDENTIFIER);
"(" { BEGIN CON; n_haakjes = 1; yymore();}
<CON>[^()]*"(" { n_haakjes++; yymore();}
<CON>[^()]*")" { if ( n_haakjes == 1) {
BEGIN 0;
return( CONDITION);
}
else {
n_haakjes--;
yymore();
}
}
{ws} ;

View file

@ -0,0 +1,51 @@
#include <ctype.h>
#include <system.h>
#include "em_decl.h"
#include "as.h"
assemble( str)
char *str;
{
char buf[512] , *b_ptr, *arg_format();
int nr;
b_ptr = buf;
out( "fprint( codefile,\"");
while ( *str) {
switch ( *str) {
case '$' : if ( *(str+1) == '$') {
*b_ptr++ = '$';
str = str + 2;
}
else {
nr = atoi( str+1) - 1;
*b_ptr = '\0';
out( "%s%s\", %s);", buf,
arg_format( nr),
C_instr_info->arg_conv[nr]);
out( "fprint( codefile,\"");
b_ptr = buf;
str = str + 2;
}
break;
default : *b_ptr++ = *str++;
}
}
*b_ptr = '\0';
out( "%s\\n\");\n", buf);
}
char *arg_format( nr)
int nr;
{
switch ( C_instr_info->arg_type[nr]) {
case ARITH : return( ARITH_FMT);
case STRING: return( "%s");
case INT : return( INT_FMT);
}
}

View file

@ -0,0 +1,9 @@
block_assemble( instructions, nr, first, last)
char **instructions;
int nr, first, last;
{
int i;
for ( i=0; i<nr; i++)
assemble( instructions[i]);
}

View file

@ -0,0 +1,210 @@
#include <ctype.h>
#include <system.h>
#include "as.h"
#include "const.h"
extern char *mnemonic[];
extern int (*instruction[])(), n_mnems;
struct t_operand operand[ MAX_OPERANDS];
/* To change MAX_OPERANDS, the last statement in execute_mnem() must
* be changed.
*/
char *skip_space(), *parse_label(), *parse_mnemonic(), *parse_operand(),
*skip_string(), *match_ch(), *Salloc(), *skip_operand();
int label();
assemble( instr)
char *instr;
/* INSTR ::= [ STRING ':']? [ STRING [ OPERAND ( ',' OPERAND)*]? ]?
* OPERAND ::= STRING [ '{' char* '}' |
* '(' char* ')' |
* '[' char* ']' ]?
* Break an assembly instruction down in a LABEL, MNEMONIC and OPERANDS.
*/
{
char *ptr, *copy, *mnem;
int n_ops = 0;
copy = ptr = Salloc( instr, strlen( instr)+1);
ptr = skip_space( ptr);
if ( label( ptr)) {
ptr = parse_label( ptr);
if ( *ptr == '\0') return;
}
ptr = parse_mnemonic( ptr, &mnem);
while ( *ptr != '\0') {
if ( n_ops++ == MAX_OPERANDS)
error( "to many operands\n");
ptr = parse_operand( ptr, n_ops, instr);
}
execute_mnemonic( mnem);
free( copy);
}
int label( ptr)
char *ptr;
{
ptr = skip_string( ptr);
ptr = skip_space( ptr);
return( *ptr == ':');
}
char *parse_label( ptr)
char *ptr;
{
char *lab = ptr;
ptr = skip_string( ptr);
if ( *ptr == ':')
*ptr++ = '\0';
else {
*ptr++ = '\0';
ptr = skip_space( ptr);
ptr++; /* skip ':' */
}
handle_label( lab);
ptr = skip_space( ptr);
return( ptr);
}
char *parse_mnemonic( ptr, mnem)
char *ptr, **mnem;
{
*mnem = ptr;
ptr = skip_string( ptr);
if ( *ptr != '\0') {
*ptr++ = '\0';
ptr = skip_space( ptr);
}
return( ptr);
}
char *parse_operand( ptr, n_ops, instr)
char *ptr, *instr;
int n_ops;
{
char *op = ptr;
ptr = skip_operand( ptr, instr);
if ( *ptr != '\0') {
if ( *ptr == ',')
*ptr++ = '\0';
else {
*ptr++ = '\0';
ptr = skip_space( ptr);
if ( *ptr != '\0') ptr++; /* skip ';' */
}
ptr = skip_space( ptr);
}
process_operand( op, &operand[ n_ops-1]);
return( ptr);
}
char *skip_operand( ptr, instr)
char *ptr, *instr;
{
while ( *ptr != ',' && *ptr != '\0') {
switch ( *ptr) {
case '{' : ptr = match_ch( '}', ptr, instr);
break;
case '(' : ptr = match_ch( ')', ptr, instr);
break;
case '[' : ptr = match_ch( ']', ptr, instr);
break;
}
ptr++;
}
return( ptr);
}
char *match_ch( c, str, instr)
char c, *str, *instr;
{
char *ptr, *strindex();
ptr = strindex( str, c);
if ( ptr == 0) {
error( "syntax error in %s : %c expected\n", instr, c);
return( str);
}
else
return( ptr);
}
char *skip_string( ptr)
char *ptr;
{
while ( isalnum( *ptr) || ( *ptr == '_'))
ptr++;
return( ptr);
}
char *skip_space( ptr)
char *ptr;
{
while ( isspace( *ptr) )
ptr++;
return( ptr);
}
/****************************************************************************/
execute_mnemonic( mnem)
char *mnem;
/* Find the function by "mnem" and execute it.
*/
{
int low, mid, high, rel;
low = 0;
high = n_mnems-1;
while ( TRUE) {
mid = ( low + high) / 2;
rel = strcmp(mnem, mnemonic[ mid]);
if ( rel == 0 )
break;
else if ( high == low)
error( "can't find %s", mnem);
else if ( rel < 0)
high = mid;
else
/* pas op, mid is naar beneden afgerond !! */
low = ( mid == low ? low + 1: mid);
}
process_mnemonic( mnem);
( *( instruction[ mid]))( &operand[0], &operand[1], &operand[2],
&operand[3]);
}
error( fmt, argv)
char *fmt;
int argv;
{
extern int yylineno;
fprint( STDERR, "!! ERROR in line %d : ", yylineno);
doprnt( STDERR, fmt, &argv);
fprint( STDERR, " !!\n");
}

View file

@ -0,0 +1,9 @@
block_assemble( instructions, nr, first, last)
char **instructions;
int nr, first, last;
{
int i;
for ( i=0; i<nr; i++)
assemble( instructions[i]);
}

View file

@ -0,0 +1,7 @@
#define Bool int
#define TRUE 1
#define FALSE 0
#define MAX_OPERANDS 4
#define MAX_MNEMONICS 100