From 56b8ead1128434d05e0998e672e9e6d449f46d70 Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 9 Sep 2001 09:33:06 +0000 Subject: [PATCH] Move inline asm parsing into a separate module git-svn-id: svn://svn.cc65.org/cc65/trunk@880 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/asmstmt.c | 105 +++++++++++++++++++++++++++++++++++++++ src/cc65/asmstmt.h | 60 ++++++++++++++++++++++ src/cc65/compile.c | 3 +- src/cc65/expr.c | 56 +-------------------- src/cc65/make/gcc.mak | 1 + src/cc65/make/watcom.mak | 2 + 6 files changed, 172 insertions(+), 55 deletions(-) create mode 100644 src/cc65/asmstmt.c create mode 100644 src/cc65/asmstmt.h diff --git a/src/cc65/asmstmt.c b/src/cc65/asmstmt.c new file mode 100644 index 000000000..76f5596d9 --- /dev/null +++ b/src/cc65/asmstmt.c @@ -0,0 +1,105 @@ +/*****************************************************************************/ +/* */ +/* asmstmt.c */ +/* */ +/* Inline assembler statements for the cc65 C compiler */ +/* */ +/* */ +/* */ +/* (C) 2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include + +/* cc65 */ +#include "codegen.h" +#include "error.h" +#include "litpool.h" +#include "scanner.h" +#include "asmstmt.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void AsmStatement (void) +/* This function parses ASM statements. The syntax of the ASM directive + * looks like the one defined for C++ (C has no ASM directive), that is, + * a string literal in parenthesis. + */ +{ + /* Skip the ASM */ + NextToken (); + + /* Need left parenthesis */ + ConsumeLParen (); + + /* String literal */ + if (CurTok.Tok != TOK_SCONST) { + Error ("String literal expected"); + } else { + + /* The string literal may consist of more than one line of assembler + * code. Separate the single lines and output the code. + */ + const char* S = GetLiteral (CurTok.IVal); + while (*S) { + + /* Separate the lines */ + const char* E = strchr (S, '\n'); + if (E) { + /* Found a newline */ + g_asmcode (S, E-S); + S = E+1; + } else { + int Len = strlen (S); + g_asmcode (S, Len); + S += Len; + } + } + + /* Reset the string pointer, effectivly clearing the string from the + * string table. Since we're working with one token lookahead, this + * will fail if the next token is also a string token, but that's a + * syntax error anyway, because we expect a right paren. + */ + ResetLiteralPoolOffs (CurTok.IVal); + } + + /* Skip the string token */ + NextToken (); + + /* Closing paren needed */ + ConsumeRParen (); +} + + + diff --git a/src/cc65/asmstmt.h b/src/cc65/asmstmt.h new file mode 100644 index 000000000..9de91f492 --- /dev/null +++ b/src/cc65/asmstmt.h @@ -0,0 +1,60 @@ +/*****************************************************************************/ +/* */ +/* asmstmt.h */ +/* */ +/* Inline assembler statements for the cc65 C compiler */ +/* */ +/* */ +/* */ +/* (C) 2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef ASMSTMT_H +#define ASMSTMT_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void AsmStatement (void); +/* This function parses ASM statements. The syntax of the ASM directive + * looks like the one defined for C++ (C has no ASM directive), that is, + * a string literal in parenthesis. + */ + + + +/* End of asmstmt.h */ + +#endif + + + diff --git a/src/cc65/compile.c b/src/cc65/compile.c index 19a5446e3..de1144bb0 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -40,6 +40,7 @@ /* cc65 */ #include "asmlabel.h" +#include "asmstmt.h" #include "codegen.h" #include "declare.h" #include "error.h" @@ -87,7 +88,7 @@ static void Parse (void) /* Check for an ASM statement (which is allowed also on global level) */ if (CurTok.Tok == TOK_ASM) { - doasm (); + AsmStatement (); ConsumeSemi (); continue; } diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 71b9788ba..46484528e 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -17,6 +17,7 @@ /* cc65 */ #include "asmcode.h" #include "asmlabel.h" +#include "asmstmt.h" #include "codegen.h" #include "datatype.h" #include "declare.h" @@ -758,59 +759,6 @@ static void CallFunction (ExprDesc* lval) -void doasm (void) -/* This function parses ASM statements. The syntax of the ASM directive - * looks like the one defined for C++ (C has no ASM directive), that is, - * a string literal in parenthesis. - */ -{ - /* Skip the ASM */ - NextToken (); - - /* Need left parenthesis */ - ConsumeLParen (); - - /* String literal */ - if (CurTok.Tok != TOK_SCONST) { - Error ("String literal expected"); - } else { - - /* The string literal may consist of more than one line of assembler - * code. Separate the single lines and output the code. - */ - const char* S = GetLiteral (CurTok.IVal); - while (*S) { - - /* Allow lines up to 256 bytes */ - const char* E = strchr (S, '\n'); - if (E) { - /* Found a newline */ - g_asmcode (S, E-S); - S = E+1; - } else { - int Len = strlen (S); - g_asmcode (S, Len); - S += Len; - } - } - - /* Reset the string pointer, effectivly clearing the string from the - * string table. Since we're working with one token lookahead, this - * will fail if the next token is also a string token, but that's a - * syntax error anyway, because we expect a right paren. - */ - ResetLiteralPoolOffs (CurTok.IVal); - } - - /* Skip the string token */ - NextToken (); - - /* Closing paren needed */ - ConsumeRParen (); -} - - - static int primary (ExprDesc* lval) /* This is the lowest level of the expression parser. */ { @@ -977,7 +925,7 @@ static int primary (ExprDesc* lval) /* ASM statement? */ if (CurTok.Tok == TOK_ASM) { - doasm (); + AsmStatement (); lval->Type = type_void; lval->Flags = E_MEXPR; lval->ConstVal = 0; diff --git a/src/cc65/make/gcc.mak b/src/cc65/make/gcc.mak index 38db8dffd..5facfeaaa 100644 --- a/src/cc65/make/gcc.mak +++ b/src/cc65/make/gcc.mak @@ -25,6 +25,7 @@ LDFLAGS= OBJS = anonname.o \ asmcode.o \ asmlabel.o \ + asmstmt.o \ codeent.o \ codegen.o \ codelab.o \ diff --git a/src/cc65/make/watcom.mak b/src/cc65/make/watcom.mak index 62a982120..ad6585172 100644 --- a/src/cc65/make/watcom.mak +++ b/src/cc65/make/watcom.mak @@ -70,6 +70,7 @@ CCCFG = $(CCCFG) -i=..\common OBJS = anonname.obj \ asmcode.obj \ asmlabel.obj \ + asmstmt.obj \ codeent.obj \ codegen.obj \ codelab.obj \ @@ -138,6 +139,7 @@ NAME $< FILE anonname.obj FILE asmcode.obj FILE asmlabel.obj +FILE asmstmt.obj FILE codeent.obj FILE codegen.obj FILE codelab.obj -- 2.39.5