112099Sjoerg%{ 291592Smarkm/* $NetBSD: scan.l,v 1.26 2002/01/31 22:30:21 tv Exp $ */ 312099Sjoerg 412099Sjoerg/* 591592Smarkm * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 612099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl 712099Sjoerg * All Rights Reserved. 812099Sjoerg * 912099Sjoerg * Redistribution and use in source and binary forms, with or without 1012099Sjoerg * modification, are permitted provided that the following conditions 1112099Sjoerg * are met: 1212099Sjoerg * 1. Redistributions of source code must retain the above copyright 1312099Sjoerg * notice, this list of conditions and the following disclaimer. 1412099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1512099Sjoerg * notice, this list of conditions and the following disclaimer in the 1612099Sjoerg * documentation and/or other materials provided with the distribution. 1712099Sjoerg * 3. All advertising materials mentioning features or use of this software 1812099Sjoerg * must display the following acknowledgement: 1912099Sjoerg * This product includes software developed by Jochen Pohl for 2012099Sjoerg * The NetBSD Project. 2112099Sjoerg * 4. The name of the author may not be used to endorse or promote products 2212099Sjoerg * derived from this software without specific prior written permission. 2312099Sjoerg * 2412099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2512099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2612099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2712099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2812099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2912099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3012099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3112099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3212099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3312099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3412099Sjoerg */ 3512099Sjoerg 3691592Smarkm#include <sys/cdefs.h> 3791592Smarkm#if defined(__RCSID) && !defined(lint) 3891592Smarkm__RCSID("$NetBSD: scan.l,v 1.26 2002/01/31 22:30:21 tv Exp $"); 3912099Sjoerg#endif 4091592Smarkm__FBSDID("$FreeBSD$"); 4112099Sjoerg 4212099Sjoerg#include <stdlib.h> 4312099Sjoerg#include <string.h> 4412099Sjoerg#include <limits.h> 4512099Sjoerg#include <float.h> 4612099Sjoerg#include <ctype.h> 4712099Sjoerg#include <errno.h> 4812099Sjoerg#include <err.h> 4917142Sjkh#include <math.h> 5012099Sjoerg 5112099Sjoerg#include "lint1.h" 5291592Smarkm#include "cgram.h" 5312099Sjoerg 5412099Sjoerg#define CHAR_MASK (~(~0 << CHAR_BIT)) 5512099Sjoerg 5612099Sjoerg/* Current position (its also updated when an included file is parsed) */ 5791592Smarkmpos_t curr_pos = { 1, "", 0 }; 5812099Sjoerg 5912099Sjoerg/* 6012099Sjoerg * Current position in C source (not updated when an included file is 6112099Sjoerg * parsed). 6212099Sjoerg */ 6391592Smarkmpos_t csrc_pos = { 1, "", 0 }; 6412099Sjoerg 6591592Smarkmstatic void incline(void); 6691592Smarkmstatic void badchar(int); 6791592Smarkmstatic sbuf_t *allocsb(void); 6891592Smarkmstatic void freesb(sbuf_t *); 6991592Smarkmstatic int inpc(void); 7091592Smarkmstatic int hash(const char *); 7191592Smarkmstatic sym_t *search(sbuf_t *); 7291592Smarkmstatic int name(void); 7391592Smarkmstatic int keyw(sym_t *); 7491592Smarkmstatic int icon(int); 7591592Smarkmstatic int fcon(void); 7691592Smarkmstatic int operator(int, op_t); 7791592Smarkmstatic int ccon(void); 7891592Smarkmstatic int wccon(void); 7991592Smarkmstatic int getescc(int); 8091592Smarkmstatic void directive(void); 8191592Smarkmstatic void comment(void); 8291592Smarkmstatic void slashslashcomment(void); 8391592Smarkmstatic int string(void); 8491592Smarkmstatic int wcstrg(void); 8512099Sjoerg 8612099Sjoerg%} 8712099Sjoerg 88250227Sjkim%option nounput 89250227Sjkim 9012099SjoergL [_A-Za-z] 9112099SjoergD [0-9] 9212099SjoergNZD [1-9] 9312099SjoergOD [0-7] 9412099SjoergHD [0-9A-Fa-f] 9512099SjoergEX ([eE][+-]?[0-9]+) 9612099Sjoerg 9712099Sjoerg%% 9812099Sjoerg 9912099Sjoerg{L}({L}|{D})* return (name()); 10012099Sjoerg0{OD}*[lLuU]* return (icon(8)); 10112099Sjoerg{NZD}{D}*[lLuU]* return (icon(10)); 10212099Sjoerg0[xX]{HD}+[lLuU]* return (icon(16)); 10312099Sjoerg{D}+\.{D}*{EX}?[fFlL]? | 10412099Sjoerg{D}+{EX}[fFlL]? | 10512099Sjoerg\.{D}+{EX}?[fFlL]? return (fcon()); 10612099Sjoerg"=" return (operator(T_ASSIGN, ASSIGN)); 10712099Sjoerg"*=" return (operator(T_OPASS, MULASS)); 10812099Sjoerg"/=" return (operator(T_OPASS, DIVASS)); 10912099Sjoerg"%=" return (operator(T_OPASS, MODASS)); 11012099Sjoerg"+=" return (operator(T_OPASS, ADDASS)); 11112099Sjoerg"-=" return (operator(T_OPASS, SUBASS)); 11212099Sjoerg"<<=" return (operator(T_OPASS, SHLASS)); 11312099Sjoerg">>=" return (operator(T_OPASS, SHRASS)); 11412099Sjoerg"&=" return (operator(T_OPASS, ANDASS)); 11512099Sjoerg"^=" return (operator(T_OPASS, XORASS)); 11612099Sjoerg"|=" return (operator(T_OPASS, ORASS)); 11712099Sjoerg"||" return (operator(T_LOGOR, LOGOR)); 11812099Sjoerg"&&" return (operator(T_LOGAND, LOGAND)); 11912099Sjoerg"|" return (operator(T_OR, OR)); 12012099Sjoerg"&" return (operator(T_AND, AND)); 12112099Sjoerg"^" return (operator(T_XOR, XOR)); 12212099Sjoerg"==" return (operator(T_EQOP, EQ)); 12312099Sjoerg"!=" return (operator(T_EQOP, NE)); 12412099Sjoerg"<" return (operator(T_RELOP, LT)); 12512099Sjoerg">" return (operator(T_RELOP, GT)); 12612099Sjoerg"<=" return (operator(T_RELOP, LE)); 12712099Sjoerg">=" return (operator(T_RELOP, GE)); 12812099Sjoerg"<<" return (operator(T_SHFTOP, SHL)); 12912099Sjoerg">>" return (operator(T_SHFTOP, SHR)); 13012099Sjoerg"++" return (operator(T_INCDEC, INC)); 13112099Sjoerg"--" return (operator(T_INCDEC, DEC)); 13212099Sjoerg"->" return (operator(T_STROP, ARROW)); 13312099Sjoerg"." return (operator(T_STROP, POINT)); 13412099Sjoerg"+" return (operator(T_ADDOP, PLUS)); 13512099Sjoerg"-" return (operator(T_ADDOP, MINUS)); 13612099Sjoerg"*" return (operator(T_MULT, MULT)); 13712099Sjoerg"/" return (operator(T_DIVOP, DIV)); 13812099Sjoerg"%" return (operator(T_DIVOP, MOD)); 13912099Sjoerg"!" return (operator(T_UNOP, NOT)); 14012099Sjoerg"~" return (operator(T_UNOP, COMPL)); 14112099Sjoerg"\"" return (string()); 14212099Sjoerg"L\"" return (wcstrg()); 14312099Sjoerg";" return (T_SEMI); 14412099Sjoerg"{" return (T_LBRACE); 14512099Sjoerg"}" return (T_RBRACE); 14612099Sjoerg"," return (T_COMMA); 14712099Sjoerg":" return (T_COLON); 14812099Sjoerg"?" return (T_QUEST); 14912099Sjoerg"[" return (T_LBRACK); 15012099Sjoerg"]" return (T_RBRACK); 15112099Sjoerg"(" return (T_LPARN); 15212099Sjoerg")" return (T_RPARN); 15312099Sjoerg"..." return (T_ELLIPSE); 15412099Sjoerg"'" return (ccon()); 15512099Sjoerg"L'" return (wccon()); 15612099Sjoerg^#.*$ directive(); 15712099Sjoerg\n incline(); 15812099Sjoerg\t|" "|\f|\v ; 15912099Sjoerg"/*" comment(); 16091592Smarkm"//" slashslashcomment(); 16112099Sjoerg. badchar(yytext[0]); 16212099Sjoerg 16312099Sjoerg%% 16412099Sjoerg 16512099Sjoergstatic void 16691592Smarkmincline(void) 16712099Sjoerg{ 16812099Sjoerg curr_pos.p_line++; 16991592Smarkm curr_pos.p_uniq = 0; 17091592Smarkm if (curr_pos.p_file == csrc_pos.p_file) { 17112099Sjoerg csrc_pos.p_line++; 17291592Smarkm csrc_pos.p_uniq = 0; 17391592Smarkm } 17412099Sjoerg} 17512099Sjoerg 17612099Sjoergstatic void 17791592Smarkmbadchar(int c) 17812099Sjoerg{ 17991592Smarkm 18012099Sjoerg /* unknown character \%o */ 18112099Sjoerg error(250, c); 18212099Sjoerg} 18312099Sjoerg 18412099Sjoerg/* 18512099Sjoerg * Keywords. 18612099Sjoerg * During initialisation they are written to the symbol table. 18712099Sjoerg */ 18812099Sjoergstatic struct kwtab { 18912099Sjoerg const char *kw_name; /* keyword */ 19012099Sjoerg int kw_token; /* token returned by yylex() */ 19112099Sjoerg scl_t kw_scl; /* storage class if kw_token T_SCLASS */ 19212099Sjoerg tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */ 19312099Sjoerg tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */ 19412099Sjoerg u_int kw_stdc : 1; /* STDC keyword */ 19512099Sjoerg u_int kw_gcc : 1; /* GCC keyword */ 19612099Sjoerg} kwtab[] = { 19712099Sjoerg { "asm", T_ASM, 0, 0, 0, 0, 1 }, 19812099Sjoerg { "__asm", T_ASM, 0, 0, 0, 0, 0 }, 19912099Sjoerg { "__asm__", T_ASM, 0, 0, 0, 0, 0 }, 20012099Sjoerg { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 }, 20112099Sjoerg { "break", T_BREAK, 0, 0, 0, 0, 0 }, 20212099Sjoerg { "case", T_CASE, 0, 0, 0, 0, 0 }, 20312099Sjoerg { "char", T_TYPE, 0, CHAR, 0, 0, 0 }, 20412099Sjoerg { "const", T_QUAL, 0, 0, CONST, 1, 0 }, 20512099Sjoerg { "__const__", T_QUAL, 0, 0, CONST, 0, 0 }, 20612099Sjoerg { "__const", T_QUAL, 0, 0, CONST, 0, 0 }, 20712099Sjoerg { "continue", T_CONTINUE, 0, 0, 0, 0, 0 }, 20812099Sjoerg { "default", T_DEFAULT, 0, 0, 0, 0, 0 }, 20912099Sjoerg { "do", T_DO, 0, 0, 0, 0, 0 }, 21012099Sjoerg { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 }, 21112099Sjoerg { "else", T_ELSE, 0, 0, 0, 0, 0 }, 21212099Sjoerg { "enum", T_ENUM, 0, 0, 0, 0, 0 }, 21312099Sjoerg { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 }, 21412099Sjoerg { "float", T_TYPE, 0, FLOAT, 0, 0, 0 }, 21512099Sjoerg { "for", T_FOR, 0, 0, 0, 0, 0 }, 21612099Sjoerg { "goto", T_GOTO, 0, 0, 0, 0, 0 }, 21712099Sjoerg { "if", T_IF, 0, 0, 0, 0, 0 }, 21812099Sjoerg { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 }, 21912099Sjoerg { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 }, 22012099Sjoerg { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 }, 22112099Sjoerg { "int", T_TYPE, 0, INT, 0, 0, 0 }, 22291592Smarkm { "__symbolrename", T_SYMBOLRENAME, 0, 0, 0, 0, 0 }, 22312099Sjoerg { "long", T_TYPE, 0, LONG, 0, 0, 0 }, 22412099Sjoerg { "register", T_SCLASS, REG, 0, 0, 0, 0 }, 22512099Sjoerg { "return", T_RETURN, 0, 0, 0, 0, 0 }, 22612099Sjoerg { "short", T_TYPE, 0, SHORT, 0, 0, 0 }, 22712099Sjoerg { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 }, 22812099Sjoerg { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 }, 22912099Sjoerg { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 }, 23012099Sjoerg { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 }, 23112099Sjoerg { "static", T_SCLASS, STATIC, 0, 0, 0, 0 }, 23212099Sjoerg { "struct", T_SOU, 0, STRUCT, 0, 0, 0 }, 23312099Sjoerg { "switch", T_SWITCH, 0, 0, 0, 0, 0 }, 23412099Sjoerg { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 }, 23512099Sjoerg { "union", T_SOU, 0, UNION, 0, 0, 0 }, 23612099Sjoerg { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 }, 23712099Sjoerg { "void", T_TYPE, 0, VOID, 0, 0, 0 }, 23812099Sjoerg { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 }, 23912099Sjoerg { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 24012099Sjoerg { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 24112099Sjoerg { "while", T_WHILE, 0, 0, 0, 0, 0 }, 24212099Sjoerg { NULL, 0, 0, 0, 0, 0, 0 } 24312099Sjoerg}; 24412099Sjoerg 24512099Sjoerg/* Symbol table */ 24612099Sjoergstatic sym_t *symtab[HSHSIZ1]; 24712099Sjoerg 24812099Sjoerg/* bit i of the entry with index i is set */ 24991592Smarkmuint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT]; 25012099Sjoerg 25112099Sjoerg/* least significant i bits are set in the entry with index i */ 25291592Smarkmuint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1]; 25312099Sjoerg 25412099Sjoerg/* least significant i bits are not set in the entry with index i */ 25591592Smarkmuint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1]; 25612099Sjoerg 25712099Sjoerg/* free list for sbuf structures */ 25812099Sjoergstatic sbuf_t *sbfrlst; 25912099Sjoerg 26012099Sjoerg/* Typ of next expected symbol */ 26112099Sjoergsymt_t symtyp; 26212099Sjoerg 26312099Sjoerg 26412099Sjoerg/* 26512099Sjoerg * All keywords are written to the symbol table. This saves us looking 266108532Sschweikh * in an extra table for each name we found. 26712099Sjoerg */ 26812099Sjoergvoid 26991592Smarkminitscan(void) 27012099Sjoerg{ 27112099Sjoerg struct kwtab *kw; 27212099Sjoerg sym_t *sym; 27312099Sjoerg int h, i; 27491592Smarkm uint64_t uq; 27512099Sjoerg 27612099Sjoerg for (kw = kwtab; kw->kw_name != NULL; kw++) { 27712099Sjoerg if (kw->kw_stdc && tflag) 27812099Sjoerg continue; 27912099Sjoerg if (kw->kw_gcc && !gflag) 28012099Sjoerg continue; 28112099Sjoerg sym = getblk(sizeof (sym_t)); 28212099Sjoerg sym->s_name = kw->kw_name; 28312099Sjoerg sym->s_keyw = 1; 28412099Sjoerg sym->s_value.v_quad = kw->kw_token; 28512099Sjoerg if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) { 28612099Sjoerg sym->s_tspec = kw->kw_tspec; 28712099Sjoerg } else if (kw->kw_token == T_SCLASS) { 28812099Sjoerg sym->s_scl = kw->kw_scl; 28912099Sjoerg } else if (kw->kw_token == T_QUAL) { 29012099Sjoerg sym->s_tqual = kw->kw_tqual; 29112099Sjoerg } 29212099Sjoerg h = hash(sym->s_name); 29312099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 29412099Sjoerg symtab[h]->s_rlink = &sym->s_link; 29512099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 29612099Sjoerg } 29712099Sjoerg 29812099Sjoerg /* initialize bit-masks for quads */ 29991592Smarkm for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) { 30091592Smarkm qbmasks[i] = (uint64_t)1 << i; 30191592Smarkm uq = ~(uint64_t)0 << i; 30212099Sjoerg qumasks[i] = uq; 30312099Sjoerg qlmasks[i] = ~uq; 30412099Sjoerg } 30512099Sjoerg qumasks[i] = 0; 30691592Smarkm qlmasks[i] = ~(uint64_t)0; 30712099Sjoerg} 30812099Sjoerg 30912099Sjoerg/* 31012099Sjoerg * Get a free sbuf structure, if possible from the free list 31112099Sjoerg */ 31212099Sjoergstatic sbuf_t * 31391592Smarkmallocsb(void) 31412099Sjoerg{ 31512099Sjoerg sbuf_t *sb; 31612099Sjoerg 31712099Sjoerg if ((sb = sbfrlst) != NULL) { 31812099Sjoerg sbfrlst = sb->sb_nxt; 31912099Sjoerg } else { 32080284Sobrien if ((sb = malloc(sizeof (sbuf_t))) == NULL) 32180284Sobrien nomem(); 32212099Sjoerg } 323204872Sjh (void)memset(sb, 0, sizeof (*sb)); 32412099Sjoerg return (sb); 32512099Sjoerg} 32612099Sjoerg 32712099Sjoerg/* 32812099Sjoerg * Put a sbuf structure to the free list 32912099Sjoerg */ 33012099Sjoergstatic void 33191592Smarkmfreesb(sbuf_t *sb) 33212099Sjoerg{ 33391592Smarkm 33412099Sjoerg sb->sb_nxt = sbfrlst; 33512099Sjoerg sbfrlst = sb; 33612099Sjoerg} 33712099Sjoerg 33812099Sjoerg/* 33912099Sjoerg * Read a character and ensure that it is positive (except EOF). 34012099Sjoerg * Increment line count(s) if necessary. 34112099Sjoerg */ 34212099Sjoergstatic int 34391592Smarkminpc(void) 34412099Sjoerg{ 34512099Sjoerg int c; 34612099Sjoerg 34712099Sjoerg if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n') 34812099Sjoerg incline(); 34912099Sjoerg return (c); 35012099Sjoerg} 35112099Sjoerg 35212099Sjoergstatic int 35391592Smarkmhash(const char *s) 35412099Sjoerg{ 35512099Sjoerg u_int v; 35612099Sjoerg const u_char *us; 35712099Sjoerg 35812099Sjoerg v = 0; 35912099Sjoerg for (us = (const u_char *)s; *us != '\0'; us++) { 36012099Sjoerg v = (v << sizeof (v)) + *us; 36112099Sjoerg v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 36212099Sjoerg } 36312099Sjoerg return (v % HSHSIZ1); 36412099Sjoerg} 36512099Sjoerg 36612099Sjoerg/* 36712099Sjoerg * Lex has found a letter followed by zero or more letters or digits. 36812099Sjoerg * It looks for a symbol in the symbol table with the same name. This 36912099Sjoerg * symbol must either be a keyword or a symbol of the type required by 37012099Sjoerg * symtyp (label, member, tag, ...). 37112099Sjoerg * 37212099Sjoerg * If it is a keyword, the token is returned. In some cases it is described 37312099Sjoerg * more deeply by data written to yylval. 37412099Sjoerg * 37512099Sjoerg * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct 37612099Sjoerg * is stored in yylval. This struct contains the name of the symbol, it's 37712099Sjoerg * length and hash value. If there is already a symbol of the same name 37812099Sjoerg * and type in the symbol table, the sbuf struct also contains a pointer 37912099Sjoerg * to the symbol table entry. 38012099Sjoerg */ 38112099Sjoergstatic int 38291592Smarkmname(void) 38312099Sjoerg{ 38412099Sjoerg char *s; 38512099Sjoerg sbuf_t *sb; 38612099Sjoerg sym_t *sym; 38712099Sjoerg int tok; 38812099Sjoerg 38912099Sjoerg sb = allocsb(); 39012099Sjoerg sb->sb_name = yytext; 39112099Sjoerg sb->sb_len = yyleng; 39212099Sjoerg sb->sb_hash = hash(yytext); 39312099Sjoerg 39412099Sjoerg if ((sym = search(sb)) != NULL && sym->s_keyw) { 39512099Sjoerg freesb(sb); 39612099Sjoerg return (keyw(sym)); 39712099Sjoerg } 39812099Sjoerg 39912099Sjoerg sb->sb_sym = sym; 40012099Sjoerg 40112099Sjoerg if (sym != NULL) { 40212099Sjoerg if (blklev < sym->s_blklev) 40312099Sjoerg lerror("name() 1"); 40412099Sjoerg sb->sb_name = sym->s_name; 40512099Sjoerg sb->sb_len = strlen(sym->s_name); 40612099Sjoerg tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 40712099Sjoerg } else { 40812099Sjoerg s = getblk(yyleng + 1); 40912099Sjoerg (void)memcpy(s, yytext, yyleng + 1); 41012099Sjoerg sb->sb_name = s; 41112099Sjoerg sb->sb_len = yyleng; 41212099Sjoerg tok = T_NAME; 41312099Sjoerg } 41412099Sjoerg 41512099Sjoerg yylval.y_sb = sb; 41612099Sjoerg return (tok); 41712099Sjoerg} 41812099Sjoerg 41912099Sjoergstatic sym_t * 42091592Smarkmsearch(sbuf_t *sb) 42112099Sjoerg{ 42212099Sjoerg sym_t *sym; 42312099Sjoerg 42412099Sjoerg for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) { 42512099Sjoerg if (strcmp(sym->s_name, sb->sb_name) == 0) { 42612099Sjoerg if (sym->s_keyw || sym->s_kind == symtyp) 42712099Sjoerg return (sym); 42812099Sjoerg } 42912099Sjoerg } 43012099Sjoerg 43112099Sjoerg return (NULL); 43212099Sjoerg} 43391592Smarkm 43412099Sjoergstatic int 43591592Smarkmkeyw(sym_t *sym) 43612099Sjoerg{ 43712099Sjoerg int t; 43812099Sjoerg 43912099Sjoerg if ((t = (int)sym->s_value.v_quad) == T_SCLASS) { 44012099Sjoerg yylval.y_scl = sym->s_scl; 44112099Sjoerg } else if (t == T_TYPE || t == T_SOU) { 44212099Sjoerg yylval.y_tspec = sym->s_tspec; 44312099Sjoerg } else if (t == T_QUAL) { 44412099Sjoerg yylval.y_tqual = sym->s_tqual; 44512099Sjoerg } 44612099Sjoerg return (t); 44712099Sjoerg} 44812099Sjoerg 44912099Sjoerg/* 45012099Sjoerg * Convert a string representing an integer into internal representation. 45112099Sjoerg * The value is returned in yylval. icon() (and yylex()) returns T_CON. 45212099Sjoerg */ 45312099Sjoergstatic int 45491592Smarkmicon(int base) 45512099Sjoerg{ 45612099Sjoerg int l_suffix, u_suffix; 45712099Sjoerg int len; 45812099Sjoerg const char *cp; 45912099Sjoerg char c, *eptr; 46012099Sjoerg tspec_t typ; 46191592Smarkm u_long ul = 0; 46291592Smarkm uint64_t uq = 0; 46312099Sjoerg int ansiu; 46412099Sjoerg static tspec_t contypes[2][3] = { 46512099Sjoerg { INT, LONG, QUAD }, 46612099Sjoerg { UINT, ULONG, UQUAD } 46712099Sjoerg }; 46812099Sjoerg 46912099Sjoerg cp = yytext; 47012099Sjoerg len = yyleng; 47112099Sjoerg 47212099Sjoerg /* skip 0x */ 47312099Sjoerg if (base == 16) { 47412099Sjoerg cp += 2; 47512099Sjoerg len -= 2; 47612099Sjoerg } 47712099Sjoerg 47812099Sjoerg /* read suffixes */ 47912099Sjoerg l_suffix = u_suffix = 0; 48012099Sjoerg for ( ; ; ) { 48112099Sjoerg if ((c = cp[len - 1]) == 'l' || c == 'L') { 48212099Sjoerg l_suffix++; 48312099Sjoerg } else if (c == 'u' || c == 'U') { 48412099Sjoerg u_suffix++; 48512099Sjoerg } else { 48612099Sjoerg break; 48712099Sjoerg } 48812099Sjoerg len--; 48912099Sjoerg } 49012099Sjoerg if (l_suffix > 2 || u_suffix > 1) { 49112099Sjoerg /* malformed integer constant */ 49212099Sjoerg warning(251); 49312099Sjoerg if (l_suffix > 2) 49412099Sjoerg l_suffix = 2; 49512099Sjoerg if (u_suffix > 1) 49612099Sjoerg u_suffix = 1; 49712099Sjoerg } 49812099Sjoerg if (tflag && u_suffix != 0) { 49912099Sjoerg /* suffix U is illegal in traditional C */ 50012099Sjoerg warning(97); 50112099Sjoerg } 50212099Sjoerg typ = contypes[u_suffix][l_suffix]; 50312099Sjoerg 50412099Sjoerg errno = 0; 50512099Sjoerg if (l_suffix < 2) { 50612099Sjoerg ul = strtoul(cp, &eptr, base); 50712099Sjoerg } else { 50812099Sjoerg uq = strtouq(cp, &eptr, base); 50912099Sjoerg } 51012099Sjoerg if (eptr != cp + len) 51112099Sjoerg lerror("icon() 1"); 51212099Sjoerg if (errno != 0) 51312099Sjoerg /* integer constant out of range */ 51412099Sjoerg warning(252); 51512099Sjoerg 51612099Sjoerg /* 51791592Smarkm * If the value is to big for the current type, we must choose 51812099Sjoerg * another type. 51912099Sjoerg */ 52012099Sjoerg ansiu = 0; 52112099Sjoerg switch (typ) { 52212099Sjoerg case INT: 52312099Sjoerg if (ul <= INT_MAX) { 52412099Sjoerg /* ok */ 52512099Sjoerg } else if (ul <= (unsigned)UINT_MAX && base != 10) { 52612099Sjoerg typ = UINT; 52712099Sjoerg } else if (ul <= LONG_MAX) { 52812099Sjoerg typ = LONG; 52912099Sjoerg } else { 53012099Sjoerg typ = ULONG; 53112099Sjoerg } 53212099Sjoerg if (typ == UINT || typ == ULONG) { 53312099Sjoerg if (tflag) { 53412099Sjoerg typ = LONG; 53512099Sjoerg } else if (!sflag) { 53612099Sjoerg /* 53712099Sjoerg * Remember that the constant is unsigned 53812099Sjoerg * only in ANSI C 53912099Sjoerg */ 54012099Sjoerg ansiu = 1; 54112099Sjoerg } 54212099Sjoerg } 54312099Sjoerg break; 54412099Sjoerg case UINT: 54512099Sjoerg if (ul > (u_int)UINT_MAX) 54612099Sjoerg typ = ULONG; 54712099Sjoerg break; 54812099Sjoerg case LONG: 54912099Sjoerg if (ul > LONG_MAX && !tflag) { 55012099Sjoerg typ = ULONG; 55112099Sjoerg if (!sflag) 55212099Sjoerg ansiu = 1; 55312099Sjoerg } 55412099Sjoerg break; 55512099Sjoerg case QUAD: 55612099Sjoerg if (uq > QUAD_MAX && !tflag) { 55712099Sjoerg typ = UQUAD; 55812099Sjoerg if (!sflag) 55912099Sjoerg ansiu = 1; 56012099Sjoerg } 56112099Sjoerg break; 56212099Sjoerg /* LINTED (enumeration values not handled in switch) */ 56391592Smarkm case STRUCT: 56491592Smarkm case VOID: 56591592Smarkm case LDOUBLE: 56691592Smarkm case FUNC: 56791592Smarkm case ARRAY: 56891592Smarkm case PTR: 56991592Smarkm case ENUM: 57091592Smarkm case UNION: 57191592Smarkm case SIGNED: 57291592Smarkm case NOTSPEC: 57391592Smarkm case DOUBLE: 57491592Smarkm case FLOAT: 57591592Smarkm case UQUAD: 57691592Smarkm case ULONG: 57791592Smarkm case USHORT: 57891592Smarkm case SHORT: 57991592Smarkm case UCHAR: 58091592Smarkm case SCHAR: 58191592Smarkm case CHAR: 58291592Smarkm case UNSIGN: 58391592Smarkm break; 58412099Sjoerg } 58512099Sjoerg 58612099Sjoerg if (typ != QUAD && typ != UQUAD) { 58712099Sjoerg if (isutyp(typ)) { 58812099Sjoerg uq = ul; 58912099Sjoerg } else { 59091592Smarkm uq = (int64_t)(long)ul; 59112099Sjoerg } 59212099Sjoerg } 59312099Sjoerg 59491592Smarkm uq = (uint64_t)xsign((int64_t)uq, typ, -1); 59512099Sjoerg 59680284Sobrien if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL) 59780284Sobrien nomem(); 59880284Sobrien yylval.y_val->v_tspec = typ; 59912099Sjoerg yylval.y_val->v_ansiu = ansiu; 60091592Smarkm yylval.y_val->v_quad = (int64_t)uq; 60112099Sjoerg 60212099Sjoerg return (T_CON); 60312099Sjoerg} 60412099Sjoerg 60512099Sjoerg/* 60612099Sjoerg * Returns 1 if t is a signed type and the value is negative. 60712099Sjoerg * 60812099Sjoerg * len is the number of significant bits. If len is -1, len is set 60912099Sjoerg * to the width of type t. 61012099Sjoerg */ 61112099Sjoergint 61291592Smarkmsign(int64_t q, tspec_t t, int len) 61312099Sjoerg{ 61491592Smarkm 61512099Sjoerg if (t == PTR || isutyp(t)) 61612099Sjoerg return (0); 61712099Sjoerg return (msb(q, t, len)); 61812099Sjoerg} 61912099Sjoerg 62012099Sjoergint 62191592Smarkmmsb(int64_t q, tspec_t t, int len) 62212099Sjoerg{ 62391592Smarkm 62412099Sjoerg if (len <= 0) 62512099Sjoerg len = size(t); 62612099Sjoerg return ((q & qbmasks[len - 1]) != 0); 62712099Sjoerg} 62812099Sjoerg 62912099Sjoerg/* 63012099Sjoerg * Extends the sign of q. 63112099Sjoerg */ 63291592Smarkmint64_t 63391592Smarkmxsign(int64_t q, tspec_t t, int len) 63412099Sjoerg{ 63591592Smarkm 63612099Sjoerg if (len <= 0) 63712099Sjoerg len = size(t); 63812099Sjoerg 63912099Sjoerg if (t == PTR || isutyp(t) || !sign(q, t, len)) { 64012099Sjoerg q &= qlmasks[len]; 64112099Sjoerg } else { 64212099Sjoerg q |= qumasks[len]; 64312099Sjoerg } 64412099Sjoerg return (q); 64512099Sjoerg} 64612099Sjoerg 64712099Sjoerg/* 64812099Sjoerg * Convert a string representing a floating point value into its interal 64912099Sjoerg * representation. Type and value are returned in yylval. fcon() 65012099Sjoerg * (and yylex()) returns T_CON. 65112099Sjoerg * XXX Currently it is not possible to convert constants of type 65291592Smarkm * long double which are greater than DBL_MAX. 65312099Sjoerg */ 65412099Sjoergstatic int 65591592Smarkmfcon(void) 65612099Sjoerg{ 65712099Sjoerg const char *cp; 65812099Sjoerg int len; 65912099Sjoerg tspec_t typ; 66012099Sjoerg char c, *eptr; 66112099Sjoerg double d; 66291592Smarkm float f = 0; 66312099Sjoerg 66412099Sjoerg cp = yytext; 66512099Sjoerg len = yyleng; 66612099Sjoerg 66712099Sjoerg if ((c = cp[len - 1]) == 'f' || c == 'F') { 66812099Sjoerg typ = FLOAT; 66912099Sjoerg len--; 67012099Sjoerg } else if (c == 'l' || c == 'L') { 67112099Sjoerg typ = LDOUBLE; 67212099Sjoerg len--; 67312099Sjoerg } else { 67412099Sjoerg typ = DOUBLE; 67512099Sjoerg } 67612099Sjoerg 67712099Sjoerg if (tflag && typ != DOUBLE) { 67812099Sjoerg /* suffixes F and L are illegal in traditional C */ 67912099Sjoerg warning(98); 68012099Sjoerg } 68112099Sjoerg 68212099Sjoerg errno = 0; 68312099Sjoerg d = strtod(cp, &eptr); 68412099Sjoerg if (eptr != cp + len) 68512099Sjoerg lerror("fcon() 1"); 68612099Sjoerg if (errno != 0) 68712099Sjoerg /* floating-point constant out of range */ 68812099Sjoerg warning(248); 68912099Sjoerg 69012099Sjoerg if (typ == FLOAT) { 69112099Sjoerg f = (float)d; 69291592Smarkm if (!finite(f)) { 69312099Sjoerg /* floating-point constant out of range */ 69412099Sjoerg warning(248); 69512099Sjoerg f = f > 0 ? FLT_MAX : -FLT_MAX; 69612099Sjoerg } 69712099Sjoerg } 69812099Sjoerg 69980284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 70080284Sobrien nomem(); 70180284Sobrien yylval.y_val->v_tspec = typ; 70212099Sjoerg if (typ == FLOAT) { 70312099Sjoerg yylval.y_val->v_ldbl = f; 70412099Sjoerg } else { 70512099Sjoerg yylval.y_val->v_ldbl = d; 70612099Sjoerg } 70712099Sjoerg 70812099Sjoerg return (T_CON); 70912099Sjoerg} 71012099Sjoerg 71112099Sjoergstatic int 71291592Smarkmoperator(int t, op_t o) 71312099Sjoerg{ 71491592Smarkm 71512099Sjoerg yylval.y_op = o; 71612099Sjoerg return (t); 71712099Sjoerg} 71812099Sjoerg 71912099Sjoerg/* 72012099Sjoerg * Called if lex found a leading \'. 72112099Sjoerg */ 72212099Sjoergstatic int 72391592Smarkmccon(void) 72412099Sjoerg{ 72512099Sjoerg int n, val, c; 72612099Sjoerg char cv; 72712099Sjoerg 72812099Sjoerg n = 0; 72912099Sjoerg val = 0; 73012099Sjoerg while ((c = getescc('\'')) >= 0) { 73112099Sjoerg val = (val << CHAR_BIT) + c; 73212099Sjoerg n++; 73312099Sjoerg } 73412099Sjoerg if (c == -2) { 73512099Sjoerg /* unterminated character constant */ 73612099Sjoerg error(253); 73712099Sjoerg } else { 73812099Sjoerg if (n > sizeof (int) || (n > 1 && (pflag || hflag))) { 73912099Sjoerg /* too many characters in character constant */ 74012099Sjoerg error(71); 74112099Sjoerg } else if (n > 1) { 74212099Sjoerg /* multi-character character constant */ 74312099Sjoerg warning(294); 74412099Sjoerg } else if (n == 0) { 74512099Sjoerg /* empty character constant */ 74612099Sjoerg error(73); 74712099Sjoerg } 74812099Sjoerg } 74912099Sjoerg if (n == 1) { 75012099Sjoerg cv = (char)val; 75112099Sjoerg val = cv; 75212099Sjoerg } 75391592Smarkm 75491592Smarkm yylval.y_val = xcalloc(1, sizeof (val_t)); 75512099Sjoerg yylval.y_val->v_tspec = INT; 75612099Sjoerg yylval.y_val->v_quad = val; 75712099Sjoerg 75812099Sjoerg return (T_CON); 75912099Sjoerg} 76012099Sjoerg 76112099Sjoerg/* 76212099Sjoerg * Called if lex found a leading L\' 76312099Sjoerg */ 76412099Sjoergstatic int 76591592Smarkmwccon(void) 76612099Sjoerg{ 76712099Sjoerg static char buf[MB_LEN_MAX + 1]; 76812099Sjoerg int i, c; 76912099Sjoerg wchar_t wc; 77012099Sjoerg 77112099Sjoerg i = 0; 77212099Sjoerg while ((c = getescc('\'')) >= 0) { 77312099Sjoerg if (i < MB_CUR_MAX) 77412099Sjoerg buf[i] = (char)c; 77512099Sjoerg i++; 77612099Sjoerg } 77712099Sjoerg 77812099Sjoerg wc = 0; 77912099Sjoerg 78012099Sjoerg if (c == -2) { 78112099Sjoerg /* unterminated character constant */ 78212099Sjoerg error(253); 78312099Sjoerg } else if (c == 0) { 78412099Sjoerg /* empty character constant */ 78512099Sjoerg error(73); 78612099Sjoerg } else { 78712099Sjoerg if (i > MB_CUR_MAX) { 78812099Sjoerg i = MB_CUR_MAX; 78912099Sjoerg /* too many characters in character constant */ 79012099Sjoerg error(71); 79112099Sjoerg } else { 79212099Sjoerg buf[i] = '\0'; 79312099Sjoerg (void)mbtowc(NULL, NULL, 0); 79412099Sjoerg if (mbtowc(&wc, buf, MB_CUR_MAX) < 0) 79512099Sjoerg /* invalid multibyte character */ 79612099Sjoerg error(291); 79712099Sjoerg } 79812099Sjoerg } 79912099Sjoerg 80080284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 80180284Sobrien nomem(); 80212099Sjoerg yylval.y_val->v_tspec = WCHAR; 80312099Sjoerg yylval.y_val->v_quad = wc; 80412099Sjoerg 80512099Sjoerg return (T_CON); 80612099Sjoerg} 80712099Sjoerg 80812099Sjoerg/* 80912099Sjoerg * Read a character which is part of a character constant or of a string 81012099Sjoerg * and handle escapes. 81112099Sjoerg * 81212099Sjoerg * The Argument is the character which delimits the character constant or 81312099Sjoerg * string. 81412099Sjoerg * 81512099Sjoerg * Returns -1 if the end of the character constant or string is reached, 81691592Smarkm * -2 if the EOF is reached, and the character otherwise. 81712099Sjoerg */ 81812099Sjoergstatic int 81991592Smarkmgetescc(int d) 82012099Sjoerg{ 82112099Sjoerg static int pbc = -1; 82212099Sjoerg int n, c, v; 82312099Sjoerg 82412099Sjoerg if (pbc == -1) { 82512099Sjoerg c = inpc(); 82612099Sjoerg } else { 82712099Sjoerg c = pbc; 82812099Sjoerg pbc = -1; 82912099Sjoerg } 83012099Sjoerg if (c == d) 83112099Sjoerg return (-1); 83212099Sjoerg switch (c) { 83312099Sjoerg case '\n': 83491592Smarkm if (tflag) { 83591592Smarkm /* newline in string or char constant */ 83691592Smarkm error(254); 83791592Smarkm return (-2); 83891592Smarkm } 83991592Smarkm return (c); 84012099Sjoerg case EOF: 84112099Sjoerg return (-2); 84212099Sjoerg case '\\': 84312099Sjoerg switch (c = inpc()) { 84412099Sjoerg case '"': 84512099Sjoerg if (tflag && d == '\'') 84612099Sjoerg /* \" inside character constant undef. ... */ 84712099Sjoerg warning(262); 84812099Sjoerg return ('"'); 84912099Sjoerg case '\'': 85012099Sjoerg return ('\''); 85112099Sjoerg case '?': 85212099Sjoerg if (tflag) 85312099Sjoerg /* \? undefined in traditional C */ 85412099Sjoerg warning(263); 85512099Sjoerg return ('?'); 85612099Sjoerg case '\\': 85712099Sjoerg return ('\\'); 85812099Sjoerg case 'a': 85912099Sjoerg if (tflag) 86012099Sjoerg /* \a undefined in traditional C */ 86112099Sjoerg warning(81); 86212099Sjoerg return ('\a'); 86312099Sjoerg case 'b': 86412099Sjoerg return ('\b'); 86512099Sjoerg case 'f': 86612099Sjoerg return ('\f'); 86712099Sjoerg case 'n': 86812099Sjoerg return ('\n'); 86912099Sjoerg case 'r': 87012099Sjoerg return ('\r'); 87112099Sjoerg case 't': 87212099Sjoerg return ('\t'); 87312099Sjoerg case 'v': 87412099Sjoerg if (tflag) 87512099Sjoerg /* \v undefined in traditional C */ 87612099Sjoerg warning(264); 87712099Sjoerg return ('\v'); 87812099Sjoerg case '8': case '9': 87912099Sjoerg /* bad octal digit %c */ 88012099Sjoerg warning(77, c); 88112099Sjoerg /* FALLTHROUGH */ 88212099Sjoerg case '0': case '1': case '2': case '3': 88312099Sjoerg case '4': case '5': case '6': case '7': 88412099Sjoerg n = 3; 88512099Sjoerg v = 0; 88612099Sjoerg do { 88712099Sjoerg v = (v << 3) + (c - '0'); 88812099Sjoerg c = inpc(); 88912099Sjoerg } while (--n && isdigit(c) && (tflag || c <= '7')); 89012099Sjoerg if (tflag && n > 0 && isdigit(c)) 89112099Sjoerg /* bad octal digit %c */ 89212099Sjoerg warning(77, c); 89312099Sjoerg pbc = c; 89412099Sjoerg if (v > UCHAR_MAX) { 89512099Sjoerg /* character escape does not fit in char. */ 89612099Sjoerg warning(76); 89712099Sjoerg v &= CHAR_MASK; 89812099Sjoerg } 89912099Sjoerg return (v); 90012099Sjoerg case 'x': 90112099Sjoerg if (tflag) 90212099Sjoerg /* \x undefined in traditional C */ 90312099Sjoerg warning(82); 90412099Sjoerg v = 0; 90512099Sjoerg n = 0; 90612099Sjoerg while ((c = inpc()) >= 0 && isxdigit(c)) { 90712099Sjoerg c = isdigit(c) ? 90812099Sjoerg c - '0' : toupper(c) - 'A' + 10; 90912099Sjoerg v = (v << 4) + c; 91012099Sjoerg if (n >= 0) { 91112099Sjoerg if ((v & ~CHAR_MASK) != 0) { 91212099Sjoerg /* overflow in hex escape */ 91312099Sjoerg warning(75); 91412099Sjoerg n = -1; 91512099Sjoerg } else { 91612099Sjoerg n++; 91712099Sjoerg } 91812099Sjoerg } 91912099Sjoerg } 92012099Sjoerg pbc = c; 92112099Sjoerg if (n == 0) { 92212099Sjoerg /* no hex digits follow \x */ 92312099Sjoerg error(74); 92412099Sjoerg } if (n == -1) { 92512099Sjoerg v &= CHAR_MASK; 92612099Sjoerg } 92712099Sjoerg return (v); 92812099Sjoerg case '\n': 92912099Sjoerg return (getescc(d)); 93012099Sjoerg case EOF: 93112099Sjoerg return (-2); 93212099Sjoerg default: 93312099Sjoerg if (isprint(c)) { 93412099Sjoerg /* dubious escape \%c */ 93512099Sjoerg warning(79, c); 93612099Sjoerg } else { 93712099Sjoerg /* dubious escape \%o */ 93812099Sjoerg warning(80, c); 93912099Sjoerg } 94012099Sjoerg } 94112099Sjoerg } 94212099Sjoerg return (c); 94312099Sjoerg} 94412099Sjoerg 94512099Sjoerg/* 94612099Sjoerg * Called for preprocessor directives. Currently implemented are: 94712099Sjoerg * # lineno 94812099Sjoerg * # lineno "filename" 94912099Sjoerg */ 95012099Sjoergstatic void 95191592Smarkmdirective(void) 95212099Sjoerg{ 95312099Sjoerg const char *cp, *fn; 95412099Sjoerg char c, *eptr; 95512099Sjoerg size_t fnl; 95612099Sjoerg long ln; 95712099Sjoerg static int first = 1; 95812099Sjoerg 95912099Sjoerg /* Go to first non-whitespace after # */ 96091592Smarkm for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) 96191592Smarkm continue; 96212099Sjoerg 96391592Smarkm if (!isdigit((unsigned char)c)) { 96412099Sjoerg error: 96512099Sjoerg /* undefined or invalid # directive */ 96612099Sjoerg warning(255); 96712099Sjoerg return; 96812099Sjoerg } 96912099Sjoerg ln = strtol(--cp, &eptr, 10); 97012099Sjoerg if (cp == eptr) 97112099Sjoerg goto error; 97212099Sjoerg if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0') 97312099Sjoerg goto error; 97491592Smarkm while ((c = *cp++) == ' ' || c == '\t') 97591592Smarkm continue; 97612099Sjoerg if (c != '\0') { 97712099Sjoerg if (c != '"') 97812099Sjoerg goto error; 97912099Sjoerg fn = cp; 98012099Sjoerg while ((c = *cp) != '"' && c != '\0') 98112099Sjoerg cp++; 98212099Sjoerg if (c != '"') 98312099Sjoerg goto error; 98412099Sjoerg if ((fnl = cp++ - fn) > PATH_MAX) 98512099Sjoerg goto error; 98691592Smarkm while ((c = *cp++) == ' ' || c == '\t') 98791592Smarkm continue; 98812099Sjoerg#if 0 98912099Sjoerg if (c != '\0') 99012099Sjoerg warning("extra character(s) after directive"); 99112099Sjoerg#endif 99291592Smarkm 99391592Smarkm /* empty string means stdin */ 99491592Smarkm if (fnl == 0) { 99591592Smarkm fn = "{standard input}"; 99691592Smarkm fnl = 16; /* strlen (fn) */ 99791592Smarkm } 99812099Sjoerg curr_pos.p_file = fnnalloc(fn, fnl); 99912099Sjoerg /* 100012099Sjoerg * If this is the first directive, the name is the name 100112099Sjoerg * of the C source file as specified at the command line. 100212099Sjoerg * It is written to the output file. 100312099Sjoerg */ 100412099Sjoerg if (first) { 100512099Sjoerg csrc_pos.p_file = curr_pos.p_file; 100612099Sjoerg outsrc(curr_pos.p_file); 100712099Sjoerg first = 0; 100812099Sjoerg } 100912099Sjoerg } 101012099Sjoerg curr_pos.p_line = (int)ln - 1; 101191592Smarkm curr_pos.p_uniq = 0; 101291592Smarkm if (curr_pos.p_file == csrc_pos.p_file) { 101312099Sjoerg csrc_pos.p_line = (int)ln - 1; 101491592Smarkm csrc_pos.p_uniq = 0; 101591592Smarkm } 101612099Sjoerg} 101712099Sjoerg 101812099Sjoerg/* 101912099Sjoerg * Handle lint comments. Following comments are currently understood: 102012099Sjoerg * ARGSUSEDn 102191592Smarkm * BITFIELDTYPE 102212099Sjoerg * CONSTCOND CONSTANTCOND CONSTANTCONDITION 102312099Sjoerg * FALLTHRU FALLTHROUGH 102412099Sjoerg * LINTLIBRARY 102512099Sjoerg * LINTED NOSTRICT 102612099Sjoerg * LONGLONG 102712099Sjoerg * NOTREACHED 102812099Sjoerg * PRINTFLIKEn 102912099Sjoerg * PROTOLIB 103012099Sjoerg * SCANFLIKEn 103112099Sjoerg * VARARGSn 103212099Sjoerg * If one of this comments is recognized, the arguments, if any, are 103312099Sjoerg * parsed and a function which handles this comment is called. 103412099Sjoerg */ 103512099Sjoergstatic void 103691592Smarkmcomment(void) 103712099Sjoerg{ 103812099Sjoerg int c, lc; 103912099Sjoerg static struct { 104012099Sjoerg const char *keywd; 104112099Sjoerg int arg; 104291592Smarkm void (*func)(int); 104312099Sjoerg } keywtab[] = { 104412099Sjoerg { "ARGSUSED", 1, argsused }, 104591592Smarkm { "BITFIELDTYPE", 0, bitfieldtype }, 104612099Sjoerg { "CONSTCOND", 0, constcond }, 104712099Sjoerg { "CONSTANTCOND", 0, constcond }, 104812099Sjoerg { "CONSTANTCONDITION", 0, constcond }, 104912099Sjoerg { "FALLTHRU", 0, fallthru }, 105012099Sjoerg { "FALLTHROUGH", 0, fallthru }, 105112099Sjoerg { "LINTLIBRARY", 0, lintlib }, 105212099Sjoerg { "LINTED", 0, linted }, 105312099Sjoerg { "LONGLONG", 0, longlong }, 105412099Sjoerg { "NOSTRICT", 0, linted }, 105512099Sjoerg { "NOTREACHED", 0, notreach }, 105612099Sjoerg { "PRINTFLIKE", 1, printflike }, 105712099Sjoerg { "PROTOLIB", 1, protolib }, 105812099Sjoerg { "SCANFLIKE", 1, scanflike }, 105912099Sjoerg { "VARARGS", 1, varargs }, 106012099Sjoerg }; 106112099Sjoerg char keywd[32]; 106212099Sjoerg char arg[32]; 106312099Sjoerg int l, i, a; 106412099Sjoerg int eoc; 106512099Sjoerg 106612099Sjoerg eoc = 0; 106712099Sjoerg 106812099Sjoerg /* Skip white spaces after the start of the comment */ 106991592Smarkm while ((c = inpc()) != EOF && isspace(c)) 107091592Smarkm continue; 107112099Sjoerg 107212099Sjoerg /* Read the potential keyword to keywd */ 107312099Sjoerg l = 0; 107412099Sjoerg while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) { 107512099Sjoerg keywd[l++] = (char)c; 107612099Sjoerg c = inpc(); 107712099Sjoerg } 107812099Sjoerg keywd[l] = '\0'; 107912099Sjoerg 108012099Sjoerg /* look for the keyword */ 108112099Sjoerg for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) { 108212099Sjoerg if (strcmp(keywtab[i].keywd, keywd) == 0) 108312099Sjoerg break; 108412099Sjoerg } 108512099Sjoerg if (i == sizeof (keywtab) / sizeof (keywtab[0])) 108612099Sjoerg goto skip_rest; 108712099Sjoerg 108812099Sjoerg /* skip white spaces after the keyword */ 108912099Sjoerg while (c != EOF && isspace(c)) 109012099Sjoerg c = inpc(); 109112099Sjoerg 109212099Sjoerg /* read the argument, if the keyword accepts one and there is one */ 109312099Sjoerg l = 0; 109412099Sjoerg if (keywtab[i].arg) { 109512099Sjoerg while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) { 109612099Sjoerg arg[l++] = (char)c; 109712099Sjoerg c = inpc(); 109812099Sjoerg } 109912099Sjoerg } 110012099Sjoerg arg[l] = '\0'; 110112099Sjoerg a = l != 0 ? atoi(arg) : -1; 110212099Sjoerg 110312099Sjoerg /* skip white spaces after the argument */ 110412099Sjoerg while (c != EOF && isspace(c)) 110512099Sjoerg c = inpc(); 110612099Sjoerg 110712099Sjoerg if (c != '*' || (c = inpc()) != '/') { 110812099Sjoerg if (keywtab[i].func != linted) 110912099Sjoerg /* extra characters in lint comment */ 111012099Sjoerg warning(257); 111112099Sjoerg } else { 111212099Sjoerg /* 111312099Sjoerg * remember that we have already found the end of the 111412099Sjoerg * comment 111512099Sjoerg */ 111612099Sjoerg eoc = 1; 111712099Sjoerg } 111812099Sjoerg 111912099Sjoerg if (keywtab[i].func != NULL) 112012099Sjoerg (*keywtab[i].func)(a); 112112099Sjoerg 112212099Sjoerg skip_rest: 112312099Sjoerg while (!eoc) { 112412099Sjoerg lc = c; 112512099Sjoerg if ((c = inpc()) == EOF) { 112612099Sjoerg /* unterminated comment */ 112712099Sjoerg error(256); 112812099Sjoerg break; 112912099Sjoerg } 113012099Sjoerg if (lc == '*' && c == '/') 113112099Sjoerg eoc = 1; 113212099Sjoerg } 113312099Sjoerg} 113412099Sjoerg 113512099Sjoerg/* 113691592Smarkm * Handle // style comments 113791592Smarkm */ 113891592Smarkmstatic void 113991592Smarkmslashslashcomment(void) 114091592Smarkm{ 114191592Smarkm int c; 114291592Smarkm 114391592Smarkm if (sflag < 2 && !gflag) 114491592Smarkm /* // comments only supported in C99 */ 114591592Smarkm (void)gnuism(312, tflag ? "traditional" : "ANSI"); 114691592Smarkm 114791592Smarkm while ((c = inpc()) != EOF && c != '\n') 114891592Smarkm continue; 114991592Smarkm} 115091592Smarkm 115191592Smarkm/* 115212099Sjoerg * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND. 115312099Sjoerg * clrwflgs() is called after function definitions and global and 115412099Sjoerg * local declarations and definitions. It is also called between 115512099Sjoerg * the controlling expression and the body of control statements 115612099Sjoerg * (if, switch, for, while). 115712099Sjoerg */ 115812099Sjoergvoid 115991592Smarkmclrwflgs(void) 116012099Sjoerg{ 116191592Smarkm 116212099Sjoerg nowarn = 0; 116312099Sjoerg quadflg = 0; 116412099Sjoerg ccflg = 0; 116512099Sjoerg} 116612099Sjoerg 116712099Sjoerg/* 116812099Sjoerg * Strings are stored in a dynamically alloceted buffer and passed 116912099Sjoerg * in yylval.y_xstrg to the parser. The parser or the routines called 117012099Sjoerg * by the parser are responsible for freeing this buffer. 117112099Sjoerg */ 117212099Sjoergstatic int 117391592Smarkmstring(void) 117412099Sjoerg{ 117512099Sjoerg u_char *s; 117612099Sjoerg int c; 117712099Sjoerg size_t len, max; 117812099Sjoerg strg_t *strg; 117912099Sjoerg 118080284Sobrien if ((s = malloc(max = 64)) == NULL) 118180284Sobrien nomem(); 118212099Sjoerg 118312099Sjoerg len = 0; 118412099Sjoerg while ((c = getescc('"')) >= 0) { 118512099Sjoerg /* +1 to reserve space for a trailing NUL character */ 118612099Sjoerg if (len + 1 == max) 118780284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 118880284Sobrien nomem(); 118912099Sjoerg s[len++] = (char)c; 119012099Sjoerg } 119112099Sjoerg s[len] = '\0'; 119212099Sjoerg if (c == -2) 119312099Sjoerg /* unterminated string constant */ 119412099Sjoerg error(258); 119512099Sjoerg 119680284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 119780284Sobrien nomem(); 119812099Sjoerg strg->st_tspec = CHAR; 119912099Sjoerg strg->st_len = len; 120012099Sjoerg strg->st_cp = s; 120112099Sjoerg 120212099Sjoerg yylval.y_strg = strg; 120312099Sjoerg return (T_STRING); 120412099Sjoerg} 120512099Sjoerg 120612099Sjoergstatic int 120791592Smarkmwcstrg(void) 120812099Sjoerg{ 120912099Sjoerg char *s; 121012099Sjoerg int c, i, n, wi; 121112099Sjoerg size_t len, max, wlen; 121212099Sjoerg wchar_t *ws; 121312099Sjoerg strg_t *strg; 121412099Sjoerg 121580284Sobrien if ((s = malloc(max = 64)) == NULL) 121680284Sobrien nomem(); 121712099Sjoerg len = 0; 121812099Sjoerg while ((c = getescc('"')) >= 0) { 121912099Sjoerg /* +1 to save space for a trailing NUL character */ 122012099Sjoerg if (len + 1 >= max) 122180284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 122280284Sobrien nomem(); 122312099Sjoerg s[len++] = (char)c; 122412099Sjoerg } 122512099Sjoerg s[len] = '\0'; 122612099Sjoerg if (c == -2) 122712099Sjoerg /* unterminated string constant */ 122812099Sjoerg error(258); 122912099Sjoerg 123012099Sjoerg /* get length of wide character string */ 123112099Sjoerg (void)mblen(NULL, 0); 123212099Sjoerg for (i = 0, wlen = 0; i < len; i += n, wlen++) { 123312099Sjoerg if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) { 123412099Sjoerg /* invalid multibyte character */ 123512099Sjoerg error(291); 123612099Sjoerg break; 123712099Sjoerg } 123812099Sjoerg if (n == 0) 123912099Sjoerg n = 1; 124012099Sjoerg } 124112099Sjoerg 124280284Sobrien if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL) 124380284Sobrien nomem(); 124412099Sjoerg 124512099Sjoerg /* convert from multibyte to wide char */ 124612099Sjoerg (void)mbtowc(NULL, NULL, 0); 124712099Sjoerg for (i = 0, wi = 0; i < len; i += n, wi++) { 124812099Sjoerg if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1) 124912099Sjoerg break; 125012099Sjoerg if (n == 0) 125112099Sjoerg n = 1; 125212099Sjoerg } 125312099Sjoerg ws[wi] = 0; 125412099Sjoerg free(s); 125512099Sjoerg 125680284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 125780284Sobrien nomem(); 125812099Sjoerg strg->st_tspec = WCHAR; 125912099Sjoerg strg->st_len = wlen; 126012099Sjoerg strg->st_wcp = ws; 126112099Sjoerg 126212099Sjoerg yylval.y_strg = strg; 126312099Sjoerg return (T_STRING); 126412099Sjoerg} 126512099Sjoerg 126612099Sjoerg/* 126712099Sjoerg * As noted above the scanner does not create new symbol table entries 126812099Sjoerg * for symbols it cannot find in the symbol table. This is to avoid 126912099Sjoerg * putting undeclared symbols into the symbol table if a syntax error 127012099Sjoerg * occurs. 127112099Sjoerg * 127212099Sjoerg * getsym() is called as soon as it is probably ok to put the symbol to 127312099Sjoerg * the symbol table. This does not mean that it is not possible that 127412099Sjoerg * symbols are put to the symbol table which are than not completely 127512099Sjoerg * declared due to syntax errors. To avoid too many problems in this 127612099Sjoerg * case symbols get type int in getsym(). 127712099Sjoerg * 127812099Sjoerg * XXX calls to getsym() should be delayed until decl1*() is called 127912099Sjoerg */ 128012099Sjoergsym_t * 128191592Smarkmgetsym(sbuf_t *sb) 128212099Sjoerg{ 128312099Sjoerg dinfo_t *di; 128412099Sjoerg char *s; 128512099Sjoerg sym_t *sym; 128612099Sjoerg 128712099Sjoerg sym = sb->sb_sym; 128812099Sjoerg 128912099Sjoerg /* 129012099Sjoerg * During member declaration it is possible that name() looked 129112099Sjoerg * for symbols of type FVFT, although it should have looked for 129212099Sjoerg * symbols of type FTAG. Same can happen for labels. Both cases 129312099Sjoerg * are compensated here. 129412099Sjoerg */ 129512099Sjoerg if (symtyp == FMOS || symtyp == FLAB) { 129612099Sjoerg if (sym == NULL || sym->s_kind == FVFT) 129712099Sjoerg sym = search(sb); 129812099Sjoerg } 129912099Sjoerg 130012099Sjoerg if (sym != NULL) { 130112099Sjoerg if (sym->s_kind != symtyp) 130212099Sjoerg lerror("storesym() 1"); 130312099Sjoerg symtyp = FVFT; 130412099Sjoerg freesb(sb); 130512099Sjoerg return (sym); 130612099Sjoerg } 130712099Sjoerg 130812099Sjoerg /* create a new symbol table entry */ 130912099Sjoerg 131012099Sjoerg /* labels must always be allocated at level 1 (outhermost block) */ 131112099Sjoerg if (symtyp == FLAB) { 131212099Sjoerg sym = getlblk(1, sizeof (sym_t)); 131312099Sjoerg s = getlblk(1, sb->sb_len + 1); 131412099Sjoerg (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 131512099Sjoerg sym->s_name = s; 131612099Sjoerg sym->s_blklev = 1; 131712099Sjoerg di = dcs; 131812099Sjoerg while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL) 131912099Sjoerg di = di->d_nxt; 132012099Sjoerg if (di->d_ctx != AUTO) 132112099Sjoerg lerror("storesym() 2"); 132212099Sjoerg } else { 132312099Sjoerg sym = getblk(sizeof (sym_t)); 132412099Sjoerg sym->s_name = sb->sb_name; 132512099Sjoerg sym->s_blklev = blklev; 132612099Sjoerg di = dcs; 132712099Sjoerg } 132812099Sjoerg 132991592Smarkm UNIQUE_CURR_POS(sym->s_dpos); 133012099Sjoerg if ((sym->s_kind = symtyp) != FLAB) 133112099Sjoerg sym->s_type = gettyp(INT); 133212099Sjoerg 133312099Sjoerg symtyp = FVFT; 133412099Sjoerg 133512099Sjoerg if ((sym->s_link = symtab[sb->sb_hash]) != NULL) 133612099Sjoerg symtab[sb->sb_hash]->s_rlink = &sym->s_link; 133712099Sjoerg (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash]; 133812099Sjoerg 133912099Sjoerg *di->d_ldlsym = sym; 134012099Sjoerg di->d_ldlsym = &sym->s_dlnxt; 134112099Sjoerg 134212099Sjoerg freesb(sb); 134312099Sjoerg return (sym); 134412099Sjoerg} 134512099Sjoerg 134612099Sjoerg/* 134712099Sjoerg * Remove a symbol forever from the symbol table. s_blklev 134812099Sjoerg * is set to -1 to avoid that the symbol will later be put 134912099Sjoerg * back to the symbol table. 135012099Sjoerg */ 135112099Sjoergvoid 135291592Smarkmrmsym(sym_t *sym) 135312099Sjoerg{ 135491592Smarkm 135512099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 135612099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 135712099Sjoerg sym->s_blklev = -1; 135812099Sjoerg sym->s_link = NULL; 135912099Sjoerg} 136012099Sjoerg 136112099Sjoerg/* 136212099Sjoerg * Remove a list of symbols declared at one level from the symbol 136312099Sjoerg * table. 136412099Sjoerg */ 136512099Sjoergvoid 136691592Smarkmrmsyms(sym_t *syms) 136712099Sjoerg{ 136812099Sjoerg sym_t *sym; 136912099Sjoerg 137012099Sjoerg for (sym = syms; sym != NULL; sym = sym->s_dlnxt) { 137112099Sjoerg if (sym->s_blklev != -1) { 137212099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 137312099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 137412099Sjoerg sym->s_link = NULL; 137512099Sjoerg sym->s_rlink = NULL; 137612099Sjoerg } 137712099Sjoerg } 137812099Sjoerg} 137912099Sjoerg 138012099Sjoerg/* 138112099Sjoerg * Put a symbol into the symbol table 138212099Sjoerg */ 138312099Sjoergvoid 138491592Smarkminssym(int bl, sym_t *sym) 138512099Sjoerg{ 138612099Sjoerg int h; 138712099Sjoerg 138812099Sjoerg h = hash(sym->s_name); 138912099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 139012099Sjoerg symtab[h]->s_rlink = &sym->s_link; 139112099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 139212099Sjoerg sym->s_blklev = bl; 139312099Sjoerg if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev) 139412099Sjoerg lerror("inssym()"); 139512099Sjoerg} 139612099Sjoerg 139712099Sjoerg/* 139812099Sjoerg * Called at level 0 after syntax errors 139912099Sjoerg * Removes all symbols which are not declared at level 0 from the 140012099Sjoerg * symbol table. Also frees all memory which is not associated with 140112099Sjoerg * level 0. 140212099Sjoerg */ 140312099Sjoergvoid 140491592Smarkmcleanup(void) 140512099Sjoerg{ 140612099Sjoerg sym_t *sym, *nsym; 140712099Sjoerg int i; 140812099Sjoerg 140912099Sjoerg for (i = 0; i < HSHSIZ1; i++) { 141012099Sjoerg for (sym = symtab[i]; sym != NULL; sym = nsym) { 141112099Sjoerg nsym = sym->s_link; 141212099Sjoerg if (sym->s_blklev >= 1) { 141312099Sjoerg if ((*sym->s_rlink = nsym) != NULL) 141412099Sjoerg nsym->s_rlink = sym->s_rlink; 141512099Sjoerg } 141612099Sjoerg } 141712099Sjoerg } 141812099Sjoerg 141912099Sjoerg for (i = mblklev; i > 0; i--) 142012099Sjoerg freelblk(i); 142112099Sjoerg} 142212099Sjoerg 142312099Sjoerg/* 142412099Sjoerg * Create a new symbol with the name of an existing symbol. 142512099Sjoerg */ 142612099Sjoergsym_t * 142791592Smarkmpushdown(sym_t *sym) 142812099Sjoerg{ 142912099Sjoerg int h; 143012099Sjoerg sym_t *nsym; 143112099Sjoerg 143212099Sjoerg h = hash(sym->s_name); 143312099Sjoerg nsym = getblk(sizeof (sym_t)); 143412099Sjoerg if (sym->s_blklev > blklev) 143512099Sjoerg lerror("pushdown()"); 143612099Sjoerg nsym->s_name = sym->s_name; 143791592Smarkm UNIQUE_CURR_POS(nsym->s_dpos); 143812099Sjoerg nsym->s_kind = sym->s_kind; 143912099Sjoerg nsym->s_blklev = blklev; 144012099Sjoerg 144112099Sjoerg if ((nsym->s_link = symtab[h]) != NULL) 144212099Sjoerg symtab[h]->s_rlink = &nsym->s_link; 144312099Sjoerg (symtab[h] = nsym)->s_rlink = &symtab[h]; 144412099Sjoerg 144512099Sjoerg *dcs->d_ldlsym = nsym; 144612099Sjoerg dcs->d_ldlsym = &nsym->s_dlnxt; 144712099Sjoerg 144812099Sjoerg return (nsym); 144912099Sjoerg} 145012099Sjoerg 145112099Sjoerg/* 145212099Sjoerg * Free any dynamically allocated memory referenced by 145312099Sjoerg * the value stack or yylval. 145412099Sjoerg * The type of information in yylval is described by tok. 145512099Sjoerg */ 145612099Sjoergvoid 145791592Smarkmfreeyyv(void *sp, int tok) 145812099Sjoerg{ 145912099Sjoerg if (tok == T_NAME || tok == T_TYPENAME) { 146012099Sjoerg sbuf_t *sb = *(sbuf_t **)sp; 146112099Sjoerg freesb(sb); 146212099Sjoerg } else if (tok == T_CON) { 146312099Sjoerg val_t *val = *(val_t **)sp; 146412099Sjoerg free(val); 146512099Sjoerg } else if (tok == T_STRING) { 146612099Sjoerg strg_t *strg = *(strg_t **)sp; 146712099Sjoerg if (strg->st_tspec == CHAR) { 146812099Sjoerg free(strg->st_cp); 146912099Sjoerg } else if (strg->st_tspec == WCHAR) { 147012099Sjoerg free(strg->st_wcp); 147112099Sjoerg } else { 147212099Sjoerg lerror("fryylv() 1"); 147312099Sjoerg } 147412099Sjoerg free(strg); 147591592Smarkm } 147612099Sjoerg} 1477