TCC source code — define_stack and tcc_alloc

define_stack is good example of lots of stack using TCC. The following is example of print Define Stack.

1
2
3
4
5
6
7
8
9
10
11
void printDefineStack(){
  Sym *p = define_stack;
  for(;;){
    if(p == NULL){
      break;
    }
 
    printf("%d\n",p -> v);
    p = p -> prev; 
  }
}

tcc_alloc is function which is allocated tcc object, It is very simple and used everywhere in tcc. Here is example on how to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void tcc_alloc_test()
{
  int i = 0;
  int len =  3;
  char *str = "int";
  TokenSym *ts;  
  table_ident = tcc_malloc((TOK_ALLOC_INCR) * sizeof(TokenSym *));
 
  ts = tcc_malloc(sizeof(TokenSym) + len);
  table_ident[i] = ts;
 
  ts->sym_define = NULL;
  ts->sym_label = NULL;
  ts->sym_struct = NULL;
  ts->sym_identifier = NULL;
  ts->len = len;
  ts->hash_next = NULL;
  memcpy(ts->str, str, len);
  ts->str[len] = '\0';
 
}