Skip to content

Commit

Permalink
std::strings: add StrBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Apr 23, 2024
1 parent b1e1cb4 commit 887dff0
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 153 deletions.
71 changes: 36 additions & 35 deletions src/julec/compile.jule
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use build for std::jule::build::{
}
use types for std::jule::types
use std::process::{ProcessError, Cmd}
use strings for std::strings
use strings for std::strings::{StrBuilder}

static mut OUT_DIR = "dist"
static mut OUT_NAME = "ir.cpp"
Expand Down Expand Up @@ -114,61 +114,62 @@ fn is_cpp_source_file(path: str): bool {
ret is_valid_cpp_ext(path[offset:])
}

fn push_comp_cmd_clang(mut &cmd: str) {
fn push_comp_cmd_clang(mut &cmd: StrBuilder) {
// Disable all warnings.
cmd += "-Wno-everything "
cmd.write_str("-Wno-everything ")

// Set C++ standard.
cmd += "--std="
cmd.write_str("--std=")
match env::CPP_STD {
| "cpp14":
cmd += "c++14"
cmd.write_str("c++14")
| "cpp17":
cmd += "c++17"
cmd.write_str("c++17")
| "cpp20":
cmd += "c++20"
cmd.write_str("c++20")
}
cmd += " "
cmd.write_byte(' ')

if env::PRODUCTION {
cmd += "-O3 " // Enable all optimizations.
cmd += "-flto " // Enable LTO.
cmd += "-DNDEBUG " // Define NDEBUG, turn off assertions.
cmd += "-fomit-frame-pointer " // Do not use frame pointer.
cmd.write_str("-O3 ") // Enable all optimizations.
cmd.write_str("-flto ") // Enable LTO.
cmd.write_str("-DNDEBUG ") // Define NDEBUG, turn off assertions.
cmd.write_str("-fomit-frame-pointer ") // Do not use frame pointer.
} else {
cmd += "-O0 " // No optimization.
cmd.write_str("-O0 ") // No optimization.
}
}

fn push_comp_cmd_gcc(mut &cmd: str) {
fn push_comp_cmd_gcc(mut &cmd: StrBuilder) {
// Disable all warnings.
cmd += "-w "
cmd.write_str("-w ")

// Set C++ standard.
cmd += "--std="
cmd.write_str("--std=")
match env::CPP_STD {
| "cpp14":
cmd += "c++14"
cmd.write_str("c++14")
| "cpp17":
cmd += "c++17"
cmd.write_str("c++17")
| "cpp20":
cmd += "c++20"
cmd.write_str("c++20")
}
cmd += " "
cmd.write_byte(' ')

if env::PRODUCTION {
cmd += "-O3 " // Enable all optimizations.
cmd += "-DNDEBUG " // Define NDEBUG, turn off assertions.
cmd += "-fomit-frame-pointer " // Do not use frame pointer.
cmd.write_str("-O3 ") // Enable all optimizations.
cmd.write_str("-DNDEBUG ") // Define NDEBUG, turn off assertions.
cmd.write_str("-fomit-frame-pointer ") // Do not use frame pointer.
} else {
cmd += "-O0 " // No optimization.
cmd.write_str("-O0 ") // No optimization.
}
}

// Generate compile command for backend-compiler.
fn gen_compile_cmd(source_path: str, &ir: &IR): (str, str) {
let &compiler = env::COMPILER_PATH
let mut cmd = ""
let mut cmd = StrBuilder.new()
cmd.grow(1 << 6)

match env::COMPILER {
| "gcc":
Expand All @@ -179,26 +180,26 @@ fn gen_compile_cmd(source_path: str, &ir: &IR): (str, str) {

// Push passes.
for _, pass in ir.passes {
cmd += pass
cmd += " "
cmd.write_str(pass)
cmd.write_byte(' ')
}

// Push linked source files.
for _, u in ir.used {
if u.cpp_linked && is_cpp_source_file(u.path) {
cmd += u.path
cmd += " "
cmd.write_str(u.path)
cmd.write_byte(' ')
}
}

if OUT != "" {
cmd += "-o "
cmd += OUT
cmd += " "
cmd.write_str("-o ")
cmd.write_str(OUT)
cmd.write_byte(' ')
}
cmd += source_path
cmd.write_str(source_path)

ret compiler, cmd
ret compiler, cmd.to_str()
}

fn get_compile_path(): str {
Expand Down Expand Up @@ -422,7 +423,7 @@ fn compile_command(mut &args: []str) {

let path = get_compile_path()
let mut file = open_output(path)
file.write(oc.obj) else {
file.write(oc.obj.buffer()) else {
throw("object code could not write")
}
file.close()!
Expand Down
22 changes: 15 additions & 7 deletions src/julec/obj/cxx/derive.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
// license that can be found in the LICENSE file.

use std::jule::sema::{Struct}
use std::strings::{StrBuilder}

struct DeriveCoder {}

impl DeriveCoder {
static fn clone_func_decl(&s: &Struct): str {
let mut obj = TypeCoder.structure(s)
obj += " clone(void) const "
ret obj
let mut obj = StrBuilder.new()
obj.grow(30 + 19)
obj.write_str(TypeCoder.structure(s))
obj.write_str(" clone(void) const ")
ret obj.to_str()
}

static fn clone_func_def(&s: &Struct): str {
let mut obj = TypeCoder.structure(s)
obj += " " + obj
obj += "::clone(void) const "
ret obj
let t = TypeCoder.structure(s)

let mut obj = StrBuilder.new()
obj.grow(60 + 20)
obj.write_str(t)
obj.write_byte(' ')
obj.write_str(t)
obj.write_str("::clone(void) const ")
ret obj.to_str()
}
}
19 changes: 10 additions & 9 deletions src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use types for std::jule::types::{
MAX_U64,
}
use math for std::math
use strings for std::strings
use strings for std::strings::{StrBuilder}
use utf8 for std::unicode::utf8

// Ignore expression for std::tie function.
Expand Down Expand Up @@ -900,9 +900,9 @@ impl ExprCoder {
}

fn push_to_slice(mut &self, mut m: &PushToSliceExprModel) {
let n = self.oc.obj.len
let n = self.oc.obj.len()
self.model(m.dest)
let dest = self.oc.obj[n:]
let dest = self.oc.obj.buffer()[n:]

self.oc.write(" = jule::alloc_for_append(")
self.oc.write_bytes(dest)
Expand Down Expand Up @@ -970,12 +970,12 @@ impl ExprCoder {
self.oc.write(m.code)
ret
}
let n = self.oc.obj.len
let n = self.oc.obj.len()
let mut args = make([]any, m.exprs.len)
for (i, mut expr) in m.exprs {
self.expr(expr)
args[i] = str(self.oc.obj[n:])
self.oc.obj = self.oc.obj[:n]
args[i] = str(self.oc.obj.buffer()[n:])
self.oc.obj.shrink(n)
}
self.oc.write(fmt::format(m.code, args...))
}
Expand Down Expand Up @@ -1153,11 +1153,12 @@ fn sbtoa(b: byte): str {
}

fn cstr_bytes(bytes: []byte): str {
let mut lit = ""
let mut lit = StrBuilder.new()
lit.grow(bytes.len)
for _, b in bytes {
lit += sbtoa(b)
lit.write_str(sbtoa(b))
}
ret lit
ret lit.to_str()
}

fn cstr_lit(bytes: []byte): str {
Expand Down
91 changes: 50 additions & 41 deletions src/julec/obj/cxx/ident.jule
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::jule::sema::{
Var,
Param,
}
use std::strings::{StrBuilder}

// Identifier of initialize function caller function.
const INIT_CALLER_IDENT = "__jule_call_initializers"
Expand All @@ -31,18 +32,19 @@ impl IdentCoder {
// - ident: Identifier.
// - addr: Pointer address of package file handler.
static fn to_out(&ident: str, addr: uintptr): str {
let mut obj = StrBuilder.new()
if addr != 0 {
let mut obj = "" //make(str, 40)
obj += "_"
obj += conv::fmt_uint(u64(addr), 0xF)
obj += "_"
obj += ident
ret obj
obj.grow(40)
obj.write_byte('_')
obj.write_str(conv::fmt_uint(u64(addr), 0xF))
obj.write_byte('_')
obj.write_str(ident)
ret obj.to_str()
}
let mut obj = ""//make(str, ident.len + 1)
obj += "_"
obj += ident
ret obj
obj.grow(ident.len + 1)
obj.write_byte('_')
obj.write_str(ident)
ret obj.to_str()
}

// Returns cpp output local identifier form of fiven identifier.
Expand All @@ -52,13 +54,14 @@ impl IdentCoder {
// - col: Column of definition.
// - ident: Identifier of definition.
static fn to_local(row: int, col: int, &ident: str): str {
let mut obj = "" //make(str, 40)
obj += "_"
obj += conv::itoa(row)
obj += conv::itoa(col)
obj += "_"
obj += ident
ret obj
let mut obj = StrBuilder.new()
obj.grow(40)
obj.write_byte('_')
obj.write_str(conv::itoa(row))
obj.write_str(conv::itoa(col))
obj.write_byte('_')
obj.write_str(ident)
ret obj.to_str()
}

// Returns output identifier of function.
Expand Down Expand Up @@ -161,49 +164,55 @@ impl IdentCoder {

// Returns begin label identifier of iteration.
static fn iter_begin(it: uintptr): str {
let mut obj = "" //make(str, 30)
obj += "_iter_begin_"
obj += conv::fmt_uint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_iter_begin_")
obj.write_str(conv::fmt_uint(u64(it), 0xF))
ret obj.to_str()
}

// Returns end label identifier of iteration.
static fn iter_end(it: uintptr): str {
let mut obj = ""//make(str, 30)
obj += "_iter_end_"
obj += conv::fmt_uint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_iter_end_")
obj.write_str(conv::fmt_uint(u64(it), 0xF))
ret obj.to_str()
}

// Returns next label identifier of iteration.
static fn iter_next(it: uintptr): str {
let mut obj = ""//make(str, 30)
obj += "_iter_next_"
obj += conv::fmt_uint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_iter_next_")
obj.write_str(conv::fmt_uint(u64(it), 0xF))
ret obj.to_str()
}

// Returns label identifier.
static fn label(&ident: str): str {
let mut obj = ""//make(str, 30)
obj += "_julec_label_"
obj += ident
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_julec_label_")
obj.write_str(ident)
ret obj.to_str()
}

// Returns end label identifier of match-case.
static fn match_end(m: uintptr): str {
let mut obj = ""//make(str, 30)
obj += "_match_end_"
obj += conv::fmt_uint(u64(m), 0xF)
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_match_end_")
obj.write_str(conv::fmt_uint(u64(m), 0xF))
ret obj.to_str()
}

// Returns begin label identifier of case.
static fn case_begin(c: uintptr): str {
let mut obj = ""//make(str, 30)
obj += "_case_begin_"
obj += conv::fmt_uint(u64(c), 0xF)
ret obj
let mut obj = StrBuilder.new()
obj.grow(30)
obj.write_str("_case_begin_")
obj.write_str(conv::fmt_uint(u64(c), 0xF))
ret obj.to_str()
}
}
Loading

0 comments on commit 887dff0

Please sign in to comment.