Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into is_same_type
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamnarlawar authored Mar 3, 2021
2 parents 4fff6b4 + fca762c commit 0a66764
Show file tree
Hide file tree
Showing 40 changed files with 638 additions and 146 deletions.
41 changes: 36 additions & 5 deletions .github/workflows/PRCheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,46 @@ on:

jobs:
build:

runs-on: ubuntu-20.04
steps:
- name: Install utils
run: (sudo apt update && sudo apt install -y llvm-11-dev cargo && cargo install cbindgen)
run: |
sudo apt update
sudo apt install -y llvm-11-dev cargo python3-pip
cargo install cbindgen
echo "$HOME/.local/bin" >> $GITHUB_PATH
pip3 install lit filecheck
- name: Install JRE 1.8
uses: actions/setup-java@v1
with:
java-version: '8'
java-package: jre
architecture: x64

- name: Clone nballerina repository
id : cache-nballerina
- name: Clone nBallerina
uses: actions/checkout@v2

- name: Build nballerinacc
run: (cd "$GITHUB_WORKSPACE/build" && cmake -DCMAKE_BUILD_TYPE=Release ../ && make -j)
run: |
cmake -DCMAKE_BUILD_TYPE=Release ../
make -j
working-directory: ./build

- name: Build runtime header
run: |
make runtime_header
working-directory: ./build

- name: Setup Ballerina pack
run: |
curl -L https://maven.pkg.github.com/ballerina-platform/ballerina-lang/org/ballerinalang/jballerina-tools/2.0.0-Preview2-nballerina-r1/jballerina-tools-2.0.0-Preview2-nballerina-r1.zip -u "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" -o jballerina-tools-2.0.0-Preview2-nballerina-r1.zip
unzip -q jballerina-tools-2.0.0-Preview2-nballerina-r1.zip
cd jballerina-tools-2.0.0-Preview2-nballerina-r1/bin
echo $(pwd) >> $GITHUB_PATH
working-directory: ./build

- name: Run LIT tests
run: |
make check
working-directory: ./build
30 changes: 23 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.15)
if (NOT UNIX)
cmake_policy(SET CMP0091 NEW)
endif()
project(nballerina VERSION 0.0.1 LANGUAGES CXX)

# Find and loacd LLVM components
Expand All @@ -7,7 +10,7 @@ message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
add_definitions(${LLVM_DEFINITIONS})

