Monthly Archives: October 2014

preprocess_new () at tccpp.c:3199 tcc的预处理

// 这些函数算是比较基本的了,看看就明白

static inline int is_space(int ch)
{
return ch == ‘ ‘ || ch == ‘\t’ || ch == ‘\v’ || ch == ‘\f’ || ch == ‘\r’;
}
static inline int isid(int c)
{
return (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’) || c == ‘_’;
}
static inline int isnum(int c)
{
return c >= ‘0’ && c <= ‘9’;
}
static inline int isoct(int c)
{
return c >= ‘0’ && c <= ‘7’;
}
static inline int toup(int c)
{
return (c >= ‘a’ && c <= ‘z’) ? c – ‘a’ + ‘A’ : c;
}

preprocess_new () at tccpp.c:3199

初始化isidnum表

/* init isid table */
for(i=CH_EOF;i<256;i++)
isidnum_table[i-CH_EOF] = isid(i) || isnum(i);

// 该函数剩下几行把tcc的关键字插入 ptable (代码如下)

[code]

static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
{
TokenSym *ts, **ptable;
[/code]

[code]

//在头文件tcctok.h定义了tcc的关键字

static const char tcc_keywords[] =
#define DEF(id, str) str “\0”
#include “tcctok.h”
#undef DEF
;
[/code]

ST_FUNC TokenSym *tok_alloc(const char *str, int len)

 

Tcc source code – CONFIG_TCCDIR

In file config.h, it defined CONFIG_TCCDIR

# define CONFIG_TCCDIR “/usr/local/lib/tcc”

Then it executes

tcc_set_lib_path(s, CONFIG_TCCDIR);

[code]

LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
{
tcc_free(s->tcc_lib_path);
s->tcc_lib_path = tcc_strdup(path);
}

[/code]

Tcc 源码分析:第一步编译 (Tcc Source code analysis: First Step Compiling)

Tcc 源码分析:第一步编译
Tcc Source code analysis: First Step Compiling

Tiny C Compiler

  1. http://bellard.org/tcc/ 下载地址(download URL)
  2. 解压缩 tcc (decompress tcc)
  3. 像编译其他开源软件那样编译,tcc 规模较小 ( compiled as other open source, tcc is small size software)
  4. 复制编译好的libtcc1.a到/usr/local/lib/tcc/ (after compiling finished, copy libtcc1.a to /usr/local/lib/tcc)
    cp libtcc1.a /usr/local/lib/tcc

编译直接./configure && make (compile use command ./configure && make)

可以配置-g打开调试配置,以方便后续用gdb调试 (use -g to enable debug for gdb)

./tcc 1.c -o 1 来生成执行文件 (./tcc 1.c -o 1 to generate executable file)

[code]

#include <stdlib.h>

int main(int argc,char **argv)
{
int x = 0;
printf(“%d”,x);
return 0;
}

[/code]