From dee30787b4a23d71946157e55f6bf0f0cb86bdfc Mon Sep 17 00:00:00 2001 From: David Schmenk Date: Sun, 7 Dec 2014 20:59:24 -0800 Subject: [PATCH] Add structures to PLASMA --- Platform/Apple/tools/PLASMA/src/lex.c | 20 +++--- Platform/Apple/tools/PLASMA/src/parse.c | 78 ++++++++++++++++++++++-- Platform/Apple/tools/PLASMA/src/plvm02.s | 70 ++++++++++----------- Platform/Apple/tools/PLASMA/src/test.pla | 53 ++++++++++------ Platform/Apple/tools/PLASMA/src/tokens.h | 2 +- 5 files changed, 151 insertions(+), 72 deletions(-) diff --git a/Platform/Apple/tools/PLASMA/src/lex.c b/Platform/Apple/tools/PLASMA/src/lex.c index c6ab6e08..075ec70d 100755 --- a/Platform/Apple/tools/PLASMA/src/lex.c +++ b/Platform/Apple/tools/PLASMA/src/lex.c @@ -22,28 +22,28 @@ t_token keywords[] = { ENDCASE_TOKEN, 'W', 'E', 'N', 'D', FOR_TOKEN, 'F', 'O', 'R', TO_TOKEN, 'T', 'O', - DOWNTO_TOKEN, 'D', 'O', 'W', 'N', 'T', 'O', + DOWNTO_TOKEN, 'D', 'O', 'W', 'N', 'T', 'O', STEP_TOKEN, 'S', 'T', 'E', 'P', NEXT_TOKEN, 'N', 'E', 'X', 'T', REPEAT_TOKEN, 'R', 'E', 'P', 'E', 'A', 'T', - UNTIL_TOKEN, 'U', 'N', 'T', 'I', 'L', - BREAK_TOKEN, 'B', 'R', 'E', 'A', 'K', + UNTIL_TOKEN, 'U', 'N', 'T', 'I', 'L', + BREAK_TOKEN, 'B', 'R', 'E', 'A', 'K', ASM_TOKEN, 'A', 'S', 'M', DEF_TOKEN, 'D', 'E', 'F', - EXPORT_TOKEN, 'E', 'X', 'P', 'O', 'R', 'T', - IMPORT_TOKEN, 'I', 'M', 'P', 'O', 'R', 'T', + EXPORT_TOKEN, 'E', 'X', 'P', 'O', 'R', 'T', + IMPORT_TOKEN, 'I', 'M', 'P', 'O', 'R', 'T', RETURN_TOKEN, 'R', 'E', 'T', 'U', 'R', 'N', END_TOKEN, 'E', 'N', 'D', - EXIT_TOKEN, 'E', 'X', 'I', 'T', DONE_TOKEN, 'D', 'O', 'N', 'E', LOGIC_NOT_TOKEN, 'N', 'O', 'T', LOGIC_AND_TOKEN, 'A', 'N', 'D', - LOGIC_OR_TOKEN, 'O', 'R', + LOGIC_OR_TOKEN, 'O', 'R', BYTE_TOKEN, 'B', 'Y', 'T', 'E', WORD_TOKEN, 'W', 'O', 'R', 'D', CONST_TOKEN, 'C', 'O', 'N', 'S', 'T', + STRUC_TOKEN, 'S', 'T', 'R', 'U', 'C', PREDEF_TOKEN, 'P', 'R', 'E', 'D', 'E', 'F', - SYSFLAGS_TOKEN, 'S', 'Y', 'S', 'F', 'L', 'A', 'G', 'S', + SYSFLAGS_TOKEN, 'S', 'Y', 'S', 'F', 'L', 'A', 'G', 'S', EOL_TOKEN }; @@ -376,7 +376,6 @@ int next_line(void) { statement = ++scanpos; scantoken = EOL_TOKEN; - scan(); } else { @@ -385,8 +384,7 @@ int next_line(void) statement = inputline; scanpos = inputline; scantoken = EOL_TOKEN; - scan(); printf("; %03d: %s\n", lineno, inputline); } - return (1); + return (scan()); } diff --git a/Platform/Apple/tools/PLASMA/src/parse.c b/Platform/Apple/tools/PLASMA/src/parse.c index cb94b89e..e4da1042 100755 --- a/Platform/Apple/tools/PLASMA/src/parse.c +++ b/Platform/Apple/tools/PLASMA/src/parse.c @@ -256,7 +256,7 @@ int parse_value(int rvalue) } else if (scantoken == CLOSE_PAREN_TOKEN) { - // type |= WORD_TYPE; + // type |= WORD_TYPE; emit_value = 1; } else @@ -355,8 +355,8 @@ int parse_value(int rvalue) } else (type & BPTR_TYPE) ? emit_lb() : emit_lw(); + emit_value = 1; } - emit_value = 1; type &= ~(VAR_TYPE | ADDR_TYPE); type |= WORD_TYPE; scantoken = scantoken == PTRB_TOKEN ? DOT_TOKEN : COLON_TOKEN; @@ -387,6 +387,7 @@ int parse_value(int rvalue) else // FUNC_TYPE { emit_globaladdr(value, elem_offset, type); + elem_offset = 0; emit_value = 1; } } @@ -416,9 +417,10 @@ int parse_value(int rvalue) } else if (type & CONST_TYPE) { - emit_const(value); + emit_const(value + elem_offset); } - emit_value = 1; + elem_offset = 0; + emit_value = 1; } while (parse_expr()) { @@ -442,7 +444,7 @@ int parse_value(int rvalue) parse_error("Invalid member offset"); return (0); } - type = elem_type; //(type & ~(ADDR_TYPE | CONST_TYPE)) | elem_type; + type = elem_type; break; case OPEN_PAREN_TOKEN: /* @@ -1050,6 +1052,65 @@ int parse_var(int type) id_add(idstr, idlen, type, size); return (1); } +int parse_struc(void) +{ + long size; + int type, constsize, offset = 0; + char *idstr, strucid[80]; + int idlen = 0, struclen = 0; + + if (scan() == ID_TOKEN) + { + struclen = tokenlen; + for (idlen = 0; idlen < struclen; idlen++) + strucid[idlen] = tokenstr[idlen]; + } + while (next_line() == BYTE_TOKEN || scantoken == WORD_TOKEN) + { + size = 1; + type = scantoken == BYTE_TOKEN ? BYTE_TYPE : WORD_TYPE; + if (scan() == OPEN_BRACKET_TOKEN) + { + size = 0; + parse_constexpr(&size, &constsize); + if (scantoken != CLOSE_BRACKET_TOKEN) + { + parse_error("Missing closing bracket"); + return (0); + } + scan(); + } + do { + idlen = 0; + if (scantoken == ID_TOKEN) + { + idstr = tokenstr; + idlen = tokenlen; + if (scan() == OPEN_BRACKET_TOKEN) + { + size = 0; + parse_constexpr(&size, &constsize); + if (scantoken != CLOSE_BRACKET_TOKEN) + { + parse_error("Missing closing bracket"); + return (0); + } + scan(); + } + } + if (type & WORD_TYPE) + size *= 2; + if (idlen) + idconst_add(idstr, idlen, offset); + offset += size; + } while (scantoken == COMMA_TOKEN); + if (scantoken != EOL_TOKEN && scantoken != COMMENT_TOKEN) + return (0); + } + if (struclen) + idconst_add(strucid, struclen, offset); + return (scantoken == END_TOKEN); +} int parse_vars(int type) { long value; @@ -1091,6 +1152,13 @@ int parse_vars(int type) } idconst_add(idstr, idlen, value); break; + case STRUC_TOKEN: + if (!parse_struc()) + { + parse_error("Bad structure definition"); + return (0); + } + break; case EXPORT_TOKEN: if (type & (EXTERN_TYPE | LOCAL_TYPE)) { diff --git a/Platform/Apple/tools/PLASMA/src/plvm02.s b/Platform/Apple/tools/PLASMA/src/plvm02.s index 3ebb4151..ebad6b49 100644 --- a/Platform/Apple/tools/PLASMA/src/plvm02.s +++ b/Platform/Apple/tools/PLASMA/src/plvm02.s @@ -258,7 +258,7 @@ CMDEXEC = * ; ; INSTALL PAGE 3 VECTORS ; - LDY #$11 + LDY #$12 - LDA PAGE3,Y STA INTERP,Y DEY @@ -401,28 +401,30 @@ IDXW LDA ESTKL,X ;* MUL STY IPY LDY #$10 - LDA #$00 - STA TMPL ; PRODL - STA TMPH ; PRODH -MULLP LSR ESTKH,X ; MULTPLRH - ROR ESTKL,X ; MULTPLRL - BCC + - LDA ESTKL+1,X ; MULTPLNDL - CLC - ADC TMPL ; PRODL + LDA ESTKL+1,X + EOR #$FF STA TMPL - LDA ESTKH+1,X ; MULTPLNDH - ADC TMPH ; PRODH + LDA ESTKH+1,X + EOR #$FF STA TMPH -+ ASL ESTKL+1,X ; MULTPLNDL - ROL ESTKH+1,X ; MULTPLNDH + LDA #$00 + STA ESTKL+1,X ; PRODL +; STA ESTKH+1,X ; PRODH +MULLP LSR TMPH ; MULTPLRH + ROR TMPL ; MULTPLRL + BCS + + STA ESTKH+1,X ; PRODH + LDA ESTKL,X ; MULTPLNDL + ADC ESTKL+1,X ; PRODL + STA ESTKL+1,X + LDA ESTKH,X ; MULTPLNDH + ADC ESTKH+1,X ; PRODH ++ ASL ESTKL,X ; MULTPLNDL + ROL ESTKH,X ; MULTPLNDH DEY BNE MULLP + STA ESTKH+1,X ; PRODH INX -; LDA TMPH ; PRODH - STA ESTKH,X - LDA TMPL ; PRODL - STA ESTKL,X LDY IPY JMP NEXTOP ;* @@ -437,6 +439,10 @@ _NEG LDA #$00 STA ESTKH,X RTS _DIV STY IPY + LDY #$11 ; #BITS+1 + LDA #$00 + STA TMPL ; REMNDRL + STA TMPH ; REMNDRH LDA ESTKH,X AND #$80 STA DVSIGN @@ -451,34 +457,28 @@ _DIV STY IPY INC DVSIGN BNE _DIV1 + ORA ESTKL+1,X ; DVDNDL - BNE _DIV1 - STA TMPL - STA TMPH - RTS -_DIV1 LDY #$11 ; #BITS+1 - LDA #$00 - STA TMPL ; REMNDRL - STA TMPH ; REMNDRH -- ASL ESTKL+1,X ; DVDNDL + BEQ _DIVEX +_DIV1 ASL ESTKL+1,X ; DVDNDL ROL ESTKH+1,X ; DVDNDH DEY - BCC - - STY ESTKL-1,X + BCC _DIV1 _DIVLP ROL TMPL ; REMNDRL ROL TMPH ; REMNDRH LDA TMPL ; REMNDRL - SEC - SBC ESTKL,X ; DVSRL - TAY + CMP ESTKL,X ; DVSRL LDA TMPH ; REMNDRH SBC ESTKH,X ; DVSRH BCC + STA TMPH ; REMNDRH - STY TMPL ; REMNDRL -+ ROL ESTKL+1,X ; DVDNDL + LDA TMPL ; REMNDRL + SBC ESTKL,X ; DVSRL + STA TMPL ; REMNDRL + SEC ++ ROL ESTKL+1,X ; DVDNDL ROL ESTKH+1,X ; DVDNDH - DEC ESTKL-1,X + DEY BNE _DIVLP +_DIVEX INX LDY IPY RTS ;* diff --git a/Platform/Apple/tools/PLASMA/src/test.pla b/Platform/Apple/tools/PLASMA/src/test.pla index e4f97f99..0e2b6a6d 100755 --- a/Platform/Apple/tools/PLASMA/src/test.pla +++ b/Platform/Apple/tools/PLASMA/src/test.pla @@ -1,29 +1,36 @@ // // Include all imported modules and their data/functions. // - include(stdlib.plh) include(testlib.plh) - +// +// Structure definition. +// +struc mystruc + byte cmd + word param + byte[3] + word data +end // // Declare all global variables for this module. +// Note that arrays are declared with prefix []. postfix [], or no []. +// Only arrays with predclared sizes need [ and ], such as "int[3] a". // - -byte hello[] = "Hello, Apple " -byte a1[] = "1" -byte a2[] = "][" -byte a2p[] = "][+" -byte a2e[] = "//e" -byte a2c[] = "//c" -byte a3[] = "///" -word struct[] = 1, 10, 100, 1000, 10000 +byte[] hello = "Hello, Apple " +byte[] a1 = "1" +byte[] a2 = "][" +byte[] a2p = "][+" +byte[] a2e = "//e" +byte[] a2c = "//c" +byte[] a3 = "///" +byte[] offsets = "Structure offsets:" +word array[] = 1, 10, 100, 1000, 10000 word ptr -byte spaces[] = " " - +byte spaces = " " // // Define functions. // - def tens(start) word i i = start @@ -35,7 +42,6 @@ def tens(start) i = i / 10 until i == 0 end - def ascii byte i i = 32 @@ -44,7 +50,6 @@ def ascii i = i + 1 loop end - def nums(range) word i for i = range downto -range step range/10 @@ -52,7 +57,6 @@ def nums(range) putln next end - export def main(range) nums(*range) tens(*range*10) @@ -83,9 +87,8 @@ export def main(range) wend putln end - -ptr=@struct -main(@struct:6) +ptr = @array +main(@array:6) puti((ptr):6) putln puti(ptr=>6) @@ -94,4 +97,14 @@ puti((ptr).6) putln puti(ptr->6) putln +puts(@offsets) +putln +puti(cmd) +putln +puti(param) +putln +puti(data) +putln +puti(mystruc) +putln done diff --git a/Platform/Apple/tools/PLASMA/src/tokens.h b/Platform/Apple/tools/PLASMA/src/tokens.h index b5288a23..3d729b2a 100755 --- a/Platform/Apple/tools/PLASMA/src/tokens.h +++ b/Platform/Apple/tools/PLASMA/src/tokens.h @@ -42,7 +42,7 @@ #define RETURN_TOKEN TOKEN(28) #define BREAK_TOKEN TOKEN(29) #define SYSFLAGS_TOKEN TOKEN(30) -#define EXIT_TOKEN TOKEN(31) +#define STRUC_TOKEN TOKEN(31) #define EVAL_TOKEN TOKEN(32) /* * Double operand operators.