]> git.sur5r.net Git - cc65/blob - src/cc65/asmstmt.c
Move inline asm parsing into a separate module
[cc65] / src / cc65 / asmstmt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asmstmt.c                                 */
4 /*                                                                           */
5 /*            Inline assembler statements for the cc65 C compiler            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* cc65 */          
39 #include "codegen.h"
40 #include "error.h"
41 #include "litpool.h"
42 #include "scanner.h"
43 #include "asmstmt.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 void AsmStatement (void)
54 /* This function parses ASM statements. The syntax of the ASM directive
55  * looks like the one defined for C++ (C has no ASM directive), that is,
56  * a string literal in parenthesis.
57  */
58 {
59     /* Skip the ASM */
60     NextToken ();
61
62     /* Need left parenthesis */
63     ConsumeLParen ();
64
65     /* String literal */
66     if (CurTok.Tok != TOK_SCONST) {
67         Error ("String literal expected");
68     } else {
69
70         /* The string literal may consist of more than one line of assembler
71          * code. Separate the single lines and output the code.
72          */
73         const char* S = GetLiteral (CurTok.IVal);
74         while (*S) {
75
76             /* Separate the lines */
77             const char* E = strchr (S, '\n');
78             if (E) {
79                 /* Found a newline */
80                 g_asmcode (S, E-S);
81                 S = E+1;
82             } else {
83                 int Len = strlen (S);
84                 g_asmcode (S, Len);
85                 S += Len;
86             }
87         }
88
89         /* Reset the string pointer, effectivly clearing the string from the
90          * string table. Since we're working with one token lookahead, this
91          * will fail if the next token is also a string token, but that's a
92          * syntax error anyway, because we expect a right paren.
93          */
94         ResetLiteralPoolOffs (CurTok.IVal);
95     }
96
97     /* Skip the string token */
98     NextToken ();
99
100     /* Closing paren needed */
101     ConsumeRParen ();
102 }
103
104
105