shinh / elvm

EsoLangVM Compiler Infrastructure
MIT License
1.13k stars 143 forks source link

Minecraft function backend #70

Closed Earthcomputer closed 4 years ago

Earthcomputer commented 4 years ago

Ask asked in #68, this PR adds support for a Minecraft function backend. It is WIP, I'm creating the PR now as I would like feedback from the maintainers to review the code and ask how best to resolve a couple of issues:

shinh commented 4 years ago

Great, thanks for your work! I'm not familiar with Minecraft but I'm curious, have you actually managed to run C compiler in Minecraft?

As for backend specific options, how about introducing a mechanism like this?

https://github.com/shinh/elvm/pull/71

If this works for you, I'll merge this PR. Handling backend specific options on EIR is not implemented. I'll probably implement it in future, or contribution is welcome :)

As for multiple output files, I don't have a better idea than introducing file separators as you are doing. Some other backends also need pre/post-processing so it'd be fine. See tools directory for examples.

Earthcomputer commented 4 years ago

71 looks good, thanks!

I have yet to test 8cc in Minecraft, because it is likely to take a while, but I'll see if I can run it overnight tonight.

By backend-specific options on EIR, do you mean something like adding a mul instruction if the backend supports it, and using the builtin function otherwise? That would work well for this backend, which natively supports multiplication, division and modulus. My worry is that it would take away from 8cc being independent of the backend; the way I see it now is that you can run 8cc once and then run elc for each of the backends, which is nicer. Anyway, this is a discussion for the future.

shinh commented 4 years ago

Thanks! I've merged #17

No, I just meant calling get_handle_args_func around https://github.com/shinh/elvm/blob/master/target/elc.c#L122 . Since EIR does not have argv and it only has stdin, elc.c uses the first line to figure out the name of backend. For example:

$ make rb
$ out/elc -rb out/hello.c.eir > a
$ (echo rb && cat out/hello.c.eir) | ruby out/elc.c.eir.rb > b
$ diff a b  # no diff

Similarly, I want to do

$ ./out/elc -rb out/hello.c.eir -chunked_func_size 200  # #71 implemented this.
$ (echo "rb -chunked_func_size 200" && cat out/hello.c.eir) | ruby out/elc.c.eir.rb # not implemented

Anyway, yeah, we don't need to do this right now.

BTW, as for ops backend may or may not implement like mul, "llvm" branch, which uses clang+llvm instead of 8cc, has such operations: https://github.com/shinh/elvm/blob/llvm/ir/ir.h#L27 .

MCRcortex commented 4 years ago

I can confirm that the 8cc compiler can compile a program in minecraft It was done in 2 stages, loading to the getchar and then post processing. There where some issues with getchar that will need to be looked into, however, it works.

loading to getchar took around 2 hours then processing c into EIR another 2 hours this is the compiled program int main(){char* x="Hello World!";while(*x!=0){putchar(*x);x++;}}\x00

Here is the compile log

[16:59:07] [Render thread/INFO]: [CHAT] Command set: data modify storage elvm:elvm stdin set value [105,110,116,32,109,97,105,110,40,41,123,99,104,97,114,42,32,120,61,34,72,101,108,108,111,32,87,111,114,108,100,33,34,59,119,104,105,108,101,40,42,120,33,61,48,41,123,112,117,116,99,104,97,114,40,42,120,41,59,120,43,43,59,125,125,0]
[16:59:09] [Server thread/INFO]: [@: Modified storage elvm:elvm]
[16:59:09] [Render thread/INFO]: [CHAT] [@: Modified storage elvm:elvm]

[18:10:58] [Render thread/INFO]: [CHAT]  [1;31m[WARN] [0m /mnt/c/Users/XXXX/Downloads/elvm/elvm/out/8cc.h:432: -:2:55: assume returning int: putchar()
[18:44:19] [Render thread/INFO]: [CHAT]  .text
[18:44:22] [Render thread/INFO]: [CHAT] main:
[18:44:32] [Render thread/INFO]: [CHAT]  #{push:main}
[18:44:34] [Render thread/INFO]: [CHAT]  mov D, SP
[18:44:37] [Render thread/INFO]: [CHAT]  add D, -1
[18:44:43] [Render thread/INFO]: [CHAT]  store BP, D
[18:44:45] [Render thread/INFO]: [CHAT]  mov SP, D
[18:44:48] [Render thread/INFO]: [CHAT]  mov BP, SP
[18:44:53] [Render thread/INFO]: [CHAT]  sub SP, 1
[18:45:10] [Render thread/INFO]: [CHAT]  .file 1 "-"
[18:45:36] [Render thread/INFO]: [CHAT]  .loc 1 2 0
[18:46:12] [Render thread/INFO]: [CHAT]  mov A, 0
[18:46:14] [Render thread/INFO]: [CHAT]  mov B, SP
[18:47:18] [Render thread/INFO]: [CHAT] .data
[18:47:20] [Render thread/INFO]: [CHAT]  .L2:
[18:48:02] [Render thread/INFO]: [CHAT]  .string "Hello World!"
[18:48:03] [Render thread/INFO]: [CHAT] .text
[18:48:07] [Render thread/INFO]: [CHAT]  mov A, .L2
[18:48:12] [Render thread/INFO]: [CHAT]  mov B, BP
[18:48:25] [Render thread/INFO]: [CHAT]  add B, 16777215
[18:48:28] [Render thread/INFO]: [CHAT]  store A, B
[18:49:30] [Render thread/INFO]: [CHAT]  .L0:
[18:52:02] [Render thread/INFO]: [CHAT]  mov B, BP
[18:52:14] [Render thread/INFO]: [CHAT]  add B, 16777215
[18:52:17] [Render thread/INFO]: [CHAT]  load A, B
[18:52:23] [Render thread/INFO]: [CHAT]  mov B, A
[18:52:27] [Render thread/INFO]: [CHAT]  load A, B
[18:52:36] [Render thread/INFO]: [CHAT]  mov D, SP
[18:52:38] [Render thread/INFO]: [CHAT]  add D, -1
[18:52:42] [Render thread/INFO]: [CHAT]  store A, D
[18:52:45] [Render thread/INFO]: [CHAT]  mov SP, D
[18:53:21] [Render thread/INFO]: [CHAT]  mov A, 0
[18:53:24] [Render thread/INFO]: [CHAT]  mov B, A
[18:53:27] [Render thread/INFO]: [CHAT]  load A, SP
[18:53:30] [Render thread/INFO]: [CHAT]  add SP, 1
[18:53:34] [Render thread/INFO]: [CHAT]  ne A, B
[18:53:43] [Render thread/INFO]: [CHAT]  jeq .L3, A, 0
[18:56:27] [Render thread/INFO]: [CHAT]  mov B, BP
[18:56:41] [Render thread/INFO]: [CHAT]  add B, 16777215
[18:56:43] [Render thread/INFO]: [CHAT]  load A, B
[18:56:46] [Render thread/INFO]: [CHAT]  mov B, A
[18:56:49] [Render thread/INFO]: [CHAT]  load A, B
[18:56:59] [Render thread/INFO]: [CHAT]  mov D, SP
[18:57:05] [Render thread/INFO]: [CHAT]  add D, -1
[18:57:10] [Render thread/INFO]: [CHAT]  store A, D
[18:57:17] [Render thread/INFO]: [CHAT]  mov SP, D
[18:57:24] [Render thread/INFO]: [CHAT]  putc A
[18:57:29] [Render thread/INFO]: [CHAT]  add SP, 1
[18:58:22] [Render thread/INFO]: [CHAT]  mov B, BP
[18:58:46] [Render thread/INFO]: [CHAT]  add B, 16777215
[18:58:50] [Render thread/INFO]: [CHAT]  load A, B
[18:58:55] [Render thread/INFO]: [CHAT]  mov D, SP
[18:58:59] [Render thread/INFO]: [CHAT]  add D, -1
[18:59:05] [Render thread/INFO]: [CHAT]  store A, D
[18:59:09] [Render thread/INFO]: [CHAT]  mov SP, D
[18:59:12] [Render thread/INFO]: [CHAT]  add A, 1
[18:59:19] [Render thread/INFO]: [CHAT]  mov B, BP
[18:59:29] [Render thread/INFO]: [CHAT]  add B, 16777215
[18:59:38] [Render thread/INFO]: [CHAT]  store A, B
[18:59:44] [Render thread/INFO]: [CHAT]  load A, SP
[18:59:48] [Render thread/INFO]: [CHAT]  add SP, 1
[18:59:55] [Render thread/INFO]: [CHAT]  jmp .L4
[18:59:58] [Render thread/INFO]: [CHAT]  .L3:
[19:00:27] [Render thread/INFO]: [CHAT]  jmp .L1
[19:00:29] [Render thread/INFO]: [CHAT]  .L4:
[19:00:55] [Render thread/INFO]: [CHAT]  jmp .L0
[19:01:23] [Render thread/INFO]: [CHAT]  .L1:
[19:01:31] [Render thread/INFO]: [CHAT]  #{pop:main}
[19:01:33] [Render thread/INFO]: [CHAT]  exit
[19:01:34] [Server thread/INFO]: [mcrcortex: Executed 37101124 commands from function 'elvm:loop']

8cc was compiled in windows linux subsystem

Earthcomputer commented 4 years ago

@shinh I believe this is ready. Should I add the mcfunction backend to the readme too?

SavagePy10 commented 3 years ago

now do it with redstone no mods or command block