1#ifndef LIB_H 2#define LIB_H 3 4#include <stdbool.h> 5#include <stdlib.h> 6#include <stddef.h> 7 8/* 9 * Basic helper routine descriptions for 'sparse'. 10 * 11 * Copyright (C) 2003 Transmeta Corp. 12 * 2003 Linus Torvalds 13 * 2004 Christopher Li 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a copy 16 * of this software and associated documentation files (the "Software"), to deal 17 * in the Software without restriction, including without limitation the rights 18 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 19 * copies of the Software, and to permit persons to whom the Software is 20 * furnished to do so, subject to the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be included in 23 * all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 30 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 31 * THE SOFTWARE. 32 */ 33 34#include "compat.h" 35#include "ptrlist.h" 36#include "utils.h" 37#include "bits.h" 38#include "options.h" 39 40#define DO_STRINGIFY(x) #x 41#define STRINGIFY(x) DO_STRINGIFY(x) 42 43#ifndef ARRAY_SIZE 44#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) 45#endif 46 47#ifdef __gnu_hurd__ 48#define PATH_MAX 4096 // Hurd doesn't define this 49#endif 50 51extern const char *sparse_version; 52 53struct position { 54 unsigned int type:6, 55 stream:14, 56 newline:1, 57 whitespace:1, 58 pos:10; 59 unsigned int line:31, 60 noexpand:1; 61}; 62 63struct ident; 64struct token; 65struct symbol; 66struct statement; 67struct asm_operand; 68struct expression; 69struct basic_block; 70struct entrypoint; 71struct instruction; 72struct multijmp; 73struct pseudo; 74 75DECLARE_PTR_LIST(symbol_list, struct symbol); 76DECLARE_PTR_LIST(statement_list, struct statement); 77DECLARE_PTR_LIST(asm_operand_list, struct asm_operand); 78DECLARE_PTR_LIST(expression_list, struct expression); 79DECLARE_PTR_LIST(basic_block_list, struct basic_block); 80DECLARE_PTR_LIST(instruction_list, struct instruction); 81DECLARE_PTR_LIST(multijmp_list, struct multijmp); 82DECLARE_PTR_LIST(pseudo_list, struct pseudo); 83DECLARE_PTR_LIST(ident_list, struct ident); 84DECLARE_PTR_LIST(string_list, char); 85 86typedef struct pseudo *pseudo_t; 87 88#ifdef __GNUC__ 89#define FORMAT_ATTR(pos) __attribute__ ((__format__ (__printf__, pos, pos+1))) 90#define NORETURN_ATTR __attribute__ ((__noreturn__)) 91#define SENTINEL_ATTR __attribute__ ((__sentinel__)) 92#else 93#define FORMAT_ATTR(pos) 94#define NORETURN_ATTR 95#define SENTINEL_ATTR 96#endif 97 98FORMAT_ATTR(1) NORETURN_ATTR 99extern void die(const char *, ...); 100 101FORMAT_ATTR(2) NORETURN_ATTR 102extern void error_die(struct position, const char *, ...); 103 104extern void info(struct position, const char *, ...) FORMAT_ATTR(2); 105extern void warning(struct position, const char *, ...) FORMAT_ATTR(2); 106extern void sparse_error(struct position, const char *, ...) FORMAT_ATTR(2); 107extern void expression_error(struct expression *, const char *, ...) FORMAT_ATTR(2); 108 109#define ERROR_CURR_PHASE (1 << 0) 110#define ERROR_PREV_PHASE (1 << 1) 111extern int has_error; 112 113 114enum phase { 115 PASS__PARSE, 116 PASS__LINEARIZE, 117 PASS__MEM2REG, 118 PASS__OPTIM, 119 PASS__FINAL, 120}; 121 122#define PASS_PARSE (1UL << PASS__PARSE) 123#define PASS_LINEARIZE (1UL << PASS__LINEARIZE) 124#define PASS_MEM2REG (1UL << PASS__MEM2REG) 125#define PASS_OPTIM (1UL << PASS__OPTIM) 126#define PASS_FINAL (1UL << PASS__FINAL) 127 128 129extern void add_pre_buffer(const char *fmt, ...) FORMAT_ATTR(1); 130extern void predefine(const char *name, int weak, const char *fmt, ...) FORMAT_ATTR(3); 131extern void predefine_strong(const char *name, ...) FORMAT_ATTR(1); 132extern void predefine_weak(const char *name, ...) FORMAT_ATTR(1); 133extern void predefine_nostd(const char *name); 134extern void predefined_macros(void); 135 136 137extern void dump_macro_definitions(void); 138extern struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **files); 139extern struct symbol_list *__sparse(char *filename); 140extern struct symbol_list *sparse_keep_tokens(char *filename); 141extern struct symbol_list *sparse(char *filename); 142extern void report_stats(void); 143 144static inline int symbol_list_size(struct symbol_list *list) 145{ 146 return ptr_list_size((struct ptr_list *)(list)); 147} 148 149static inline int statement_list_size(struct statement_list *list) 150{ 151 return ptr_list_size((struct ptr_list *)(list)); 152} 153 154static inline int expression_list_size(struct expression_list *list) 155{ 156 return ptr_list_size((struct ptr_list *)(list)); 157} 158 159static inline int instruction_list_size(struct instruction_list *list) 160{ 161 return ptr_list_size((struct ptr_list *)(list)); 162} 163 164static inline int pseudo_list_size(struct pseudo_list *list) 165{ 166 return ptr_list_size((struct ptr_list *)(list)); 167} 168 169static inline int bb_list_size(struct basic_block_list *list) 170{ 171 return ptr_list_size((struct ptr_list *)(list)); 172} 173 174static inline void free_instruction_list(struct instruction_list **head) 175{ 176 free_ptr_list(head); 177} 178 179static inline struct instruction * delete_last_instruction(struct instruction_list **head) 180{ 181 return undo_ptr_list_last((struct ptr_list **)head); 182} 183 184static inline struct basic_block *first_basic_block(struct basic_block_list *head) 185{ 186 return first_ptr_list((struct ptr_list *)head); 187} 188static inline struct instruction *last_instruction(struct instruction_list *head) 189{ 190 return last_ptr_list((struct ptr_list *)head); 191} 192 193static inline struct instruction *first_instruction(struct instruction_list *head) 194{ 195 return first_ptr_list((struct ptr_list *)head); 196} 197 198static inline struct expression *first_expression(struct expression_list *head) 199{ 200 return first_ptr_list((struct ptr_list *)head); 201} 202 203static inline pseudo_t first_pseudo(struct pseudo_list *head) 204{ 205 return first_ptr_list((struct ptr_list *)head); 206} 207 208static inline struct symbol *first_symbol(struct symbol_list *head) 209{ 210 return first_ptr_list((struct ptr_list *)head); 211} 212 213static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to) 214{ 215 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to); 216} 217 218static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to) 219{ 220 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to); 221} 222 223static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to) 224{ 225 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to); 226} 227 228static inline void add_symbol(struct symbol_list **list, struct symbol *sym) 229{ 230 add_ptr_list(list, sym); 231} 232 233static inline void add_statement(struct statement_list **list, struct statement *stmt) 234{ 235 add_ptr_list(list, stmt); 236} 237 238static inline void add_expression(struct expression_list **list, struct expression *expr) 239{ 240 add_ptr_list(list, expr); 241} 242 243static inline void add_ident(struct ident_list **list, struct ident *ident) 244{ 245 add_ptr_list(list, ident); 246} 247 248#define hashval(x) ((unsigned long)(x)) 249 250#endif 251