Skip to content

Compiler from Imp to Asm - #59

Merged
Lysxia merged 153 commits into
masterfrom
imp2asm
Mar 2, 2019
Merged

Lysxia merged 153 commits into
masterfrom
imp2asm

Conversation

@gmalecha

Copy link
Copy Markdown
Collaborator

Yannick and I wrote this compiler together. Yannick is working on cleaning up some of the definitions and we'll verify it. There are a few things left to do:

  1. Write a compiler for expressions (this means that we need to come up with a scheme for defining temporaries.
  2. Unifying the effects that the two languages use (e.g. Imp has errors, assembly doesn't, assembly has a heap, Imp doesn't).
  3. Verify the compiler.

Some possible extensions:

  1. I think we could use the same compiler structure to add other control flow, e.g. break, continue, etc.
  2. Track local variables explicitly (similar to the way that LLVM works, though personally I would opt for continuations rather than phi nodes).

This isn't ready to merge yet

@gmalecha gmalecha added the wip Work in Progress label Feb 13, 2019
@YaZko

YaZko commented Feb 13, 2019

Copy link
Copy Markdown
Collaborator
  1. I quickly drafted a very naïve scheme by simply assuming a reserved prefix "local". I think it should do the trick but I'll have to double check.

  2. I removed errors from Imp. They were only used in the C-like evaluator assuming partial maps for the environment. I kept the Imp-like total maps for simplicity. If you feel that it's an oversimplification, we can roll back.
    I kept the heap for now, although the compiler currently allocate everything in the stack. Do we want to be fancier?

  3. I cannot express the theorem right now, I need to parameterize the denotation of Imp rather than fix its effects as is right now.

N.B. : TODO: switch the domain of value from Nat to Z to be able to write factorial and odd/even.

@gmalecha

Copy link
Copy Markdown
Collaborator Author
  1. I quickly drafted a very naïve scheme by simply assuming a reserved prefix "local". I think it should do the trick but I'll have to double check.

This should be fine.

  1. I removed errors from Imp. They were only used in the C-like evaluator assuming partial maps for the environment. I kept the Imp-like total maps for simplicity. If you feel that it's an oversimplification, we can roll back.

Partial maps might be better in both cases but we can have the effect actually raise the error so that errors don't show up in the implementation.

I kept the heap for now, although the compiler currently allocate everything in the stack. Do we want to be fancier?

Either way. Dropping it or keeping it, as long as it is consistent shouldn't matter. Ultimately, it would be nice to have a language with a heap.

  1. I cannot express the theorem right now, I need to parameterize the denotation of Imp rather than fix its effects as is right now.

The other option is to give an effect transformer from ImpEff into a parameterized effect. For example,

Definition to_param {E} {Estate : StateE ~> E} {... } : ImpEff ~> E :=
   fun _ e => match e with ... end.

N.B. : TODO: switch the domain of value from Nat to Z to be able to write factorial and odd/even.

Comment thread examples/Imp2Asm.v Outdated

(* YZ: Ascii.ascii_of_nat is not what we want, unreadable *)
Definition gen_local (n: nat): string :=
"local_" ++ (String (Ascii.ascii_of_nat n) "").

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtLib has a Show instance for nat.

Comment thread examples/Imp2Asm.v Outdated
end.
End fmap_block.

(* CR essentially corresponds to an open (asm) program.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually wondering if we should take this as the primitive representation of asm programs.

Comment thread examples/Imp2Asm.v Outdated
To double check.
*)
Open Scope string_scope.
Fixpoint compileCR (s : stmt) {L} (k : block L) {struct s} : CR L.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the re-associating in this definition has me wondering if we should define some new variant types for LocalOrImport and such.

Comment thread examples/Imp2Asm.v Outdated
c_main is the current entry point.
*)
Record CR {imports : Type} : Type :=
{ c_label : Type (* Internal labels *)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside of this representation is that we can't really do anything interesting with it except for denote it since we can't inspect the c_label type. After we finish this, it might be better to switch labels to nat and make c_blocks be a finite map. This would allow us to implement transformations such as jump-tunneling, block merging, etc.

Comment thread examples/Asm.v Outdated
let p3 := run_env _ p2 empty in
let p4 := run_env _ p3 empty in
p4.
Definition run (p: program) : itree emptyE (env * (memory * unit)) :=

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zdancewic personally, I like this way to write it better. It also has the benefit that type class resolution doesn't loop forever.

@YaZko

YaZko commented Feb 13, 2019

Copy link
Copy Markdown
Collaborator

Note that switching the representation of programs in Asm to the old definition of CR required to split their denotation in two, first to denote the program and then the main itself, since the latter is now a block and no longer a label.

@gmalecha

gmalecha commented Mar 1, 2019

Copy link
Copy Markdown
Collaborator Author

Is the plan to hold this until the deadline?

@Lysxia

Lysxia commented Mar 1, 2019

Copy link
Copy Markdown
Collaborator

that sounds like a good plan!

@Lysxia
Lysxia merged commit 5b17f60 into master Mar 2, 2019
@Lysxia
Lysxia deleted the imp2asm branch March 12, 2019 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wip Work in Progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants