Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320783
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: 嵌入式

2009-03-22 11:23:46

程序流程图

代码注释



/*
 * Read a line from the top file in istk, handling multiple CR/LFs
 * at the end of the line read, and handling spurious ^Zs. Will
 * return lines from the standard macro set if this has not already
 * been done.
 */

/*
 * 先处理nasm自己定义的宏定义,等宏定义处理完之后在处理
 */

static char *read_line(void)
{
    char *buffer, *p, *q;
    int bufsize, continued_count;

    if (stdmacpos)
    {
        if (*stdmacpos)
        {
            char *ret = nasm_strdup(*stdmacpos++);
            if (!*stdmacpos && any_extrastdmac) /* 判断是否到达stdmacpos最后一个参数,并且是否有额外的宏要处理 */
            {
                stdmacpos = extrastdmac;
                any_extrastdmac = FALSE;
                return ret;
            }
            /*
             * Nasty hack: here we push the contents of `predef' on
             * to the top-level expansion stack, since this is the
             * most convenient way to implement the pre-include and
             * pre-define features.
             */

            if (!*stdmacpos) /* 宏定义是否处理完了 */
            {
                Line *pd, *l;
                Token *head, **tail, *t;

                /* 将 predef(static Line *) 链表复制到一份副本到 istk->expansion,但是其总的l->finishes = FALSE(bug?逻辑上应该是l->finishes = NULL) */
                for (pd = predef; pd; pd = pd->next)
                {
                    head = NULL;
                    tail = &head;
                    for (t = pd->first; t; t = t->next) /* 复制pd->first(Token *)链表 */
                    {
                        *tail = new_Token(NULL, t->type, t->text, 0);
                        tail = &(*tail)->next;
                    }
                    l = nasm_malloc(sizeof(Line));
                    l->next = istk->expansion; /* 插入链表头 */
                    l->first = head;
                    l->finishes = FALSE;
                    istk->expansion = l;
                }
            }
            return ret;
        }
        else
        {
            stdmacpos = NULL;
        }
    }

    bufsize = BUF_DELTA; /* #define BUF_DELTA 512 */
    buffer = nasm_malloc(BUF_DELTA); /* 建立缓冲区 */
    p = buffer;
    continued_count = 0; /* 与处理行号有关 */
    while (1) {
        q = fgets(p, bufsize - (p - buffer), istk->fp); /* 行输入 */
        if (!q)
            break;
        p += strlen(p);
        /* 将通过续行符分开的代码语句合并成单行的一条语句 */
        if (p > buffer && p[-1] == '\n') {
            /* Convert backslash-CRLF line continuation sequences into
               nothing at all (for DOS and Windows) */

            if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
                p -= 3;
                *p = 0;
                continued_count++;
            }
            /* Also convert backslash-LF line continuation sequences into
               nothing at all (for Unix) */

            else if (((p - 1) > buffer) && (p[-2] == '\\')) {
                p -= 2;
                *p = 0;
                continued_count++;
            } else {
                break;
            }
        }
        if (p - buffer > bufsize - 10) { /* 如果缓冲区不充足,则在原来大小的基础上 +BUF_DELTA */
            long offset = p - buffer;
            bufsize += BUF_DELTA;
            buffer = nasm_realloc(buffer, bufsize);
            p = buffer + offset; /* prevent stale-pointer problems */
        }
    }

    if (!q && p == buffer) {
        nasm_free(buffer);
        return NULL;
    }

    src_set_linnum(src_get_linnum() + istk->lineinc + /* 改变行号 */
                   (continued_count * istk->lineinc));

    /*
     * Play safe: remove CRs as well as LFs, if any of either are
     * present at the end of the line.
     */

    while (--p >= buffer && (*p == '\n' || *p == '\r'))
        *p = '\0';

    /*
     * Handle spurious ^Z, which may be inserted into source files
     * by some file transfer utilities.
     */

    buffer[strcspn(buffer, "\032")] = '\0';
    list->line(LIST_READ, buffer); /* listing.c 中实现 */

    return buffer;
}

阅读(1446) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~