错误:搜索内容不能为空,请输入英文关键词
错误:关键词超出字数限制,请精简
高级检索

Functions

  • Torben Ægidius Mogensen

摘要

In Chap.  6 we have shown how to translate the body of a single function. Function calls and returns were left (mostly) untranslated by using the CALL and RETURN instructions in the intermediate code. Nor did we in Chap.  7 show how these instructions should be translated to machine code. We will, in this chapter, remedy these omissions. We will initially assume that all variables are local to the function that accesses them and that parameters are call-by-value, meaning that the value of an argument expression is passed to the called function. This is the default parameter-passing mechanism in most languages, and in many languages (e.g, C or SML) it is the only one. A single procedure body uses (in most languages) a finite number of variables. We have seen in Chap.  8 that we can map these variables into a (possibly smaller) set of registers. A program that uses recursive procedures or functions may, however, use an unbounded number of variables, as each recursive invocation of the function has its own set of variables, and there is no bound on the recursion depth. We can not hope to keep all these variables in registers, so we will use memory for some of these. The basic idea is that only variables that are local to the active (most recently called) function will be kept in registers. All other variables will be kept in memory. When a function is called, all the live variables of the calling function (which we will refer to as the caller) need to be stored in memory so the registers will be free for use by the called function (the callee). When the callee returns, the stored variables are loaded back into registers. We will use a stack for this storing and loading, pushing register contents on the stack when they must be saved and popping them back into registers when they must be restored.