Implement Dynamic Typing Lanugage Using LLVM IR [closed]
Asked Answered
P

0

10

I'm trying to implement a toy language with dynamic typing, i.e., variables do not have types, only values do, and every variable needs to be declared before being used. For example, the code might look like var x; x = 3; x = 'a';. Furthermore, I want to compile this toy language to native code instead of running it in some virtual machine.

Currently, I've generated an AST and I am going to compile the AST to LLVM IR using LLVM C++ APIs. The question is, what kind of IR should I generate for

  1. Variable Declaration (with or without initial value, like var x; or var y = 3.4;
  2. Variable Access (like a in c = a + 1)
Pantywaist answered 3/9, 2015 at 13:7 Comment(7)
The answer to this question depends on GC. If you're using GC in your runtime, variables should all be pointers to the boxed tagged unions representing the code data types. Each operation should be guarded with tag checks. See how it's done in Julia, for example.Kovrov
@Kovrov Thanks for your comment! I'll take a look into Julia, but I think GC is too complicated for my toy language. My idea is very similiar to your description that variables are pointers to the boxed tagged unions. I'm curious about details of implementing it with LLVM, since the LLVM system is too difficult for me to understand from scratch.Pantywaist
If you've got no GC, you still need to use tagged unions, but allocate them on stack instead (so, LLVM variables would be of a structure type and you'll have a lot of bitcasts - a low level union type had been discontinued long ago). I'd suggest to start with something simple: generate your code as calls to runtime functions (allocate a value, check value type, return value contents, etc.), and then start optimising it, replacing the function calls with more complex IR patterns (may not even be needed at all if inlining takes care of it).Kovrov
What's going on here? It seems like the mods didn't understand the question, closed the question, and then deleted their comments. WTF guys?Dorotea
@Dorotea I think the problem is that I added 'c++' tag to this question. I've removed this tag but they still closed my question. Is thery anything I can do to reopen it?Pantywaist
@YakumoRan Flag it for mod attention. Hopefully they'll will vote to reopen.Dorotea
I am cautiously voting to reopen. The question's phrasing suggests the OP is a bit in over their head, but answers to the question might still be useful to others. (Really good answers will be quite long, so the original closure as too broad is entirely defensible.)Cagliostro

© 2022 - 2024 — McMap. All rights reserved.