1 /******************************************************************************
3 * Module Name: aslsupport.l - Flex/lex scanner C support routines.
4 * NOTE: Included into aslcompile.l, not compiled by itself.
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2016, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #define ASL_SPACES_PER_TAB 4
49 #define ASL_NORMAL_CHAR 0
50 #define ASL_ESCAPE_SEQUENCE 1
51 #define ASL_OCTAL_CONSTANT 2
52 #define ASL_HEX_CONSTANT 3
55 /* File node - used for "Include" operator file stack */
57 typedef struct asl_file_node
60 UINT32 CurrentLineNumber;
61 YY_BUFFER_STATE State;
63 struct asl_file_node *Next;
67 /* File stack for the "Include" operator (NOT #include operator) */
69 ASL_FILE_NODE *Gbl_IncludeFileStack = NULL;
72 /*******************************************************************************
74 * FUNCTION: AslParserCleanup
76 * Used to delete the current buffer
78 ******************************************************************************/
85 yy_delete_buffer (YY_CURRENT_BUFFER);
89 /*******************************************************************************
91 * FUNCTION: AslDoLineDirective
93 * PARAMETERS: None. Uses input() to access current source code line
95 * RETURN: Updates global line number and filename
97 * DESCRIPTION: Handle #line directives emitted by the preprocessor.
99 * The #line directive is emitted by the preprocesser, and is used to
100 * pass through line numbers from the original source code file to the
101 * preprocessor output file (.i). This allows any compiler-generated
102 * error messages to be displayed with the correct line number.
104 ******************************************************************************/
116 Gbl_HasIncludeFiles = TRUE;
118 /* Eat the entire line that contains the #line directive */
120 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
122 while ((c = input()) != '\n' && c != EOF)
129 /* First argument is the actual line number */
131 Token = strtok (Gbl_CurrentLineBuffer, " ");
137 /* First argument is the line number */
139 LineNumber = (UINT32) UtDoConstant (Token);
141 /* Emit the appropriate number of newlines */
143 Gbl_CurrentColumn = 0;
144 if (LineNumber > Gbl_CurrentLineNumber)
146 for (i = 0; i < (LineNumber - Gbl_CurrentLineNumber); i++)
148 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, "\n", 1);
153 FlSetLineNumber (LineNumber);
155 /* Second argument is the optional filename (in double quotes) */
157 Token = strtok (NULL, " \"");
160 Filename = ACPI_ALLOCATE_ZEROED (strlen (Token) + 1);
161 strcpy (Filename, Token);
162 FlSetFilename (Filename);
165 /* Third argument is not supported at this time */
169 /* Reset globals for a new line */
171 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
172 Gbl_CurrentColumn = 0;
173 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
177 /*******************************************************************************
179 * FUNCTION: AslPopInputFileStack
183 * RETURN: 0 if a node was popped, -1 otherwise
185 * DESCRIPTION: Pop the top of the input file stack and point the parser to
186 * the saved parse buffer contained in the fnode. Also, set the
187 * global line counters to the saved values. This function is
188 * called when an include file reaches EOF.
190 ******************************************************************************/
193 AslPopInputFileStack (
196 ASL_FILE_NODE *Fnode;
199 Gbl_PreviousIncludeFilename = Gbl_Files[ASL_FILE_INPUT].Filename;
200 Fnode = Gbl_IncludeFileStack;
201 DbgPrint (ASL_PARSE_OUTPUT,
202 "\nPop InputFile Stack, Fnode %p\n", Fnode);
204 DbgPrint (ASL_PARSE_OUTPUT,
205 "Include: Closing \"%s\"\n\n", Gbl_Files[ASL_FILE_INPUT].Filename);
212 /* Close the current include file */
216 /* Update the top-of-stack */
218 Gbl_IncludeFileStack = Fnode->Next;
220 /* Reset global line counter and filename */
222 Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
223 Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
225 /* Point the parser to the popped file */
227 yy_delete_buffer (YY_CURRENT_BUFFER);
228 yy_switch_to_buffer (Fnode->State);
230 /* All done with this node */
237 /*******************************************************************************
239 * FUNCTION: AslPushInputFileStack
241 * PARAMETERS: InputFile - Open file pointer
242 * Filename - Name of the file
246 * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
247 * to this file. Called when an include file is successfully
250 ******************************************************************************/
253 AslPushInputFileStack (
257 ASL_FILE_NODE *Fnode;
258 YY_BUFFER_STATE State;
261 /* Save the current state in an Fnode */
263 Fnode = UtLocalCalloc (sizeof (ASL_FILE_NODE));
266 Fnode->Next = Gbl_IncludeFileStack;
267 Fnode->State = YY_CURRENT_BUFFER;
268 Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
269 Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
271 /* Push it on the stack */
273 Gbl_IncludeFileStack = Fnode;
275 /* Point the parser to this file */
277 State = yy_create_buffer (InputFile, YY_BUF_SIZE);
278 yy_switch_to_buffer (State);
280 DbgPrint (ASL_PARSE_OUTPUT,
281 "\nPush InputFile Stack, returning %p\n\n", InputFile);
283 /* Reset the global line count and filename */
285 Gbl_Files[ASL_FILE_INPUT].Filename =
286 UtStringCacheCalloc (strlen (Filename) + 1);
288 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
290 Gbl_CurrentLineNumber = 1;
295 /*******************************************************************************
297 * FUNCTION: AslResetCurrentLineBuffer
303 * DESCRIPTION: Reset the Line Buffer to zero, increment global line numbers.
305 ******************************************************************************/
308 AslResetCurrentLineBuffer (
312 if (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle)
314 FlWriteFile (ASL_FILE_SOURCE_OUTPUT, Gbl_CurrentLineBuffer,
315 Gbl_LineBufPtr - Gbl_CurrentLineBuffer);
318 Gbl_CurrentLineOffset += Gbl_CurrentColumn;
319 Gbl_CurrentColumn = 0;
321 Gbl_CurrentLineNumber++;
322 Gbl_LogicalLineNumber++;
323 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
327 /*******************************************************************************
329 * FUNCTION: AslInsertLineBuffer
331 * PARAMETERS: SourceChar - One char from the input ASL source file
335 * DESCRIPTION: Put one character of the source file into the temp line buffer
337 ******************************************************************************/
340 AslInsertLineBuffer (
347 if (SourceChar == EOF)
352 Gbl_InputByteCount++;
354 /* Handle tabs. Convert to spaces */
356 if (SourceChar == '\t')
359 Count = ASL_SPACES_PER_TAB -
360 (Gbl_CurrentColumn & (ASL_SPACES_PER_TAB-1));
363 for (i = 0; i < Count; i++)
367 /* Insert the character into the line buffer */
369 *Gbl_LineBufPtr = (UINT8) SourceChar;
373 (Gbl_CurrentLineBuffer + (Gbl_LineBufferSize - 1)))
377 * Warning if we have split a long source line.
378 * <Probably overkill>
380 sprintf (MsgBuffer, "Max %u", Gbl_LineBufferSize);
381 AslCommonError (ASL_WARNING, ASL_MSG_LONG_LINE,
382 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
383 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
384 Gbl_Files[ASL_FILE_INPUT].Filename, MsgBuffer);
387 AslResetCurrentLineBuffer ();
389 else if (SourceChar == '\n')
393 AslResetCurrentLineBuffer ();
399 /*******************************************************************************
403 * PARAMETERS: yytext - Contains the matched keyword.
404 * Type - Keyword/Character type:
405 * 0 = anything except a keyword
406 * 1 = pseudo-keywords
407 * 2 = non-executable ASL keywords
408 * 3 = executable ASL keywords
412 * DESCRIPTION: Count keywords and put them into the line buffer
414 ******************************************************************************/
434 TotalExecutableOpcodes++;
442 for (i = 0; (yytext[i] != 0) && (yytext[i] != EOF); i++)
444 AslInsertLineBuffer (yytext[i]);
450 /*******************************************************************************
452 * FUNCTION: AslDoComment
458 * DESCRIPTION: Process a standard comment.
460 ******************************************************************************/
470 AslInsertLineBuffer ('/');
471 AslInsertLineBuffer ('*');
475 /* Eat chars until end-of-comment */
477 while (((c = input ()) != '*') && (c != EOF))
479 AslInsertLineBuffer (c);
489 * Check for nested comment -- can help catch cases where a previous
490 * comment was accidently left unterminated
492 if ((c1 == '/') && (c == '*'))
494 AslCommonError (ASL_WARNING, ASL_MSG_NESTED_COMMENT,
495 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
496 Gbl_InputByteCount, Gbl_CurrentColumn,
497 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
500 /* Comment is closed only if the NEXT character is a slash */
502 AslInsertLineBuffer (c);
504 if (((c1 = input ()) != '/') && (c1 != EOF))
515 AslInsertLineBuffer (c1);
521 * Premature End-Of-File
523 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
524 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
525 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
526 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
531 /*******************************************************************************
533 * FUNCTION: AslDoCommentType2
539 * DESCRIPTION: Process a new "//" comment.
541 ******************************************************************************/
550 AslInsertLineBuffer ('/');
551 AslInsertLineBuffer ('/');
553 while (((c = input ()) != '\n') && (c != EOF))
555 AslInsertLineBuffer (c);
560 /* End of file is OK, change to newline. Let parser detect EOF later */
565 AslInsertLineBuffer (c);
570 /*******************************************************************************
572 * FUNCTION: AslDoStringLiteral
578 * DESCRIPTION: Process a string literal (surrounded by quotes)
580 ******************************************************************************/
586 char *StringBuffer = MsgBuffer;
587 char *EndBuffer = MsgBuffer + ASL_MSG_BUFFER_SIZE;
590 UINT32 State = ASL_NORMAL_CHAR;
593 char ConvertBuffer[4];
597 * Eat chars until end-of-literal.
598 * NOTE: Put back the original surrounding quotes into the
599 * source line buffer.
601 AslInsertLineBuffer ('\"');
602 while ((StringChar = input()) != EOF)
604 AslInsertLineBuffer (StringChar);
609 case ASL_NORMAL_CHAR:
615 * Special handling for backslash-escape sequence. We will
616 * toss the backslash and translate the escape char(s).
618 State = ASL_ESCAPE_SEQUENCE;
623 /* String terminator */
625 goto CompletedString;
634 case ASL_ESCAPE_SEQUENCE:
636 State = ASL_NORMAL_CHAR;
641 StringChar = 0x07; /* BELL */
646 StringChar = 0x08; /* BACKSPACE */
651 StringChar = 0x0C; /* FORMFEED */
656 StringChar = 0x0A; /* LINEFEED */
661 StringChar = 0x0D; /* CARRIAGE RETURN*/
666 StringChar = 0x09; /* HORIZONTAL TAB */
671 StringChar = 0x0B; /* VERTICAL TAB */
676 State = ASL_HEX_CONSTANT;
680 case '\'': /* Single Quote */
681 case '\"': /* Double Quote */
682 case '\\': /* Backslash */
688 /* Check for an octal digit (0-7) */
690 if (ACPI_IS_OCTAL_DIGIT (StringChar))
692 State = ASL_OCTAL_CONSTANT;
693 ConvertBuffer[0] = StringChar;
698 /* Unknown escape sequence issue warning, but use the character */
700 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_ESCAPE,
701 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
702 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
703 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
709 case ASL_OCTAL_CONSTANT:
711 /* Up to three octal digits allowed */
713 if (!ACPI_IS_OCTAL_DIGIT (StringChar) ||
717 * Reached end of the constant. Convert the assembled ASCII
718 * string and resume processing of the next character
720 ConvertBuffer[i] = 0;
721 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 8);
723 /* Check for NULL or non-ascii character (ignore if so) */
725 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
727 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
728 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
729 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
730 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
734 *StringBuffer = (char) Digit;
736 if (StringBuffer >= EndBuffer)
742 State = ASL_NORMAL_CHAR;
747 /* Append another digit of the constant */
749 ConvertBuffer[i] = StringChar;
753 case ASL_HEX_CONSTANT:
755 /* Up to two hex digits allowed */
757 if (!isxdigit (StringChar) ||
761 * Reached end of the constant. Convert the assembled ASCII
762 * string and resume processing of the next character
764 ConvertBuffer[i] = 0;
765 Digit = (UINT8) strtoul (ConvertBuffer, NULL, 16);
767 /* Check for NULL or non-ascii character (ignore if so) */
769 if ((Digit == 0) || (Digit > ACPI_ASCII_MAX))
771 AslCommonError (ASL_WARNING, ASL_MSG_INVALID_STRING,
772 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
773 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
774 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
778 *StringBuffer = (char) Digit;
780 if (StringBuffer >= EndBuffer)
786 State = ASL_NORMAL_CHAR;
791 /* Append another digit of the constant */
793 ConvertBuffer[i] = StringChar;
802 /* Save the finished character */
804 *StringBuffer = StringChar;
806 if (StringBuffer >= EndBuffer)
813 * Premature End-Of-File
815 AslCommonError (ASL_ERROR, ASL_MSG_EARLY_EOF,
816 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
817 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
818 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
824 * Null terminate the input string and copy string to a new buffer
828 CleanString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
831 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
832 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
833 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
834 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
838 strcpy (CleanString, MsgBuffer);
839 AslCompilerlval.s = CleanString;
845 /* Literal was too long */
847 AslCommonError (ASL_ERROR, ASL_MSG_STRING_LENGTH,
848 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
849 Gbl_CurrentLineOffset, Gbl_CurrentColumn,
850 Gbl_Files[ASL_FILE_INPUT].Filename, "Max length 4096");