2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
18 #include <sys/types.h>
25 #include "expression.h"
27 #include "linearize.h"
30 struct token
*skip_to(struct token
*token
, int op
)
32 while (!match_op(token
, op
) && !eof_token(token
))
37 struct token
*expect(struct token
*token
, int op
, const char *where
)
39 if (!match_op(token
, op
)) {
40 static struct token bad_token
;
41 if (token
!= &bad_token
) {
42 bad_token
.next
= token
;
43 warn(token
->pos
, "Expected %s %s", show_special(op
), where
);
44 warn(token
->pos
, "got %s", show_token(token
));
47 return skip_to(token
, op
);
53 unsigned int hexval(unsigned int c
)
61 retval
= c
- 'a' + 10;
64 retval
= c
- 'A' + 10;
71 * Simple allocator for data that doesn't get partially free'd.
72 * The tokenizer and parser allocate a _lot_ of small data structures
73 * (often just two-three bytes for things like small integers),
74 * and since they all depend on each other you can't free them
75 * individually _anyway_. So do something that is very space-
76 * efficient: allocate larger "blobs", and give out individual
77 * small bits and pieces of it with no maintenance overhead.
79 struct allocation_blob
{
80 struct allocation_blob
*next
;
81 unsigned int left
, offset
;
86 #define blob_alloc(size) mmap(NULL, ((size)+4095) & ~4095, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
87 #define blob_free(addr,size) munmap((addr), ((size)+4095) & ~4095)
89 struct allocator_struct
{
91 struct allocation_blob
*blobs
;
92 unsigned int alignment
;
93 unsigned int chunking
;
95 unsigned int allocations
, total_bytes
, useful_bytes
;
98 void drop_all_allocations(struct allocator_struct
*desc
)
100 struct allocation_blob
*blob
= desc
->blobs
;
103 desc
->allocations
= 0;
104 desc
->total_bytes
= 0;
105 desc
->useful_bytes
= 0;
107 struct allocation_blob
*next
= blob
->next
;
108 blob_free(blob
, desc
->chunking
);
113 void *allocate(struct allocator_struct
*desc
, unsigned int size
)
115 unsigned long alignment
= desc
->alignment
;
116 struct allocation_blob
*blob
= desc
->blobs
;
120 desc
->useful_bytes
+= size
;
121 size
= (size
+ alignment
- 1) & ~(alignment
-1);
122 if (!blob
|| blob
->left
< size
) {
123 unsigned int offset
, chunking
= desc
->chunking
;
124 struct allocation_blob
*newblob
= blob_alloc(chunking
);
126 die("out of memory");
127 desc
->total_bytes
+= chunking
;
128 newblob
->next
= blob
;
130 desc
->blobs
= newblob
;
131 offset
= offsetof(struct allocation_blob
, data
);
132 if (alignment
> offset
)
134 blob
->left
= chunking
- offset
;
135 blob
->offset
= offset
- offsetof(struct allocation_blob
, data
);
137 retval
= blob
->data
+ blob
->offset
;
138 blob
->offset
+= size
;
143 static void show_allocations(struct allocator_struct
*x
)
145 fprintf(stderr
, "%s: %d allocations, %d bytes (%d total bytes, "
146 "%6.2f%% usage, %6.2f average size)\n",
147 x
->name
, x
->allocations
, x
->useful_bytes
, x
->total_bytes
,
148 100 * (double) x
->useful_bytes
/ x
->total_bytes
,
149 (double) x
->useful_bytes
/ x
->allocations
);
152 struct allocator_struct ident_allocator
= { "identifiers", NULL
, __alignof__(struct ident
), CHUNK
};
153 struct allocator_struct token_allocator
= { "tokens", NULL
, __alignof__(struct token
), CHUNK
};
154 struct allocator_struct symbol_allocator
= { "symbols", NULL
, __alignof__(struct symbol
), CHUNK
};
155 struct allocator_struct expression_allocator
= { "expressions", NULL
, __alignof__(struct expression
), CHUNK
};
156 struct allocator_struct statement_allocator
= { "statements", NULL
, __alignof__(struct statement
), CHUNK
};
157 struct allocator_struct string_allocator
= { "strings", NULL
, __alignof__(struct statement
), CHUNK
};
158 struct allocator_struct scope_allocator
= { "scopes", NULL
, __alignof__(struct scope
), CHUNK
};
159 struct allocator_struct bytes_allocator
= { "bytes", NULL
, 1, CHUNK
};
160 struct allocator_struct basic_block_allocator
= { "basic_block", NULL
, __alignof__(struct basic_block
), CHUNK
};
161 struct allocator_struct entrypoint_allocator
= { "entrypoint", NULL
, __alignof__(struct entrypoint
), CHUNK
};
162 struct allocator_struct instruction_allocator
= { "instruction", NULL
, __alignof__(struct instruction
), CHUNK
};
163 struct allocator_struct multijmp_allocator
= { "multijmp", NULL
, __alignof__(struct multijmp
), CHUNK
};
164 struct allocator_struct phi_allocator
= { "phi", NULL
, __alignof__(struct phi
), CHUNK
};
165 struct allocator_struct pseudo_allocator
= { "pseudo", NULL
, __alignof__(struct pseudo
), CHUNK
};
167 #define __ALLOCATOR(type, size, x) \
168 type *__alloc_##x(int extra) \
170 return allocate(&x##_allocator, size+extra); \
172 void show_##x##_alloc(void) \
174 show_allocations(&x##_allocator); \
176 void clear_##x##_alloc(void) \
178 drop_all_allocations(&x##_allocator); \
180 #define ALLOCATOR(x) __ALLOCATOR(struct x, sizeof(struct x), x)
182 ALLOCATOR(ident
); ALLOCATOR(token
); ALLOCATOR(symbol
);
183 ALLOCATOR(expression
); ALLOCATOR(statement
); ALLOCATOR(string
);
184 ALLOCATOR(scope
); __ALLOCATOR(void, 0, bytes
);
185 ALLOCATOR(basic_block
); ALLOCATOR(entrypoint
);
186 ALLOCATOR(instruction
);
191 int ptr_list_size(struct ptr_list
*head
)
196 struct ptr_list
*list
= head
;
199 } while ((list
= list
->next
) != head
);
204 void iterate(struct ptr_list
*head
, void (*callback
)(void *, void *, int), void *data
)
206 struct ptr_list
*list
= head
;
207 int flag
= ITERATE_FIRST
;
213 for (i
= 0; i
< list
->nr
; i
++) {
214 if (i
== list
->nr
-1 && list
->next
== head
)
215 flag
|= ITERATE_LAST
;
216 callback(list
->list
[i
], data
, flag
);
220 } while (list
!= head
);
223 void add_ptr_list(struct ptr_list
**listp
, void *ptr
)
225 struct ptr_list
*list
= *listp
;
226 struct ptr_list
*last
;
229 if (!list
|| (nr
= (last
= list
->prev
)->nr
) >= LIST_NODE_NR
) {
230 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
232 die("out of memory for symbol/statement lists");
233 memset(newlist
, 0, sizeof(*newlist
));
235 newlist
->next
= newlist
;
236 newlist
->prev
= newlist
;
239 newlist
->prev
= last
;
240 newlist
->next
= list
;
241 list
->prev
= newlist
;
242 last
->next
= newlist
;
247 last
->list
[nr
] = ptr
;
252 void init_iterator(struct ptr_list
**head
, struct list_iterator
*iterator
, int flags
)
254 iterator
->head
= head
;
256 iterator
->active
= NULL
;
257 iterator
->flags
= flags
;
260 void * next_iterator(struct list_iterator
*iterator
)
262 struct ptr_list
*list
= iterator
->active
;
266 if (*iterator
->head
==NULL
)
269 list
= *iterator
->head
;
271 if (!(iterator
->flags
& ITERATOR_BACKWARDS
)) {
272 iterator
->active
= list
;
273 return list
->list
[0];
277 if (iterator
->flags
& ITERATOR_BACKWARDS
) {
278 index
= iterator
->index
-1;
280 if (list
->prev
== *iterator
->head
)
282 list
= iterator
->active
= list
->prev
;
286 index
= iterator
->index
+ 1;
287 if (index
>= list
->nr
) {
288 if (list
->next
== *iterator
->head
)
290 list
= iterator
->active
= list
->next
;
294 iterator
->index
= index
;
295 return list
->list
[index
];
298 void replace_iterator(struct list_iterator
*iterator
, void* ptr
)
300 struct ptr_list
*list
= iterator
->active
;
302 list
->list
[iterator
->index
] = ptr
;
305 void delete_iterator(struct list_iterator
*iterator
)
307 struct ptr_list
*list
= iterator
->active
;
308 int movsize
= list
->nr
- iterator
->index
- 1;
309 void ** curptr
= list
->list
+iterator
->index
;
312 memmove(curptr
, curptr
+1, movsize
*sizeof curptr
);
315 if (iterator
->flags
& ITERATOR_BACKWARDS
) {
316 if (iterator
->index
+ 1 >= list
->nr
) {
317 iterator
->active
= (list
->next
== *iterator
->head
) ? NULL
: list
->next
;
321 if (--iterator
->index
<0) {
322 iterator
->active
= (list
->prev
== *iterator
->head
) ? NULL
: list
->prev
;
323 iterator
->index
= list
->prev
->nr
;
327 list
->prev
->next
= list
->next
;
328 list
->next
->prev
= list
->prev
;
329 if (list
== *iterator
->head
)
330 *iterator
->head
= (list
->next
== *iterator
->head
) ? NULL
: list
->next
;
335 void init_terminator_iterator(struct instruction
* terminator
, struct terminator_iterator
*iterator
)
337 iterator
->terminator
= terminator
;
341 switch (terminator
->opcode
) {
343 iterator
->branch
= BR_INIT
;
346 init_multijmp_iterator(&terminator
->multijmp_list
, &iterator
->multijmp
, 0);
351 struct basic_block
* next_terminator_bb(struct terminator_iterator
*iterator
)
353 struct instruction
*terminator
= iterator
->terminator
;
357 switch (terminator
->opcode
) {
359 switch(iterator
->branch
) {
361 if (terminator
->bb_true
) {
362 iterator
->branch
= BR_TRUE
;
363 return terminator
->bb_true
;
366 if (terminator
->bb_false
) {
367 iterator
->branch
= BR_FALSE
;
368 return terminator
->bb_false
;
371 iterator
->branch
= BR_END
;
376 struct multijmp
*jmp
= next_multijmp(&iterator
->multijmp
);
377 return jmp
? jmp
->target
: NULL
;
383 void replace_terminator_bb(struct terminator_iterator
*iterator
, struct basic_block
* bb
)
385 struct instruction
*terminator
= iterator
->terminator
;
389 switch (terminator
->opcode
) {
391 switch(iterator
->branch
) {
393 if (terminator
->bb_true
) {
394 terminator
->bb_true
= bb
;
398 if (terminator
->bb_false
) {
399 terminator
->bb_false
= bb
;
406 struct multijmp
*jmp
= (struct multijmp
*) current_iterator(&iterator
->multijmp
);
415 int replace_ptr_list(struct ptr_list
**head
, void *old_ptr
, void *new_ptr
)
418 struct list_iterator iterator
;
421 init_iterator(head
, &iterator
, 0);
422 while ((ptr
=next_iterator(&iterator
))) {
425 replace_iterator(&iterator
, new_ptr
);
427 delete_iterator(&iterator
);
434 void * delete_ptr_list_last(struct ptr_list
**head
)
437 struct ptr_list
*last
, *first
= *head
;
443 ptr
= last
->list
[--last
->nr
];
445 first
->prev
= last
->prev
;
446 last
->prev
->next
= first
;
454 void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
)
457 FOR_EACH_PTR(a
, entry
) {
458 add_ptr_list(b
, entry
);
462 void free_ptr_list(struct ptr_list
**listp
)
464 struct ptr_list
*tmp
, *list
= *listp
;
469 list
->prev
->next
= NULL
;
479 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
481 static char buffer
[512];
484 vsprintf(buffer
, fmt
, args
);
485 name
= input_streams
[pos
.stream
].name
;
487 fprintf(stderr
, "%s: %s:%d:%d: %s\n",
488 type
, name
, pos
.line
, pos
.pos
, buffer
);
491 void warn(struct position pos
, const char * fmt
, ...)
493 static int warnings
= 0;
496 if (warnings
> 100) {
500 fmt
= "too many warnings";
505 do_warn("warning", pos
, fmt
, args
);
510 void error(struct position pos
, const char * fmt
, ...)
514 do_warn("error", pos
, fmt
, args
);
519 void die(const char *fmt
, ...)
522 static char buffer
[512];
525 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
528 fprintf(stderr
, "%s\n", buffer
);
532 unsigned int pre_buffer_size
;
533 unsigned char pre_buffer
[8192];
539 void add_pre_buffer(const char *fmt
, ...)
545 size
= pre_buffer_size
;
546 size
+= vsnprintf(pre_buffer
+ size
,
547 sizeof(pre_buffer
) - size
,
549 pre_buffer_size
= size
;
553 char **handle_switch_D(char *arg
, char **next
)
555 const char *name
= arg
+ 1;
556 const char *value
= "";
562 if (isspace(c
) || c
== '=') {
568 add_pre_buffer("#define %s %s\n", name
, value
);
572 char **handle_switch_E(char *arg
, char **next
)
578 char **handle_switch_v(char *arg
, char **next
)
583 char **handle_switch_I(char *arg
, char **next
)
585 add_pre_buffer("#add_include \"%s/\"\n", arg
+ 1);
589 char **handle_switch_i(char *arg
, char **next
)
591 if (*next
&& !strcmp(arg
, "include")) {
592 char *name
= *++next
;
593 int fd
= open(name
, O_RDONLY
);
603 char **handle_switch_m(char *arg
, char **next
)
605 if (!strcmp(arg
, "m64")) {
607 max_int_alignment
= 8;
608 bits_in_pointer
= 64;
609 pointer_alignment
= 8;
614 char **handle_switch(char *arg
, char **next
)
619 case 'D': rc
= handle_switch_D(arg
, next
); break;
620 case 'E': rc
= handle_switch_E(arg
, next
); break;
621 case 'v': rc
= handle_switch_v(arg
, next
); break;
622 case 'I': rc
= handle_switch_I(arg
, next
); break;
623 case 'i': rc
= handle_switch_i(arg
, next
); break;
624 case 'm': rc
= handle_switch_m(arg
, next
); break;
627 * Ignore unknown command line options:
628 * they're probably gcc switches
635 void create_builtin_stream(void)
637 add_pre_buffer("#define __i386__ 1\n");
638 add_pre_buffer("#define __linux__ 1\n");
639 add_pre_buffer("#define __STDC__ 1\n");
640 add_pre_buffer("#define linux linux\n");
641 add_pre_buffer("#define cond_syscall(x)\n");
642 add_pre_buffer("#define __GNUC__ 2\n");
643 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
644 add_pre_buffer("#define __func__ \"function\"\n");
645 add_pre_buffer("#define __extension__\n");
646 add_pre_buffer("#define __pragma__\n");
647 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
648 add_pre_buffer("#define __builtin_va_arg(arg,type) ((type)0)\n");
649 add_pre_buffer("#define __builtin_va_end(arg)\n");