/*
* 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;
}
|