How to convert stack-based instruction to register-based instruction?
I am in a task of generating mips instructions for a C-like language from the Compiler Design school work. From Computer Architecture one learns that mips is a register-based instruction set, which explicitly manages 32 or so general purpose registers. As I code all the way from lexical analysis, building AST towards generating real code, I found the register-based instruction quite difficult to think of. As I currently follow the design of the compiler part of Python interpreter, I find its stack-based byte code rather easy to understand and implement. However, the course I am taking requires 3-address intermediate code and finally mips code.
Here comes the reasons I find stack-based code easy while register-based hard:
- Stack-based code basically just pushes operands, pops them and calculate and pushes the result back. No need to worry about register allocation.
- Stack-based code has a stack that can grow and shrink, not like the limited numbers of registers one can use in register-based code.
Here is my questions: given an AST (much like the one in Python) and a symbol table, how can one
- generate 3-address intermediate code, then convert to mips code
- generate stack-based code, then convert to 3-address code, finally to mips code.
Any one working scheme will be fine.
Edit: What I really want to know is how to generate MIPS code in the first place, not to convert from other forms.
compiler-construction mips
add a comment |
I am in a task of generating mips instructions for a C-like language from the Compiler Design school work. From Computer Architecture one learns that mips is a register-based instruction set, which explicitly manages 32 or so general purpose registers. As I code all the way from lexical analysis, building AST towards generating real code, I found the register-based instruction quite difficult to think of. As I currently follow the design of the compiler part of Python interpreter, I find its stack-based byte code rather easy to understand and implement. However, the course I am taking requires 3-address intermediate code and finally mips code.
Here comes the reasons I find stack-based code easy while register-based hard:
- Stack-based code basically just pushes operands, pops them and calculate and pushes the result back. No need to worry about register allocation.
- Stack-based code has a stack that can grow and shrink, not like the limited numbers of registers one can use in register-based code.
Here is my questions: given an AST (much like the one in Python) and a symbol table, how can one
- generate 3-address intermediate code, then convert to mips code
- generate stack-based code, then convert to 3-address code, finally to mips code.
Any one working scheme will be fine.
Edit: What I really want to know is how to generate MIPS code in the first place, not to convert from other forms.
compiler-construction mips
1
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07
add a comment |
I am in a task of generating mips instructions for a C-like language from the Compiler Design school work. From Computer Architecture one learns that mips is a register-based instruction set, which explicitly manages 32 or so general purpose registers. As I code all the way from lexical analysis, building AST towards generating real code, I found the register-based instruction quite difficult to think of. As I currently follow the design of the compiler part of Python interpreter, I find its stack-based byte code rather easy to understand and implement. However, the course I am taking requires 3-address intermediate code and finally mips code.
Here comes the reasons I find stack-based code easy while register-based hard:
- Stack-based code basically just pushes operands, pops them and calculate and pushes the result back. No need to worry about register allocation.
- Stack-based code has a stack that can grow and shrink, not like the limited numbers of registers one can use in register-based code.
Here is my questions: given an AST (much like the one in Python) and a symbol table, how can one
- generate 3-address intermediate code, then convert to mips code
- generate stack-based code, then convert to 3-address code, finally to mips code.
Any one working scheme will be fine.
Edit: What I really want to know is how to generate MIPS code in the first place, not to convert from other forms.
compiler-construction mips
I am in a task of generating mips instructions for a C-like language from the Compiler Design school work. From Computer Architecture one learns that mips is a register-based instruction set, which explicitly manages 32 or so general purpose registers. As I code all the way from lexical analysis, building AST towards generating real code, I found the register-based instruction quite difficult to think of. As I currently follow the design of the compiler part of Python interpreter, I find its stack-based byte code rather easy to understand and implement. However, the course I am taking requires 3-address intermediate code and finally mips code.
Here comes the reasons I find stack-based code easy while register-based hard:
- Stack-based code basically just pushes operands, pops them and calculate and pushes the result back. No need to worry about register allocation.
- Stack-based code has a stack that can grow and shrink, not like the limited numbers of registers one can use in register-based code.
Here is my questions: given an AST (much like the one in Python) and a symbol table, how can one
- generate 3-address intermediate code, then convert to mips code
- generate stack-based code, then convert to 3-address code, finally to mips code.
Any one working scheme will be fine.
Edit: What I really want to know is how to generate MIPS code in the first place, not to convert from other forms.
compiler-construction mips
compiler-construction mips
edited Nov 14 '18 at 15:32
cgsdfc
asked Nov 14 '18 at 2:57
cgsdfccgsdfc
8611
8611
1
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07
add a comment |
1
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07
1
1
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292544%2fhow-to-convert-stack-based-instruction-to-register-based-instruction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292544%2fhow-to-convert-stack-based-instruction-to-register-based-instruction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
First implement a stack management code in assembly in MIPS. Then translate your code directly by using that stack. This will not generate fast code but that's an optimisation problem
– slebetman
Nov 14 '18 at 4:42
you mean emulating stack-based code in mips?
– cgsdfc
Nov 14 '18 at 7:14
No, not emulating. Convert the stack opcodes to native opcode but emulate a stack-based memory system by using a (software implemented) stack as memory. That's what most C compilers did in the early days
– slebetman
Nov 14 '18 at 9:02
From the point of view of MIPS, a stack is nothing more than an array of RAM accessed using LOAD/STORE instructions
– slebetman
Nov 14 '18 at 9:03
I think I get your general idea. First I generate stack oriented IR. Then I write code to translate each of those IR into MIPS equivalents (some wrapper code). The stack managed by my wrapper code is the software stack in your terms, right? Indeed this is a doable way. And in my case optimization is optional, your idea is quite plausible. If you bother writing a formal answer, I will adopt it.
– cgsdfc
Nov 14 '18 at 12:07