c/c++语言开发共享c语言处理函数调用的方法

1. 要编译的测试代码:int plus(int x, int y) { return x + y;}int main(void) { return plus(3, 4);}2. main中ret


1. 要编译的测试代码:

  int plus(int x, int y) {    return x + y;  }    int main(void) {    return plus(3, 4);  }

2. main中return语句语法分析

  if (equal(tok, "return")) {      node *node = new_node(nd_return, tok);      if (consume(rest, tok->next, ";"))        return node;        node *exp = expr(&tok, tok->next);      *rest = skip(tok, ";");        node->lhs = exp;      return node;    }

2.1 当前token为return

则创建类型为nd_return的node。

2.2 由于return后面跟着plus(3, 4)

所以调用expr函数解析这个表达式。

2.3 跳过”;”符号。

2.4 将类型为nd_return的node的左表达式设置为类型为nd_cast的node。

3. expr的处理

3.1 解析”plus”

  if (tok->kind == tk_ident) {      varscope *sc = find_var(tok);        return new_var_node(sc->var, tok);    }  static node *new_var_node(obj *var, token *tok) {    node *node = new_node(nd_var, tok);    node->var = var;    return node;  }

  当token类型为tk_ident时,从符号表中找到代表plus函数的node,这个node

是在解析plus函数时创建的,就不详细分析了。

  创建新的类型为nd_var的node,这个node的var域为代表plus函数的node。

  if (equal(tok, "(")) {        node = funcall(&tok, tok->next, node);  }  if (ty->kind != ty_struct && ty->kind != ty_union)    exp = new_cast(exp, current_fn->ty->return_ty);    node->lhs = exp;  return node;

  解析完函数名plus后,继续解析函数参数调用,如果plus后面跟着是”(“,则判断为函数调用,

于是调用funcall函数,这个函数的参数node即为上面创建的类型为nd_var的node。

  创建完类型为nd_funcall的node,又调用new_cast创建类型为nd_cast的节点,这个节点

的左表达式为类型为nd_funcall的node。return的node类型为nd_return,它的左表达式为

nd_cast的node。

3.2 funcall函数

  node head = {};   node *cur = &head;    while (!equal(tok, ")")) {      if (cur != &head)        tok = skip(tok, ",");        node *arg = assign(&tok, tok);if (param_ty) {        if (param_ty->kind != ty_struct && param_ty->kind != ty_union)          arg = new_cast(arg, param_ty);        param_ty = param_ty->next;      } else if (arg->ty->kind == ty_float) {        // if parameter type is omitted (e.g. in "..."), float        // arguments are promoted to double.        arg = new_cast(arg, ty_double);      }        cur = cur->next = arg;  }    *rest = skip(tok, ")");    node *node = new_unary(nd_funcall, fn, tok);  node->func_ty = ty;  node->ty = ty->return_ty; node->args = head.next;    return node;

  在funcall函数中调用assign函数解析”(3,4)”,3被解析为类型为nd_num的node,

  node *new_cast(node *expr, type *ty) {    add_type(expr);      node *node = calloc(1, sizeof(node));    node->kind = nd_cast;    node->tok = expr->tok;    node->lhs = expr;    node->ty = copy_type(ty);    return node;  }

  调用new_cast函数,创建类型为nd_cast的node节点,这个节点的左表达式为代表3的node。

跳过”,”,继续调用assign解析4,4也被解析为nd_num的node,继续调用new_cast,创建类型为

nd_cast的node节点,这个节点的左表达式为代表4的node。

  跳出循环,跳过”)”。

  new_unary函数中创建类型为nd_funcall的node,这个node的左表达式为plus函数的node,

args参数为解析”(3,4)”生成的两个类型为nd_cast的node。

4. 生成汇编语言

  static void gen_stmt(node *node) {  switch (node->kind) {  case nd_return:      if (node->lhs) {        gen_expr(node->lhs);      }   println("  jmp .l.return.%s", current_fn->name);  ...  }

  判断node节点为nd_return,则调用gen_expr处理类型为nd_cast的节点。

  static void gen_expr(node *node) {    switch (node->kind) {    case nd_funcall: {    int stack_args = push_args(node);      gen_expr(node->lhs);    for (node *arg = node->args; arg; arg = arg->next) {     pop(argreg64[gp++]);        println(" mov %%rax, %%r10");      println("  call *%%r10");        println("  add $%d, %%rsp", stack_args * 8);    }    case nd_var:      gen_addr(node);return;      case nd_cast:      gen_expr(node->lhs);      cast(node->lhs->ty, node->ty);      return;  ...    }  ...

4.1 判断为nd_cast类型的node

则调用gen_expr,参数为类型为nd_funcall的node。

4.2 调用push_args函数依次生成汇编语句

  ”mov rax, 4″

  ”push rax”

  ”mov rax, 3″

  ”push rax”

  将4和3压入栈。

4.3 递归调用gen_expr

参数为类型为nd_var的node。

4.4 调用gen_addr函数生成汇编代码”lea rax, plus”

将plus函数地址载入rax寄存器。

4.5 pop语句生成汇编代码”pop rdi”

“pop rsi”,将3弹入rdi寄存器,将4弹入rsi寄存器,

plus函数中从这两个寄存器中读取参数。

4.6 生成汇编代码

  ”mov r10, rax”

  ”call r10″

  ”add rsp, 0″

  将plus函数地址从rax载入r10寄存器,call语句完成调用plus函数,由于没有分配栈空间传递参数

,所以这里不需要修改rsp寄存器的值。

4.7 “jmp .l.return.main”

跳转到main函数的结尾处,实现return功能。

到此这篇关于c语言是如何处理函数调用的?的文章就介绍到这了,更多相关c语言处理函数调用内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享c语言处理函数调用的方法,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/992916.html

(0)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