Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCIL-25: Update of control flow, native bool support, documentation #250

Merged
merged 31 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8971fe6
SCIL-55: Bool support
troelsfr Oct 23, 2023
7d15d10
Adding trait to create symbol table
troelsfr Oct 23, 2023
68178b0
Fixing hex conversion of precompile addresses
troelsfr Oct 25, 2023
6ed7931
Adding module defined default constructors
troelsfr Oct 26, 2023
f6c2f2a
Adding example
troelsfr Oct 26, 2023
bb6bd56
Adding hex to the emitter
troelsfr Oct 26, 2023
64a5629
Adding support for wild cards in match statements
troelsfr Oct 27, 2023
300c717
Fixing symbols
troelsfr Oct 27, 2023
236006f
Fixing source location TODO for function calls
troelsfr Oct 27, 2023
86a368e
Merge branch 'main' into feature/scil-update
troelsfr Oct 27, 2023
049e749
Merging main
troelsfr Oct 27, 2023
b174a3e
Adding and fixing function calls with zero arguments
troelsfr Oct 27, 2023
e948451
Cleaning code
troelsfr Oct 31, 2023
503742e
Removing unused switch
troelsfr Oct 31, 2023
7dbc799
Updating
troelsfr Oct 31, 2023
797a70f
Fixing warnings
troelsfr Oct 31, 2023
926ec9c
Minor update
troelsfr Oct 31, 2023
920cdba
Fixing warnings
troelsfr Oct 31, 2023
0631702
Finishing eliminating warnings
troelsfr Oct 31, 2023
e539b49
Documentation for AST
troelsfr Oct 31, 2023
1144d2c
Updating documentation
troelsfr Oct 31, 2023
e911614
Removing script
troelsfr Nov 1, 2023
bffa807
Removing debug info
troelsfr Nov 2, 2023
28f80ad
Update products/bluebell/core/src/evm_bytecode_generator.rs
troelsfr Nov 3, 2023
2f57480
Update products/bluebell/playground/src/vm_remote_layout.rs
troelsfr Nov 3, 2023
68506e1
Adding README file
troelsfr Nov 3, 2023
a7d2277
Merge branch 'feature/scil-update' of github.com:Zilliqa/zilliqa-deve…
troelsfr Nov 3, 2023
8c04190
Squashing imports
troelsfr Nov 3, 2023
0e30bbe
Update products/bluebell/README.md
troelsfr Nov 3, 2023
7f5784b
Update products/bluebell/README.md
troelsfr Nov 3, 2023
63b0dc2
Update products/bluebell/core/src/ast/nodes.rs
troelsfr Nov 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion products/bluebell/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]

resolver = "2"
members = [
"core",
"evm_assembly",
Expand Down
36 changes: 36 additions & 0 deletions products/bluebell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Bluebell Scilla Compiler

This README provides examples of how to use the Bluebell command line tool.

## Running a Scilla File

To run a Scilla file, use the `Run` command followed by the `--entry_point` flag to specify the function to invoke, the `--args` flag to pass arguments to the function, and the `--backend` flag to specify the backend to use. Here is an example:

```bash
cargo run --bin cli -- examples/hello-world.scilla --runtime-enable debug run --backend evm --entry-point "HelloWorld::setHello" --args "[\"Zilliqa ❤️ Rocks\"]"
```

This command will run the `main` function of the `hello-world.scilla` file with the argument `[\"Zilliqa ❤️ Rocks\"]` using the EVM backend. This should procude an output similar to
troelsfr marked this conversation as resolved.
Show resolved Hide resolved

```bash
Zilliqa ❤️ Rocks


Exit reason: Succeed(
Returned,
)Result: []%
```

This is a basic example of compiling a Scilla contract into EVM bytecode and running the code in a EVM instance. Note that this example makes use of external precompiles which provides the `print` command to print `Zilliqa ❤️ Rocks` to the terminal.

## Running the playground

To set up Rust to run the playground, first follow these steps:

1. Install Rust by following the instructions on the official Rust website.
2. Add WebAssembly (Wasm) support to your Rust setup by running `rustup target add wasm32-unknown-unknown`.
3. Install Trunk by running `cargo install trunk`.
4. Navigate to your project `playground/`.
5. Run `trunk serve` to start the development server.

A more detailed guide can be found in the playgrounds [README.md](playground/README.md) file.
troelsfr marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 11 additions & 6 deletions products/bluebell/cargo-webdev/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::{
path::Path,
sync::{Arc, Mutex},
};

use notify::{recommended_watcher, RecursiveMode, Watcher};
use std::path::Path;
use std::sync::{Arc, Mutex};
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
use tokio::process::{Child, Command};
use tokio::sync::broadcast;
use tokio::time::{sleep, Duration};
use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
process::{Child, Command},
sync::broadcast,
time::{sleep, Duration},
};

async fn stream_output(mut child: Child) -> Result<Child, Box<dyn std::error::Error>> {
let mut stdout = child
Expand Down
167 changes: 11 additions & 156 deletions products/bluebell/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
// Importing necessary modules and types
use bluebell::support::modules::BluebellModule;
use bluebell::support::modules::ScillaDebugBuiltins;
use std::ffi::CStr;

use std::{fs::File, io::Read, process};

use bluebell::{
ast::nodes::NodeProgram,
parser::{lexer, lexer::Lexer, parser, ParserError},
support::{
evm::EvmCompiler,
modules::{ScillaDebugBuiltins, ScillaDefaultBuiltins, ScillaDefaultTypes},
},
};
use clap::{Parser, Subcommand, ValueEnum};
// DEPRECATED
// use inkwell::context::Context;
// use inkwell::targets::{InitializationConfig, Target};
use std::fs::File;
use std::io::Read;
use std::os::raw::c_char;
use std::process;

use bluebell::ast::nodes::NodeProgram;
use bluebell::contract_executor::UnsafeContractExecutor;
use bluebell::passes::debug_printer::DebugPrinter;

use bluebell::support::evm::EvmCompiler;
use bluebell::support::modules::{ScillaDefaultBuiltins, ScillaDefaultTypes};

// use bluebell::llvm_ir_generator::LlvmIrGenerator;
// use bluebell::support::llvm::{LlvmBackend, UnsafeLlvmTestExecutor};

use bluebell::intermediate_representation::emitter::IrEmitter;
use bluebell::intermediate_representation::pass_manager::PassManager;

use bluebell::parser::lexer;
use bluebell::parser::lexer::Lexer;
use bluebell::parser::{parser, ParserError};
use evm_assembly::types::EvmTypeValue;
use log::{Log, Metadata, Record};

Expand Down Expand Up @@ -71,14 +52,12 @@ fn setup_logger() {
// Enum to define the output format of Bluebell
#[derive(Clone, Debug, Subcommand)]
enum BluebellOutputFormat {
LlvmIr,
FormattedScilla,
}

// Enum to define the backend of Bluebell
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum BluebellBackend {
Llvm,
Evm,
}

Expand All @@ -104,7 +83,7 @@ enum BluebellCommand {
entry_point: String,

/// Arguments to pass to function
#[arg(short, long, default_value_t= ("".to_string()))]
#[arg(short, long, default_value_t= String::new())]
args: String,
},
}
Expand Down Expand Up @@ -180,131 +159,9 @@ fn bluebell_evm_run(
serde_json::from_str(&args).expect("Failed to deserialize arguments")
};

print!("Arguments: {:?}", args);
executable.execute(&entry_point, arguments);
}

// Function to run Bluebell with LLVM backend
fn bluebell_llvm_run(ast: &NodeProgram, entry_point: String, debug: bool) {
// DEPRECATED
panic!("LLVM support is DEPRECATED for now.");
/*
/****** Executable *****/
///////
let backend = LlvmBackend::new();
// TODO: runtime is a poor name.
let mut specification = backend.create_backend_specification();

specification.declare_integer("Int8", 8);
specification.declare_integer("Int16", 16);
specification.declare_integer("Int32", 32);
specification.declare_integer("Int64", 64);
specification.declare_unsigned_integer("Uint8", 8);
specification.declare_unsigned_integer("Uint16", 16);
specification.declare_unsigned_integer("Uint32", 32);
specification.declare_unsigned_integer("Uint64", 64);

let _ = specification
.declare_intrinsic("add", ["Int32", "Int32"].to_vec(), "Int32")
.attach_runtime(|| {
extern "C" fn addi32(a: i32, b: i32) -> i32 {
a + b
}

addi32 as usize
});

// let _executable = backend.create_executable("test");
// let executable = backend.compile(name, script);

let context = Context::create();
let mut module = context.create_module("main");

// Runtime struct <- contains Context
// VM / Executor
// Executable <- contains Module
// Compiler

// Declaring runtime
let ft = context.f64_type();
let i8_ptr_type = context.i8_type().ptr_type(inkwell::AddressSpace::default());
let fn_type = context.void_type().fn_type(&[i8_ptr_type.into()], false);

module.add_function("sumf", ft.fn_type(&[ft.into(), ft.into()], false), None);
module.add_function("builtin__print<msg>", fn_type, None);

let setup_runtime = |contract_executor: &UnsafeLlvmTestExecutor| {
Target::initialize_native(&InitializationConfig::default()).unwrap();

//////
// Defining runtime

extern "C" fn sumf(a: f64, b: f64) -> f64 {
a + b
}
extern "C" fn print_string(s: *const c_char) {
let c_str = unsafe { CStr::from_ptr(s) };
let str_slice: &str = c_str.to_str().unwrap();
}
unsafe {
contract_executor.link_symbol("sumf", sumf as usize);
contract_executor.link_symbol("builtin__print<msg>", print_string as usize);
}
};

/*** Compiling ***/

/////
// Frontend: AST -> Highlevel IR
let mut generator = IrEmitter::new();
let mut ir = generator.emit(ast).expect("Failed generating highlevel IR");

/*** Analysis ***/
let mut pass_manager = PassManager::default_pipeline();

if let Err(err) = pass_manager.run(&mut ir) {
panic!("{}", err);
}

let mut debug_printer = DebugPrinter::new();
let _ = ir.run_pass(&mut debug_printer);

///////
// Lowering/"backend": Generating LLVM IR
let mut generator = LlvmIrGenerator::new(&context, ir, &mut module);

match generator.build_module() {
Err(e) => {
let llvm_str = module.print_to_string();
let output = llvm_str.to_str().expect("Failed converting to UTF8");
println!("{}", output);

panic!("Error: {:?}", e);
}
Ok(_) => (),
};

if debug {
let llvm_str = module.print_to_string();
let output = llvm_str.to_str().expect("Failed converting to UTF8");
println!("{}", output);
}

/****** Execution *****/
//////

//////
// Executing

let contract_executor = UnsafeLlvmTestExecutor::new(&mut module);
setup_runtime(&contract_executor);

unsafe {
contract_executor.execute(&entry_point);
}
*/
}

// Main function
fn main() {
// Setting up the logger
Expand Down Expand Up @@ -339,8 +196,6 @@ fn main() {
args: arguments,
backend,
} => match backend {
// Running with LLVM backend
BluebellBackend::Llvm => bluebell_llvm_run(&ast, entry_point, args.debug),
// Running with EVM backend
BluebellBackend::Evm => {
bluebell_evm_run(&ast, entry_point, arguments, features, args.debug)
Expand Down
Loading
Loading