C语言指针原理教程:语法树及代码实现

2019-02-0814:29:19编程语言入门到精通Comments2,675 views字数 3367阅读模式
指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址。要搞清一个指针需要搞清指针的四方面的内容:指针的类型、指针所指向的类型、指针的值或者叫指针所指向的内存区、指针本身所占据的内存区。

tcctok.h定义了C语言的词法分析的基本元素,主要定义了关键字。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

/ keywords /
DEF(TOK_INT, "int")
DEF(TOK_VOID, "void")
DEF(TOK_CHAR, "char")
DEF(TOK_IF, "if")
DEF(TOK_ELSE, "else")
DEF(TOK_WHILE, "while")
DEF(TOK_BREAK, "break")
DEF(TOK_RETURN, "return")
DEF(TOK_FOR, "for")
DEF(TOK_EXTERN, "extern")
DEF(TOK_STATIC, "static")
DEF(TOK_UNSIGNED, "unsigned")
DEF(TOK_GOTO, "goto")
DEF(TOK_DO, "do")
DEF(TOK_CONTINUE, "continue")
DEF(TOK_SWITCH, "switch")
DEF(TOK_CASE, "case")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

..............................文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

..............................文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

..............................文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

同时定义了条件编译的相关内容文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

/*****/
/ the following are not keywords. They are included to ease parsing /
/ preprocessor only /
DEF(TOK_DEFINE, "define")
DEF(TOK_INCLUDE, "include")
DEF(TOK_INCLUDE_NEXT, "include_next")
DEF(TOK_IFDEF, "ifdef")
DEF(TOK_IFNDEF, "ifndef")
DEF(TOK_ELIF, "elif")
DEF(TOK_ENDIF, "endif")
DEF(TOK_DEFINED, "defined")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

在i386-tok.h中定义了汇编的相关关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

/ ------------------------------------------------------------------ /
/ WARNING: relative order of tokens is important. /文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

/ register /
DEF_ASM(al)
DEF_ASM(cl)
DEF_ASM(dl)
DEF_ASM(bl)
DEF_ASM(ah)
DEF_ASM(ch)
DEF_ASM(dh)
DEF_ASM(bh)
DEF_ASM(ax)
DEF_ASM(cx)
DEF_ASM(dx)
DEF_ASM(bx)
DEF_ASM(sp)
DEF_ASM(bp)
DEF_ASM(si)
DEF_ASM(di)
DEF_ASM(eax)
DEF_ASM(ecx)
DEF_ASM(edx)
DEF_ASM(ebx)
DEF_ASM(esp)
DEF_ASM(ebp)
DEF_ASM(esi)
DEF_ASM(edi)
#ifdef TCC_TARGET_X86_64
DEF_ASM(rax)
DEF_ASM(rcx)
DEF_ASM(rdx)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

...........................文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

............................文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

在x86_64-asm.h中定义了64位汇编相关关键字文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

     DEF_ASM_OP0(clc, 0xf8) / must be first OP0 /
DEF_ASM_OP0(cld, 0xfc)
DEF_ASM_OP0(cli, 0xfa)
DEF_ASM_OP0(clts, 0x0f06)
DEF_ASM_OP0(cmc, 0xf5)
DEF_ASM_OP0(lahf, 0x9f)
DEF_ASM_OP0(sahf, 0x9e)
DEF_ASM_OP0(pushfl, 0x9c)
DEF_ASM_OP0(popfl, 0x9d)
DEF_ASM_OP0(pushf, 0x9c)
DEF_ASM_OP0(popf, 0x9d)
DEF_ASM_OP0(stc, 0xf9)
DEF_ASM_OP0(std, 0xfd)
DEF_ASM_OP0(sti, 0xfb)
DEF_ASM_OP0(aaa, 0x37)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

先从几个重要文件入手。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

libtcc.c文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

/
  TCC - Tiny C Compiler
 
  Copyright (c) 2001-2004 Fabrice Bellard
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "tcc.h"

/****/
/ global variables /
使用GCC扩展还是TCC扩展

/ use GNU C extensions /
ST_DATA int gnu_ext = 1;

/ use TinyCC extensions /
ST_DATA int tcc_ext = 1;

ST_DATA 结构标注TCC状态。
/ XXX: get rid of this ASAP /
ST_DATA struct TCCState *tcc_state;

/****/

根据标志包含一些相应TCC文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

#ifdef ONE_SOURCE
#include "tccpp.c"
#include "tccgen.c"
#include "tccelf.c"
#include "tccrun.c"
#ifdef TCC_TARGET_I386
#include "i386-gen.c"
#endif
#ifdef TCC_TARGET_ARM
#include "arm-gen.c"
#endif
#ifdef TCC_TARGET_C67
#include "c67-gen.c"
#endif
#ifdef TCC_TARGET_X86_64
#include "x86_64-gen.c"
#endif
#ifdef CONFIG_TCC_ASM
#include "tccasm.c"
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
#include "i386-asm.c"
#endif
#endif
#ifdef TCC_TARGET_COFF
#include "tcccoff.c"
#endif
#ifdef TCC_TARGET_PE
#include "tccpe.c"
#endif
#endif / ONE_SOURCE /文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

CONFIG_TCC_ASM打开内联汇编的开关
/****/
#ifndef CONFIG_TCC_ASM
ST_FUNC void asm_instr(void)
{
tcc_error("inline asm() not supported");
}
ST_FUNC void asm_global_instr(void)
{
tcc_error("inline asm() not supported");
}
#endif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/9462.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/9462.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定