Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Willyboar committed May 16, 2023
1 parent 560ddfb commit 7cad696
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: erlef/[email protected]
with:
otp-version: "25.2"
gleam-version: "0.28.3"
rebar3-version: "3"
# elixir-version: "1.14.2"
- run: gleam format --check src test
- run: gleam deps download
- run: gleam test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.beam
*.ez
build
erl_crash.dump
.DS_Store
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# glove



## Don't Use it (WIP)

[![Package Version](https://img.shields.io/hexpm/v/glove)](https://hex.pm/packages/glove)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/glove/)

A Gleam project

## Quick start

```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```

## Installation

If available on Hex this package can be added to your Gleam project:

```sh
gleam add glove
```

and its documentation can be found at <https://hexdocs.pm/glove>.
16 changes: 16 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name = "glove"
version = "0.1.0"
description = "A Gleam project"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]

[dependencies]
gleam_stdlib = "~> 0.28"

[dev-dependencies]
gleeunit = "~> 0.10"
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.28.2", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "088E334A00C9530D4B194F31406E51B6E4E4697C6A380D11591E60CC2F239724" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
]

[requirements]
gleam_stdlib = "~> 0.28"
gleeunit = "~> 0.10"
298 changes: 298 additions & 0 deletions src/glove.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
import gleam/int

// QBE Comparison Operators
pub type Comp {
// Less Than
Slt
// Less or Equal
Sle
// Greater than
Sgt
// Greater or equal
Sge
// Equal
Eq
// Not equal
Ne
}

pub type Option {
Option(Value)
}

// QBE instruction
pub type Inst {
// Adds values
Add(Value, Value)
// Substracts value(b) from value(a)
Sub(Value, Value)
// Multiplies values
Mul(Value, Value)
// Divides value(a) by value(b)
Div(Value, Value)
// Returns a remaider from division
Rem(Value, Value)
// Perform a Comparison
Comp(Type, Comp, Value, Value)
// Bitwise AND
And(Value, Value)
// Bitwise OR
Or(Value, Value)
// Copies either temporary or literal value
Copy(Value)
// Return from a function, optionally with a value
Ret(Option)
// Jumps to first label if a value is nonzero or to the second one otherwise
Jnz(Value, String, String)
// Unconditionally jumps to a label
Jmp(String)
// Calls a function
Call(String, #(Type, Value))
// Allocates a 4-byte aligned area on the stack
Alloc4(Int)
// Allocates a 8-byte aligned area on the stack
Alloc8(Int)
// Allocates a 16-byte aligned area on the stack
Alloc16(Int)
// Stores a value into memory pointed to by destination.
// (type, destination, value)
Store(Type, Value, Value)
// Loads a value from memory pointed to by source
// (type, source)
Load(Type, Value)
// (source, destination, n)
//
// Copy `n` bytes from the source address to the destination address.
//
// n must be a constant value.
//
// Minimum supported QBE version 1.1
Blit(Value, Value, Int)
}

// Display function for Instructions
pub fn display_inst() -> String {
todo
}

// QBE Value for instructions
pub type Value {
// `%`-temporary
Temporary(name: String)
// `$`-global
Global(name: String)
// Constant
Const(value: Int)
}

// Display Value function
pub fn display_value(value: Value) -> String {
case value {
Temporary(name) -> "%{" <> name <> "}"
Global(name) -> "${" <> name <> "}"
Const(value) -> "{" <> int.to_string(value) <> "}"
}
}

// QBE Types
pub type Type {
// Base Types
Word
Long
Single
Double
// Extended Types
Byte
Halfword
// Agreegate type with a specified name
Agreegate(TypeDef)
}

// Display Type function
pub fn display_type(ty: Type) -> String {
case ty {
Byte -> "b"
Halfword -> "h"
Word -> "w"
Long -> "l"
Single -> "s"
Double -> "d"
Agreegate(TypeDef) -> display_type_def(TypeDef)
}
}

// Returns a C ABI type. Extended types are converted to closest base
// types
pub fn into_abi() -> Nil {
todo
}

// Returns the closest base type
pub fn into_base() -> Nil {
todo
}

// Returns byte size for values of the type
pub fn size() -> Int {
todo
}

// QBE data definition
pub type DataDef {
DataDef(linkage: Linkage, name: String, align: Int, items: #(Type, DataItem))
}

pub fn new_datadef() -> DataDef {
todo
}

// Display function for Datadef
pub fn display_datadef() -> String {
todo
}

// QBE aggregate type definition
pub type TypeDef {
TypeDef(name: String, align: Option, items: #(Type, Int))
}

// Display function for TypeDef
pub fn display_type_def() -> Nil {
todo
}

// QBE Data definition item
pub type DataItem {
// Symbol and offset
Symbol(String, Int)
// String
Str(String)
//
Constant(Int)
}

// Display function for DataItem
pub fn display_data_item() -> String {
todo
}

// IR Statement
pub type Statement {
Assign(Value, Type, Inst)
Volatile(Inst)
}

// Display function for Statement
pub fn display_statement() -> String {
todo
}

// Function block with a label
pub type Block {
Block(label: String, statements: #(Statement))
}

// Display function for block
pub fn display_block() -> String {
todo
}

/// Adds a new instruction to the block
pub fn add_inst() -> Nil {
todo
}

/// Adds a new instruction assigned to a temporary
pub fn assign_inst() -> Nil {
todo
}

/// Returns true if the block's last instruction is a jump
pub fn jumps() -> Bool {
todo
}

// QBE Function
pub type Function {
Function(
linkage: Linkage,
name: String,
arguments: #(Type, Value),
return_ty: Option(Type),
blocks: #(Block),
)
}

// Display function for functions
pub fn display_function() -> String {
todo
}

// Instantiates an empty function and returns it
pub fn new_function() -> Function {
todo
}

// Adds a new empty block with a specified label and returns
// a reference to it
pub fn add_block() -> Block {
todo
}

// Returns a reference to the last block
pub fn last_block() -> Nil {
todo
}

// Adds a new instruction to the last block
pub fn add_instr() -> Nil {
todo
}

// Adds a new instruction assigned to a temporary
pub fn assign_instr() -> Nil {
todo
}

// Linkage of a function or data defintion (e.g. section and
// private/public status)
pub type Linkage {
Linkage(exported: Bool, section: Option(String), secflags: Option(String))
}

// Returns the default configuration for private linkage
pub fn private() -> Linkage {
todo
}

// Returns the configuration for private linkage with a provided section
pub fn private_with_section() -> Nil {
todo
}

/// A complete IL file
pub type Module {
Module(functions: #(Function), types: #(TypeDef), data: #(DataDef))
}

// Creates a new module
pub fn new_module() -> Module {
Module(functions: #(), types: #(), data: #())
}

// Display function for Module
pub fn display_module(module: Module) -> String {
todo
}

pub fn add_function(module: Module, function: Function) -> Module {
todo
}

pub fn add_type(module: Module, type_def: TypeDef) -> Module {
todo
}

pub fn add_data(module: Module, data_def: DataDef) -> Module {
todo
}
Loading

0 comments on commit 7cad696

Please sign in to comment.