# Include and lib directories
include_directories(${LLVM_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/runtime/include)
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
link_directories(${CMAKE_SOURCE_DIR}/runtime/target/release)

# Add runtime project
Expand All @@ -18,14 +21,27 @@ file(GLOB SOURCES
${PROJECT_SOURCE_DIR}/compiler/*.cpp
)
add_executable(nballerinacc ${SOURCES})
add_dependencies(nballerinacc nballerina_rt)
target_link_libraries(nballerinacc PRIVATE LLVM-11)

add_dependencies(nballerinacc BallerinaRT)

llvm_map_components_to_libnames(llvm_libs codegen)
if (NOT UNIX)
set_property(TARGET nballerinacc PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
target_link_libraries(nballerinacc PRIVATE ${llvm_libs})

# Set extra warnings for nballerinacc
set(DEBUG_OPTIONS -Wall)
set(RELEASE_OPTIONS -Wall)
if (MSVC)
set(DEBUG_OPTIONS /W4)
set(RELEASE_OPTIONS /W4)
else()
set(DEBUG_OPTIONS -Wall)
set(RELEASE_OPTIONS -Wall)
endif()

target_compile_options(nballerinacc PUBLIC "$<$<CONFIG:DEBUG>:${DEBUG_OPTIONS}>")
target_compile_options(nballerinacc PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_OPTIONS}>")

# Add LIT tests
add_subdirectory(test)
add_subdirectory(test)
126 changes: 113 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,130 @@
# This repository contains code for translating Ballerina IR to LLVM IR
# nBallerina
Translate Ballerina IR to LLVM IR.

## Prerequisites for Ubuntu 20.04
## Building from source in Ubuntu 20.04
### Prerequisites
* `sudo apt install build-essential llvm-11-dev cmake cargo python3-pip`
* `cargo install cbindgen`
* `pip3 install lit filecheck`

## Build steps
### Build steps
1. `cd build`
2. `cmake ../ -DCMAKE_BUILD_TYPE=Debug`
3. `make -j`
This will build the Rust runtime (output in runtime/target/release), the C++ header for the runtime (output in runtime/include/) and the nballerinacc app.

## Usage
* Output binary will be at `build/nballerinacc`
This will build:
* The Rust runtime : runtime/target/release/libballerina_rt.so
* The nballerinacc (BIR to LLVM IR converter) app : build/nballerinacc

### Run tests
1. Install Java 8 runtime (e.g. openjdk-8-jre on ubuntu)
2. Create `JAVA_HOME` environment variable and point it to the Java install dir
3. Get this specific Ballerina version : [link](https://drive.google.com/file/d/1a1TlJdw-rTtCLOFrrvJe4nr1FkGzwpKH/view?usp=sharing)
4. Extract Ballerina pack and add the `bin` folder with the `ballerina` executable to your `PATH` system variable
5. Navigate to nBallerina `build/` folder and run tests

cd build/
make check

### Usage
* Run nballerinacc against a BIR dump file to generate the .ll LLVM IR file

./nballerinacc ../BIR2llvmIR/main-bir-dump
./nballerinacc ../compiler/main-bir-dump
* The .ll file can be compiled into an a.out by invoking clang with -O0 option.

clang++-11 -O0 main-bir-dump.ll
clang -O0 main-bir-dump.ll
* The -O0 is required because the optimizer will otherwise optimize everything away.
* Running the a.out and checking the return value on the command-line by using "echo $?" will yield the int value returned by the function.
* The a.out can be disassembled using "objdump -d" to see the machine instructions generated in main.
* Optional: Run `export LD_LIBRARY_PATH=<Full path to runtime .so>` during development so that the dynamic loader can find the nBallerina runtime libs at app runtime.
* Execute the tests in `test` folder:
* The a.out can be disassembled using "objdump -d" to see the machine instructions

### To codegen the C header for the Rust runtime lib
1. Install cbindgen

cargo install cbindgen
2. Execute build command

cd build/
make runtime_header
3. The generated header will be at : runtime/include/ballerina_rt.h


## Building from source in MacOSX 10.15.0
### Prerequisites
* If you haven’t installed xcode and brew yet, please install them. Following steps assumes that xcode and brew are already installed.
* `brew install llvm@11 rust python3`
* `brew postinstall python3`
* `pip3 install lit filecheck`

### Build steps
1. `cd build`
2. `cmake ../ -DCMAKE_BUILD_TYPE=Debug -DLLVM_DIR=/usr/local/opt/llvm/lib/cmake/llvm/ -DCMAKE_CXX_STANDARD=14`
3. `make -j`

This will build:
* The Rust runtime : runtime/target/release/libballerina_rt.so
* The nballerinacc (BIR to LLVM IR converter) app : build/nballerinacc

### Run tests
1. Install Java 8 runtime (e.g. JRE 8 package for MacOS)
2. Create `JAVA_HOME` environment variable and point it to the Java install dir
3. Get this specific Ballerina version : [link](https://drive.google.com/file/d/1a1TlJdw-rTtCLOFrrvJe4nr1FkGzwpKH/view?usp=sharing)
4. Extract Ballerina pack and add the `bin` folder with the `ballerina` executable to your `PATH` system variable
5. Navigate to nBallerina `build/` folder and run tests

cd build/
make check
* To add LIT tests, add LIT compatible .bal files to the `test/` folder

### Usage
* Run nballerinacc against a BIR dump file to generate the .ll LLVM IR file

./nballerinacc ../compiler/main-bir-dump -o main.ll
* The .ll file can be compiled into an a.out by invoking clang with -O0 option.

clang -O0 main.ll
* The -O0 is required because the optimizer will otherwise optimize everything away.
* Running the a.out and checking the return value on the command-line by using `echo $?` will yield the int value returned by the function.
* The a.out can be disassembled using `objdump -d` to see the machine instructions

### To codegen the C header for the Rust runtime lib
1. Install cbindgen

cargo install cbindgen
* cargo installs the dependencies in user's home directory. Hence, add `~/.cargo/bin` to your `PATH` system variable. `export PATH=$PATH:~/.cargo/bin`
2. Execute build command

cd build/
make runtime_header
3. The generated header will be at : runtime/include/ballerina_rt.h

## Building from source in Windows 10(x64)

### Prerequisites
* Install [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html)
* Install [python-3](https://www.python.org/downloads/)
* Build LLVM from source
* Install [Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019). Please make sure that tools should be installed with **C++ CMake tools for Windows** and **C++ ATL for latest v142 build tools (x86 & x64)** which are optional.
* Download the LLVM source from [here](https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.1/llvm-11.0.1.src.tar.xz) and extract.
* Create the `build` directory. Run following commands in **x64 Native Tools Command Prompt for VS 2019**.

cd build
cmake -DCMAKE_BUILD_TYPE="Release" -Thost=x64 -G "Visual Studio 16 2019" -A x64 -DLLVM_USE_CRT_RELEASE=MT -DCMAKE_INSTALL_PREFIX="<LLVM_INSTALL_PATH>" ../
msbuild INSTALL.vcxproj /p:configuration=release


### Build steps
Clone the nballerina source and run below commands.
1. `cd build`
2. `cmake -DLLVM_DIR=<LLVM_INSTALL_PATH>\lib\cmake\llvm ../`
3. `msbuild ALL_BUILD.vcxproj /p:configuration=Release`

### Usage
* Output binary will be at `Release\nballerinacc.exe`
* Run nballerinacc against a BIR dump file to generate the .ll LLVM IR file

nballerinacc.exe ../../compiler/main-bir-dump -o main.ll
* Create the executable file and execute it

clang -O0 main.ll
a.exe

### Run tests
* Tests are currently not supported in Windows
Loading

0 comments on commit 0a66764

Please sign in to comment.