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

Roadmap #1

Closed
kassane opened this issue May 7, 2023 · 2 comments
Closed

Roadmap #1

kassane opened this issue May 7, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kassane
Copy link
Owner

kassane commented May 7, 2023

Initially, the LLVM 16 binding is being ported, which may later be extended to older versions for greater compatibility.

Based on the references in the readme will include some examples from the official LLVM-doc tutorial.

Note: Although the project is using the name Kaleidoscope tutorial. Nothing prevents the (llvm-kaleidoscope-zig -> llvm-zig) binding from being renamed for practical purposes other than educational purposes only.

Contributions are always welcome!!

@kassane kassane added enhancement New feature or request help wanted Extra attention is needed labels May 7, 2023
@kassane
Copy link
Owner Author

kassane commented May 7, 2023

@kassane
Copy link
Owner Author

kassane commented May 8, 2023

First example

This C example code has been used zig translate-c to Zig (it will be refactored for better understanding).

#include <stdio.h>
#include <stdlib.h>
#include "llvm-c/Core.h"
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Target.h"

int main() {
  // Initialize LLVM
  LLVMInitializeNativeTarget();
  LLVMInitializeNativeAsmPrinter();
  LLVMInitializeNativeAsmParser();

  // Create a new LLVM module
  LLVMModuleRef module = LLVMModuleCreateWithName("sum_module");

  // Create a function that computes the sum of two integers
  LLVMTypeRef params[] = { LLVMInt32Type(), LLVMInt32Type() };
  LLVMTypeRef func_type = LLVMFunctionType(LLVMInt32Type(), params, 2, 0);
  LLVMValueRef sum_func = LLVMAddFunction(module, "sum", func_type);
  LLVMBasicBlockRef entry = LLVMAppendBasicBlock(sum_func, "entry");
  LLVMBuilderRef builder = LLVMCreateBuilder();
  LLVMPositionBuilderAtEnd(builder, entry);
  LLVMValueRef arg1 = LLVMGetParam(sum_func, 0);
  LLVMValueRef arg2 = LLVMGetParam(sum_func, 1);
  LLVMValueRef sum = LLVMBuildAdd(builder, arg1, arg2, "sum");
  LLVMBuildRet(builder, sum);

  // Dump the LLVM module to stdout
  LLVMDumpModule(module);

  // Clean up LLVM resources
  LLVMDisposeBuilder(builder);
  LLVMDisposeModule(module);
  LLVMShutdown();

  return 0;
}

pub fn main() void {
// Initialize LLVM
_ = target.LLVMInitializeNativeTarget();
_ = target.LLVMInitializeNativeAsmPrinter();
_ = target.LLVMInitializeNativeAsmParser();
// Create a new LLVM module
var module: types.LLVMModuleRef = core.LLVMModuleCreateWithName("sum_module");
var params: [2]types.LLVMTypeRef = [_]types.LLVMTypeRef{
core.LLVMInt32Type(),
core.LLVMInt32Type(),
};
// Create a function that computes the sum of two integers
const func_type: types.LLVMTypeRef = core.LLVMFunctionType(core.LLVMInt32Type(), &params, 2, 0);
const sum_func: types.LLVMValueRef = core.LLVMAddFunction(module, "sum", func_type);
const entry: types.LLVMBasicBlockRef = core.LLVMAppendBasicBlock(sum_func, "entry");
const builder: types.LLVMBuilderRef = core.LLVMCreateBuilder();
core.LLVMPositionBuilderAtEnd(builder, entry);
const arg1: types.LLVMValueRef = core.LLVMGetParam(sum_func, 0);
const arg2: types.LLVMValueRef = core.LLVMGetParam(sum_func, 1);
const sum: types.LLVMValueRef = core.LLVMBuildAdd(builder, arg1, arg2, "sum");
_ = core.LLVMBuildRet(builder, sum);
// Dump the LLVM module to stdout
core.LLVMDumpModule(module);
// Clean up LLVM resources
core.LLVMDisposeBuilder(builder);
core.LLVMDisposeModule(module);
core.LLVMShutdown();

@kassane kassane closed this as completed Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant