Giter Site home page Giter Site logo

clang's Introduction

LLVM backend for DCPU-16

DCPU-16 is the processor from Mojang's new game 0x10c. This project has a goal to make a full-featured LLVM backend for DCPU-16 and port Clang to support this architecture.

Currently llvm backend and Clang support are nearly fully implemented.

Simple C SDK

You can download a simple C SDK, which includes clang with the binutils port for dcpu16. It is Makefile based and contains a very simple C test program to demonstrate how you can develop your own program.

####Download#### You can find different precompiled packages for linux here.

There is also a very simple build script, which will assemble all needed parts for building the SDK yourself.

Please, let us know, if it does not work for you.

Building

First, of all, you need to install prerequisites:

sudo apt-get install gcc g++ git subversion git-svn make\
 perl gawk expect tcl texinfo bison autoconf automake cmake

Note, that when your python executable points to python3 (you can check that by running python --version) you have to replace "cmake .." with "cmake -DPYTHON_EXECUTABLE=/path/to/python2 .." in the following steps (path is /usr/bin/python2 in most cases)

Next, get the sources and build it:

git clone git://github.com/llvm-dcpu16/llvm-dcpu16.git # Checkout LLVM
cd llvm-dcpu16/tools
git clone git://github.com/llvm-dcpu16/clang.git # Checkout Clang
mkdir ../cbuild
cd ../cbuild
cmake ..
make -j4

Using LLVM-DCPU16

Consider the following C file:

fib.c:

int fib(int n) {
  int cur = 1;
  int prev = 1;
  for (int i = 0; i < n; i++) {
    int next = cur+prev;
    prev = cur;
    cur = next;
  }
  return cur;
}

int main(void) {
  return fib(5);
}

Now, let's translate C to DCPU16 assembly:

bin/clang -ccc-host-triple dcpu16 -S fib.c -o fib.s

We generate DCPU16 v1.7 ASM with GAS compatible syntax. Most online assemblers won't work. Please use the binutils port for DCPU16 to assemble and link your program and then use an DCPU16 v1.7 compatible emulator to test your program.

Enjoy and, please, report bugs!

clang's People

Contributors

abramobagnara avatar akyrtzi avatar annazaks avatar belkadan avatar bob-wilson avatar chandlerc avatar chapuni avatar d0k avatar ddunbar avatar douggregor avatar dwblaikie avatar echristo avatar eefriedman avatar espindola avatar fpichet avatar ggreif avatar isanbard avatar jdennett avatar kjdyck avatar lattner avatar nico avatar nlewycky avatar nunoplopes avatar pcc avatar rjmccall avatar sampo3k avatar tkremenek avatar weverything avatar xuzhongxing avatar zygoloid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clang's Issues

memset optimizer(?) still thinks DCPU is 8-bit addressible

The optimizer seems to think a 16-bit set is enough to clear two adjacent words in some cases. The code is correct without -O.

$ cat memset3.c
void* mymemclr4(int *buf, int len) {
  int *end = buf+len;
  while(buf != end) {
    *buf++ = 0;
    *buf++ = 0;
    *buf++ = 0;
    *buf++ = 0;
  }
}

extern int buf[128];
int main() {
  mymemclr4(buf, sizeof(buf));
}

$ bin/clang -target dcpu16 -S memset3.c -O -o -
memset3.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
        ; .file "memset3.c"
        .text
        .globl  mymemclr4
        ; .align        1
:mymemclr4
        IFE     B, 0x0
        SET     PC, .LBB0_2
:.LBB0_1
        SET     [0x2+A], 0x0       ; <--- it somehow thinks set [a+2],0 and set [a], 0
        SET     [A], 0x0           ; <--- is sufficient to clear 4 words.
        ADD     A, 0x4
        ADD     B, 0xfffc    ; <-- it's also somewhat perplexing that it doesn't generate sub B, 4 here
        IFN     B, 0x0
        SET     PC, .LBB0_1
:.LBB0_2
        SET     PC, POP

        .globl  main
        ; .align        1
:main
        SET     PUSH, J
        SET     J, SP
        SET     A, SP
        SET     [A], 0x80
        SET     A, buf
        SET     B, 0x0
        SET     C, 0x0
        JSR     memset
        SET     A, 0x0
        SET     J, POP
        SET     PC, POP

Crash on literal strings

The following program crashes while being parsed (?) by clang:

struct device_information {
  unsigned id_lo, id_hi;
  unsigned version;
  unsigned manufacturer_lo, manufacturer_hi;
};

void get_device_information(unsigned device_number,
                            struct device_information *info) {
  asm volatile (
      "HWQ %5\n"
      "SET %0, A\n"
      "SET %1, B\n"
      "SET %2, C\n"
      "SET %3, X\n"
      "SET %4, Y"
      :
      "=g" (info->id_lo),
      "=g" (info->id_hi),
      "=g" (info->version),
      "=g" (info->manufacturer_lo),
      "=g" (info->manufacturer_hi)
      :
      "r" (device_number)
      :
      // Clobbered registers
      "A", "B", "C", "X", "Y");
}

If the asm string is concatenated into one line ("HWQ %5\nSET %0, A\nSET %1, B\nSET %2, C\nSET %3, X\nSET %4, Y"), everything works as it should.

Tests are broken

It appears that Clang tests are broken. The tree is closed.

$ make clang-test -j8

FAIL: Clang :: Driver/dcpu16.cpp (2191 of 4518)
******************** TEST 'Clang :: Driver/dcpu16.cpp' FAILED ********************
Script:
--
/home/ubuntu/llvm-dcpu16/cbuild/bin/./clang  -ccc-host-triple dcpu16 -ccc-echo /home/ubuntu/llvm-dcpu16/tools/clang/test/Driver/dcpu16.cpp -emit-llvm -c -o /tmp/OUTPUTNAME 2> /home/ubuntu/llvm-dcpu16/cbuild/tools/clang/test/Driver/Output/dcpu16.cpp.tmp.log
grep 'clang\(-[0-9.]\+\)\?\(\.[Ee][Xx][Ee]\)\?" -cc1 .*dcpu16.c' /home/ubuntu/llvm-dcpu16/cbuild/tools/clang/test/Driver/Output/dcpu16.cpp.tmp.log
llvm-dis < /tmp/OUTPUTNAME | FileCheck /home/ubuntu/llvm-dcpu16/tools/clang/test/Driver/dcpu16.cpp
--
Exit Code: 1
Command Output (stdout):
--
 "/home/ubuntu/llvm-dcpu16/cbuild/bin/clang-3.1" -cc1 -triple dcpu16 -emit-llvm-bc -disable-free -main-file-name dcpu16.cpp -mrelocation-model static -mdisable-fp-elim -mconstructor-aliases -momit-leaf-frame-pointer -coverage-file /tmp/OUTPUTNAME -resource-dir /home/ubuntu/llvm-dcpu16/cbuild/bin/../lib/clang/3.1 -fmodule-cache-path /var/tmp/clang-module-cache -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /home/ubuntu/llvm-dcpu16/cbuild/tools/clang/test/Driver -ferror-limit 19 -fmessage-length 0 -mstackrealign -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-fragile-abi -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o /tmp/OUTPUTNAME -x c++ /home/ubuntu/llvm-dcpu16/tools/clang/test/Driver/dcpu16.cpp
--
Command Output (stderr):
--
/home/ubuntu/llvm-dcpu16/tools/clang/test/Driver/dcpu16.cpp:21:12: error: expected string not found in input
 // CHECK: signext i8 @check_char()
           ^
<stdin>:5:31: note: scanning from here
define void @__DCPU16__defined() nounwind {
                              ^
<stdin>:10:8: note: possible intended match here
define signext i16 @check_char() nounwind {
       ^
--

********************
Testing Time: 45.16s
********************
Failing Tests (1):
    Clang :: Driver/dcpu16.cpp

  Expected Passes    : 4491
  Expected Failures  : 26
  Unexpected Failures: 1
make[3]: *** [tools/clang/test/CMakeFiles/clang-test] Error 1
make[2]: *** [tools/clang/test/CMakeFiles/clang-test.dir/all] Error 2
make[1]: *** [tools/clang/test/CMakeFiles/clang-test.dir/rule] Error 2
make: *** [clang-test] Error 2

calling convention on builtins is broken

If I call memset(a,b,c), it always generates a call to memset with c = 0. If I call memset2 instead, it works. observe:

This is especially annoying because clang does enough inference to figure out when I'm doing a memset, and autogenerates a call to memset in those instances.

$ cat memset1.c
void* memset(void* buf, int c, unsigned siz);

extern int buf[128];
int main()
{
  memset(buf, 1, sizeof(buf));
}

$ bin/clang -target dcpu16 -S memset1.c -O -o -
        ; .file "memset1.c"
        .text
        .globl  main
        ; .align        1
:main
        SET     PUSH, J
        SET     J, SP
        SET     A, SP
        SET     [A], 0x80
        SET     A, buf
        SET     B, 0x1
        SET     C, 0x0 ; <--- ?!  C is supposed to be 0x80.
        JSR     memset
        SET     A, 0x0
        SET     J, POP
        SET     PC, POP

now if I just change memset to memset2:

$ cat memset2.c
void* memset2(void* buf, int c, unsigned siz);

extern int buf[128];
int main()
{
  memset2(buf, 1, sizeof(buf));
}


$ bin/clang -target dcpu16 -S memset2.c -O -o -
        ; .file "memset2.c"
        .text
        .globl  main
        ; .align        1
:main
        SET     PUSH, J
        SET     J, SP
        SUB     SP, 0xfffe
        SET     A, buf
        SET     B, 0x1
        SET     C, 0x80     ; <--- correct now as it's just another call
        JSR     memset2
        SET     A, 0x0
        ADD     SP, 0xfffe
        SET     J, POP
        SET     PC, POP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.