Skip to content

Commit e25a3fa

Browse files
committed
porting
1 parent 4072ff7 commit e25a3fa

File tree

12 files changed

+78
-61
lines changed

12 files changed

+78
-61
lines changed

TODO.md

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ would be nice if we could trace execution
1010

1111
a test suite would be nice
1212

13-
## I/O
14-
15-
Input functions and better output functions
16-
1713
## Floating point
1814

1915
Would be nice if we could do floats as well.

call.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
2325
#include <stddef.h>
2426

2527
#include "call.h"
2628

27-
size_t call_return(call_stack_t *call_stack) {
29+
size_t call_return(call_stk_t *call_stack) {
2830
if (call_stack->sp == 0) {
2931
return 0;
3032
}
3133
call_stack->sp--;
3234
return call_stack->mem[call_stack->sp];
3335
}
3436

35-
void call_link(call_stack_t *call_stack, size_t c) {
37+
void call_link(call_stk_t *call_stack, size_t c) {
3638
if (call_stack->sp + 1 >= CSTKSZ) {
3739
return;
3840
}

call.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SOFTWARE.
2727

2828
#include "types.h"
2929

30-
void call_link(call_stack_t *call_stack, size_t c);
31-
size_t call_return(call_stack_t *call_stack);
30+
void call_link(call_stk_t *call_stack, size_t c);
31+
size_t call_return(call_stk_t *call_stack);
3232

3333
#endif

configure.ac

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ AC_INIT([tclang], [0.0.0], [[email protected]])
2222
AM_INIT_AUTOMAKE([-Wall foreign])
2323
AC_LANG([C])
2424
AC_PROG_CC
25+
AC_CONFIG_HEADERS([config.h:config.in])
2526
AC_CONFIG_FILES([Makefile])
2627
AC_OUTPUT

main.c

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
2325
#include <stdlib.h>
2426

2527
#include "types.h"

opcodes.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
25+
#include <limits.h>
2326
#include <stdio.h>
2427
#include <stdlib.h>
2528
#include <string.h>
@@ -113,9 +116,9 @@ void op_inc(vm_t *vm) {
113116
}
114117

115118
void op_ini(vm_t *vm) {
116-
char line[96], *s;
117-
memset(line, '\0', 96);
118-
if ((s = fgets(line, 96, stdin)) != NULL) {
119+
char line[LINE_MAX], *s;
120+
memset(line, '\0', LINE_MAX);
121+
if ((s = fgets(line, LINE_MAX, stdin)) != NULL) {
119122
pushstack(&vm->stack, atoi(line));
120123
}
121124
}

stack.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,26 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
2324
#include "stack.h"
2425
#include "types.h"
2526

26-
cell_t peekstack(stack_t *stack) {
27+
cell_t peekstack(stk_t *stack) {
2728
if (stack->sp == 0) {
2829
return 0;
2930
}
3031
return stack->mem[stack->sp-1];
3132
}
3233

33-
cell_t popstack(stack_t *stack) {
34+
cell_t popstack(stk_t *stack) {
3435
if (stack->sp == 0) {
3536
return 0;
3637
}
3738
stack->sp--;
3839
return stack->mem[stack->sp];
3940
}
4041

41-
void pushstack(stack_t *stack, cell_t c) {
42+
void pushstack(stk_t *stack, cell_t c) {
4243
if (stack->sp + 1 >= STKSZ) {
4344
return;
4445
}

stack.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ SOFTWARE.
2525

2626
#include "types.h"
2727

28-
cell_t peekstack(stack_t *stack);
29-
cell_t popstack(stack_t *stack);
30-
void pushstack(stack_t *stack, cell_t c);
28+
cell_t peekstack(stk_t *stack);
29+
cell_t popstack(stk_t *stack);
30+
void pushstack(stk_t *stack, cell_t c);
3131

3232
#endif

symtab.c

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
2325
#include <stddef.h>
2426
#include <string.h>
2527

types.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ struct stack {
3535
cell_t mem[STKSZ]; /* array of memory cells used by this stack */
3636
size_t sp; /* stack pointer */
3737
};
38-
typedef struct stack stack_t;
38+
typedef struct stack stk_t;
3939

4040
struct call_stack {
4141
size_t mem[CSTKSZ]; /* array of memory cells used by this stack */
4242
size_t sp; /* stack pointer */
4343
};
44-
typedef struct call_stack call_stack_t;
44+
typedef struct call_stack call_stk_t;
4545

4646
struct symbol {
4747
char label[LBLLN]; /* label name + '\0' */
@@ -63,8 +63,8 @@ typedef struct program program_t;
6363

6464
struct vm {
6565
cell_t memory[MEMSZ]; /* main memory */
66-
stack_t stack; /* working stack */
67-
call_stack_t call_stack; /* call stack */
66+
stk_t stack; /* working stack */
67+
call_stk_t call_stack; /* call stack */
6868
symtab_t symtab; /* symbol table */
6969
program_t program; /* text of program */
7070
size_t pc; /* program counter */

util.c

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
2325
#include <stddef.h>
2426
#include <string.h>
2527

vm.c

+48-40
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
******************************************************************************/
2222

23+
#include "config.h"
24+
25+
#include <limits.h>
2326
#include <stdint.h>
2427
#include <string.h>
2528
#include <stdio.h>
@@ -33,49 +36,50 @@ SOFTWARE.
3336
#include "types.h"
3437
#include "util.h"
3538

39+
#define op(NAME, FUNC) { { NAME }, { 0, 0, 0, 0 }, FUNC }
40+
3641
#define NOPS (34)
3742
static op_t opcodes[NOPS] = {
38-
{ { 'A', 'D', 'D', '\0' }, { 0, 0, 0, 0 }, op_add },
39-
{ { 'A', 'N', 'D', '\0' }, { 0, 0, 0, 0 }, op_and },
40-
{ { 'B', 'L', 'S', '\0' }, { 0, 0, 0, 0 }, op_bls },
41-
{ { 'B', 'R', 'A', '\0' }, { 0, 0, 0, 0 }, op_bra },
42-
{ { 'B', 'R', 'S', '\0' }, { 0, 0, 0, 0 }, op_brs },
43-
{ { 'B', 'E', 'Z', '\0' }, { 0, 0, 0, 0 }, op_bez },
44-
{ { 'B', 'N', 'Z', '\0' }, { 0, 0, 0, 0 }, op_bnz },
45-
{ { 'C', 'E', 'Q', '\0' }, { 0, 0, 0, 0 }, op_ceq },
46-
{ { 'C', 'G', 'E', '\0' }, { 0, 0, 0, 0 }, op_cge },
47-
{ { 'C', 'G', 'T', '\0' }, { 0, 0, 0, 0 }, op_cgt },
48-
{ { 'C', 'L', 'E', '\0' }, { 0, 0, 0, 0 }, op_cle },
49-
{ { 'C', 'L', 'T', '\0' }, { 0, 0, 0, 0 }, op_clt },
50-
{ { 'C', 'N', 'E', '\0' }, { 0, 0, 0, 0 }, op_cne },
51-
{ { 'D', 'I', 'V', '\0' }, { 0, 0, 0, 0 }, op_div },
52-
{ { 'D', 'E', 'C', '\0' }, { 0, 0, 0, 0 }, op_dec },
53-
{ { 'D', 'U', 'P', '\0' }, { 0, 0, 0, 0 }, op_dup },
54-
{ { 'H', 'L', 'T', '\0' }, { 0, 0, 0, 0 }, op_hlt },
55-
{ { 'I', 'C', 'H', '\0' }, { 0, 0, 0, 0 }, op_ich },
56-
{ { 'I', 'N', 'C', '\0' }, { 0, 0, 0, 0 }, op_inc },
57-
{ { 'I', 'N', 'I', '\0' }, { 0, 0, 0, 0 }, op_ini },
58-
{ { 'J', 'A', 'L', '\0' }, { 0, 0, 0, 0 }, op_jal },
59-
{ { 'L', 'D', 'A', '\0' }, { 0, 0, 0, 0 }, op_lda },
60-
{ { 'L', 'D', 'I', '\0' }, { 0, 0, 0, 0 }, op_ldi },
61-
{ { 'M', 'O', 'D', '\0' }, { 0, 0, 0, 0 }, op_mod },
62-
{ { 'M', 'U', 'L', '\0' }, { 0, 0, 0, 0 }, op_mul },
63-
{ { 'N', 'O', 'T', '\0' }, { 0, 0, 0, 0 }, op_not },
64-
{ { 'O', 'A', 'R', '\0' }, { 0, 0, 0, 0 }, op_oar },
65-
{ { 'O', 'C', 'H', '\0' }, { 0, 0, 0, 0 }, op_och },
66-
{ { 'O', 'T', 'I', '\0' }, { 0, 0, 0, 0 }, op_oti },
67-
{ { 'O', 'T', 'S', '\0' }, { 0, 0, 0, 0 }, op_ots },
68-
{ { 'R', 'T', 'N', '\0' }, { 0, 0, 0, 0 }, op_rtn },
69-
{ { 'S', 'T', 'A', '\0' }, { 0, 0, 0, 0 }, op_sta },
70-
{ { 'S', 'U', 'B', '\0' }, { 0, 0, 0, 0 }, op_sub },
71-
{ { 'X', 'O', 'R', '\0' }, { 0, 0, 0, 0 }, op_xor }
43+
op("ADD", op_add),
44+
op("AND", op_and),
45+
op("BEZ", op_bez),
46+
op("BLS", op_bls),
47+
op("BNZ", op_bnz),
48+
op("BRA", op_bra),
49+
op("BRS", op_brs),
50+
op("CEQ", op_ceq),
51+
op("CGE", op_cge),
52+
op("CGT", op_cgt),
53+
op("CLE", op_cle),
54+
op("CLT", op_clt),
55+
op("CNE", op_cne),
56+
op("DEC", op_dec),
57+
op("DIV", op_div),
58+
op("DUP", op_dup),
59+
op("HLT", op_hlt),
60+
op("ICH", op_ich),
61+
op("INC", op_inc),
62+
op("INI", op_ini),
63+
op("JAL", op_jal),
64+
op("LDA", op_lda),
65+
op("LDI", op_ldi),
66+
op("MOD", op_mod),
67+
op("MUL", op_mul),
68+
op("NOT", op_not),
69+
op("OAR", op_oar),
70+
op("OCH", op_och),
71+
op("OTI", op_oti),
72+
op("OTS", op_ots),
73+
op("RTN", op_rtn),
74+
op("STA", op_sta),
75+
op("SUB", op_sub),
76+
op("XOR", op_xor)
7277
};
7378

74-
7579
void load(vm_t *vm, FILE *in) {
7680

77-
char line[96]; /* TODO move to const.h */
78-
int cap = 96;
81+
char line[LINE_MAX];
82+
int cap = LINE_MAX;
7983

8084
memset(vm, '\0', sizeof(vm_t));
8185

@@ -105,19 +109,23 @@ void run(vm_t *vm) {
105109
}
106110

107111
for (vm->pc = begin; vm->pc < vm->program.sp && !vm->done && !feof(stdin) && !ferror(stdin); vm->pc++) {
108-
size_t i;
112+
size_t i, run;
109113
if (vm->program.lines[vm->pc][0] != ' ') {
110114
continue; /* label or comment */
111115
}
112116
/* opcode */
113-
for (i = 0; i < NOPS; i++) {
117+
for (run = i = 0; i < NOPS; i++) {
114118
/* find op code and execute it */
115119
if (memcmp(vm->program.lines[vm->pc] + 8, opcodes[i].code, 3) == 0) {
116120
opcodes[i].fn(vm);
121+
run = 1;
117122
break;
118123
}
119124
}
120-
/* TODO probably want to abort here if no op code found */
125+
if (run == 0) {
126+
fprintf(stderr, "ERROR: BAD OP CODE (LINE %lu)\n", vm->pc);
127+
return;
128+
}
121129
}
122130

123131
}

0 commit comments

Comments
 (0)