1 /*****************************************************************************/
5 /* Inline assembler statements for the cc65 C compiler */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 static void AsmRangeError (unsigned Arg)
62 /* Print a diagnostic about a range error in the argument with the given number */
64 Error ("Range error in argument %u", Arg);
69 static void AsmErrorSkip (void)
70 /* Called in case of an error, skips tokens until the closing paren or a
71 * semicolon is reached.
74 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
75 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
80 static SymEntry* AsmGetSym (unsigned Arg, unsigned Type)
81 /* Find the symbol with the name currently in NextTok. The symbol must be of
82 * the given type. On errors, NULL is returned.
87 /* We expect an argument separated by a comma */
90 /* Argument must be an identifier */
91 if (CurTok.Tok != TOK_IDENT) {
92 Error ("Identifier expected for argument %u", Arg);
97 /* Get a pointer to the symbol table entry */
98 Sym = FindSym (CurTok.Ident);
100 /* Did we find a symbol with this name? */
102 Error ("Undefined symbol `%s' for argument %u", CurTok.Ident, Arg);
107 /* We found the symbol - skip the name token */
110 /* Check if we have a global symbol */
111 if ((Sym->Flags & Type) != Type) {
112 Error ("Type of argument %u differs from format specifier", Arg);
117 /* Mark the symbol as referenced */
118 Sym->Flags |= SC_REF;
126 static void ParseByteArg (StrBuf* T, unsigned Arg)
127 /* Parse the %b format specifier */
132 /* We expect an argument separated by a comma */
135 /* Evaluate the expression */
136 ConstAbsIntExpr (hie1, &Expr);
138 /* Check the range but allow negative values if the type is signed */
139 if (IsSignUnsigned (Expr.Type)) {
140 if (Expr.Val < 0 || Expr.Val > 0xFF) {
145 if (Expr.Val < -128 || Expr.Val > 127) {
151 /* Convert into a hex number */
152 xsprintf (Buf, sizeof (Buf), "$%02lX", Expr.Val & 0xFF);
154 /* Add the number to the target buffer */
155 SB_AppendStr (T, Buf);
160 static void ParseWordArg (StrBuf* T, unsigned Arg)
161 /* Parse the %w format specifier */
166 /* We expect an argument separated by a comma */
169 /* Evaluate the expression */
170 ConstAbsIntExpr (hie1, &Expr);
172 /* Check the range but allow negative values if the type is signed */
173 if (IsSignUnsigned (Expr.Type)) {
174 if (Expr.Val < 0 || Expr.Val > 0xFFFF) {
179 if (Expr.Val < -32768 || Expr.Val > 32767) {
185 /* Convert into a hex number */
186 xsprintf (Buf, sizeof (Buf), "$%04lX", Expr.Val & 0xFFFF);
188 /* Add the number to the target buffer */
189 SB_AppendStr (T, Buf);
194 static void ParseLongArg (StrBuf* T, unsigned Arg attribute ((unused)))
195 /* Parse the %l format specifier */
200 /* We expect an argument separated by a comma */
203 /* Evaluate the expression */
204 ConstAbsIntExpr (hie1, &Expr);
206 /* Convert into a hex number */
207 xsprintf (Buf, sizeof (Buf), "$%08lX", Expr.Val & 0xFFFFFFFF);
209 /* Add the number to the target buffer */
210 SB_AppendStr (T, Buf);
215 static void ParseGVarArg (StrBuf* T, unsigned Arg)
216 /* Parse the %v format specifier */
218 /* Parse the symbol name parameter and check the type */
219 SymEntry* Sym = AsmGetSym (Arg, SC_STATIC);
221 /* Some sort of error */
225 /* Check for external linkage */
226 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
227 /* External linkage */
228 /* ### FIXME: Asm name should be generated by codegen */
229 SB_AppendChar (T, '_');
230 SB_AppendStr (T, Sym->Name);
231 } else if (Sym->Flags & SC_REGISTER) {
233 xsprintf (Buf, sizeof (Buf), "regbank+%d", Sym->V.R.RegOffs);
234 SB_AppendStr (T, Buf);
236 /* Static variable */
238 xsprintf (Buf, sizeof (Buf), "L%04X", Sym->V.Label);
239 SB_AppendStr (T, Buf);
245 static void ParseLVarArg (StrBuf* T, unsigned Arg)
246 /* Parse the %o format specifier */
251 /* Parse the symbol name parameter and check the type */
252 SymEntry* Sym = AsmGetSym (Arg, SC_AUTO);
254 /* Some sort of error */
258 /* The symbol may be a parameter to a variadic function. In this case, we
259 * don't have a fixed stack offset, so check it and bail out with an error
260 * if this is the case.
262 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
263 Error ("Argument %u has no fixed stack offset", Arg);
268 /* Calculate the current offset from SP */
269 Offs = Sym->V.Offs - StackPtr;
271 /* Output the offset */
272 xsprintf (Buf, sizeof (Buf), (Offs > 0xFF)? "$%04X" : "$%02X", Offs);
273 SB_AppendStr (T, Buf);
278 static void ParseStrArg (StrBuf* T, unsigned Arg attribute ((unused)))
279 /* Parse the %s format specifier */
284 /* We expect an argument separated by a comma */
287 /* Check what comes */
288 switch (CurTok.Tok) {
292 SB_AppendStr (T, CurTok.Ident);
297 /* String constant */
298 SB_AppendStr (T, GetLiteral (CurTok.IVal));
299 ResetLiteralPoolOffs (CurTok.IVal);
304 ConstAbsIntExpr (hie1, &Expr);
305 xsprintf (Buf, sizeof (Buf), "%ld", Expr.Val);
306 SB_AppendStr (T, Buf);
313 static void ParseAsm (void)
314 /* Parse the contents of the ASM statement */
319 /* Create a target string buffer */
320 StrBuf T = AUTO_STRBUF_INITIALIZER;
322 /* Create a string buffer from the string literal */
323 StrBuf S = AUTO_STRBUF_INITIALIZER;
324 GetLiteralStrBuf (&S, CurTok.IVal);
326 /* Reset the string pointer, effectivly clearing the string from the
327 * string table. Since we're working with one token lookahead, this
328 * will fail if the next token is also a string token, but that's a
329 * syntax error anyway, because we expect a right paren.
331 ResetLiteralPoolOffs (CurTok.IVal);
333 /* Skip the string token */
336 /* Parse the statement. It may contain several lines and one or more
337 * of the following place holders:
338 * %b - Numerical 8 bit value
339 * %w - Numerical 16 bit value
340 * %l - Numerical 32 bit value
341 * %v - Assembler name of a (global) variable
342 * %o - Stack offset of a (local) variable
343 * %s - Any argument converted to a string (almost)
347 while ((C = SB_Get (&S)) != '\0') {
349 /* If it is a newline, the current line is ready to go */
352 /* Pass it to the backend and start over */
356 } else if (C == '%') {
358 /* Format specifier */
362 case '%': SB_AppendChar (&T, '%'); break;
363 case 'b': ParseByteArg (&T, Arg); break;
364 case 'l': ParseLongArg (&T, Arg); break;
365 case 'o': ParseLVarArg (&T, Arg); break;
366 case 's': ParseStrArg (&T, Arg); break;
367 case 'v': ParseGVarArg (&T, Arg); break;
368 case 'w': ParseWordArg (&T, Arg); break;
370 Error ("Error in __asm__ format specifier %u", Arg);
377 /* A normal character, just copy it */
378 SB_AppendChar (&T, C);
383 /* If the target buffer is not empty, we have a last line in there */
384 if (!SB_IsEmpty (&T)) {
389 /* Call the string buf destructors */
396 void AsmStatement (void)
397 /* This function parses ASM statements. The syntax of the ASM directive
398 * looks like the one defined for C++ (C has no ASM directive), that is,
399 * a string literal in parenthesis.
405 /* Need left parenthesis */
406 if (!ConsumeLParen ()) {
411 if (CurTok.Tok != TOK_SCONST) {
413 /* Print a diagnostic */
414 Error ("String literal expected");
416 /* Try some smart error recovery: Skip tokens until we reach the
417 * enclosing paren, or a semicolon.
423 /* Parse the ASM statement */
427 /* Closing paren needed */