Skip to content

Commit

Permalink
compiler: replace make(str) calls with StrBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 2, 2024
1 parent cdb1928 commit 24d542a
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 181 deletions.
70 changes: 35 additions & 35 deletions src/julec/compile.jule
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 OutDir = "dist"
static mut OutName = "ir.cpp"
Expand Down Expand Up @@ -104,61 +104,61 @@ fn isCppSourceFile(path: str): bool {
ret IsValidCppExt(path[offset:])
}

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

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

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.WriteStr("-O3 ") // Enable all optimizations.
cmd.WriteStr("-flto ") // Enable LTO.
cmd.WriteStr("-DNDEBUG ") // Define NDEBUG, turn off assertions.
cmd.WriteStr("-fomit-frame-pointer ") // Do not use frame pointer.
} else {
cmd += "-O0 " // No optimization.
cmd.WriteStr("-O0 ") // No optimization.
}
}

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

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

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.WriteStr("-O3 ") // Enable all optimizations.
cmd.WriteStr("-DNDEBUG ") // Define NDEBUG, turn off assertions.
cmd.WriteStr("-fomit-frame-pointer ") // Do not use frame pointer.
} else {
cmd += "-O0 " // No optimization.
cmd.WriteStr("-O0 ") // No optimization.
}
}

// Generate compile command for backend-compiler.
fn genCompileCmd(sourcePath: str, &ir: &IR): (str, str) {
let &compiler = env::CompilerPath
let mut cmd = ""//make(str, 0, 1 << 6)
let mut cmd = StrBuilder.New(1 << 6)

match env::Compiler {
| "gcc":
Expand All @@ -170,30 +170,30 @@ fn genCompileCmd(sourcePath: str, &ir: &IR): (str, str) {
// Push binded source files.
for _, u in ir.Used {
if u.Binded && isCppSourceFile(u.Path) {
cmd += u.Path
cmd += " "
cmd.WriteStr(u.Path)
cmd.WriteByte(' ')
}
}

if Out != "" {
cmd += "-o "
cmd += Out
cmd += " "
cmd.WriteStr("-o ")
cmd.WriteStr(Out)
cmd.WriteByte(' ')
}
cmd += sourcePath
cmd.WriteStr(sourcePath)

// Push passes.
for _, pass in ir.Passes {
cmd += " "
cmd += pass
cmd.WriteByte(' ')
cmd.WriteStr(pass)
}

// Link necessary libraries for Windows.
if build::Os == build::DistOs.Windows {
cmd += " -lshell32"
cmd.WriteStr(" -lshell32")
}

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

fn getCompilePath(): str {
Expand Down
8 changes: 4 additions & 4 deletions src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use types for std::jule::types::{
MaxU64,
}
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 @@ -1537,11 +1537,11 @@ fn sbtoa(b: byte): str {
}

fn cstrBytes(bytes: []byte): str {
let mut lit = ""//make(str, 0, len(bytes))
let mut lit = StrBuilder.New(len(bytes))
for _, b in bytes {
lit += sbtoa(b)
lit.WriteStr(sbtoa(b))
}
ret lit
ret lit.Str()
}

fn cstrLit(bytes: []byte): str {
Expand Down
91 changes: 46 additions & 45 deletions src/julec/obj/cxx/ident.jule
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::jule::sema::{
Param,
}
use utf8 for std::unicode::utf8
use std::strings::{StrBuilder}

// Identifier of initialize function caller function.
const initCallerIdent = "__jule_call_initializers"
Expand All @@ -29,26 +30,26 @@ impl identCoder {
// Write identifiers to buf. If identifier contains unicode runes,
// handle as ASCII characters. Some backend compilers are not supports
// unicode identifiers and causes compile errors.
static fn writeIdentTo(mut &buf: str, &ident: str) {
static fn writeIdentTo(mut &buf: StrBuilder, &ident: str) {
for _, b in ident {
if b >= utf8::RuneSelf {
// ident contains unicode runes.
goto unicode
}
}
// ident is ASCII, append directly.
buf += ident
buf.WriteStr(ident)
ret
unicode:
for _, r in []rune(ident) {
if r < utf8::RuneSelf { // ASCII
// Cast to byte to enable compiler optimization.
// Append directy to buf.
buf += str(byte(r))
buf.WriteByte(byte(r))
continue
}
// Unicode, handle as ASCII.
buf += conv::FmtInt(i64(r), 0xF)
buf.WriteStr(conv::FmtInt(i64(r), 0xF))
}
}

Expand All @@ -59,18 +60,17 @@ impl identCoder {
// - addr: Pointer address of package file handler.
static fn toOut(&ident: str, addr: uintptr): str {
if addr != 0 {
let mut obj = ""//make(str, 0, 40)
obj += "_"
obj += conv::FmtUint(u64(addr), 0xF)
obj += "_"
let mut obj = StrBuilder.New(40)
obj.WriteByte('_')
obj.WriteStr(conv::FmtUint(u64(addr), 0xF))
obj.WriteByte('_')
identCoder.writeIdentTo(obj, ident)
ret obj
ret obj.Str()
}
let mut obj = ""//make(str, 0, len(ident) + 1)
obj += "_"
let mut obj = StrBuilder.New(len(ident) + 1)
obj.WriteByte('_')
identCoder.writeIdentTo(obj, ident)
obj += ident
ret obj
ret obj.Str()
}

// Returns cpp output local identifier form of fiven identifier.
Expand All @@ -80,13 +80,13 @@ impl identCoder {
// - col: Column of definition.
// - ident: Identifier of definition.
static fn toLocal(row: int, col: int, &ident: str): str {
let mut obj = ""//make(str, 0, 40)
obj += "_"
obj += conv::Itoa(row)
obj += conv::Itoa(col)
obj += "_"
let mut obj = StrBuilder.New(40)
obj.WriteByte('_')
obj.WriteStr(conv::Itoa(row))
obj.WriteStr(conv::Itoa(col))
obj.WriteByte('_')
identCoder.writeIdentTo(obj, ident)
ret obj
ret obj.Str()
}

// Returns output identifier of function.
Expand Down Expand Up @@ -164,9 +164,10 @@ impl identCoder {
if f.Owner.Binded {
ret f.Ident
}
let mut obj = "_field_"
let mut obj = StrBuilder.New(7 + len(f.Ident))
obj.WriteStr("_field_")
identCoder.writeIdentTo(obj, f.Ident)
ret obj
ret obj.Str()
}

// Returns output identifier of variable.
Expand All @@ -193,49 +194,49 @@ impl identCoder {

// Returns begin label identifier of iteration.
static fn iterBegin(it: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_iter_begin_"
obj += conv::FmtUint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_iter_begin_")
obj.WriteStr(conv::FmtUint(u64(it), 0xF))
ret obj.Str()
}

// Returns end label identifier of iteration.
static fn iterEnd(it: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_iter_end_"
obj += conv::FmtUint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_iter_end_")
obj.WriteStr(conv::FmtUint(u64(it), 0xF))
ret obj.Str()
}

// Returns next label identifier of iteration.
static fn iterNext(it: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_iter_next_"
obj += conv::FmtUint(u64(it), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_iter_next_")
obj.WriteStr(conv::FmtUint(u64(it), 0xF))
ret obj.Str()
}

// Returns label identifier.
static fn label(u: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_julec_label_"
obj += conv::FmtUint(u64(u), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_julec_label_")
obj.WriteStr(conv::FmtUint(u64(u), 0xF))
ret obj.Str()
}

// Returns end label identifier of match-case.
static fn matchEnd(m: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_match_end_"
obj += conv::FmtUint(u64(m), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_match_end_")
obj.WriteStr(conv::FmtUint(u64(m), 0xF))
ret obj.Str()
}

// Returns begin label identifier of case.
static fn caseBegin(c: uintptr): str {
let mut obj = ""//make(str, 0, 30)
obj += "_case_begin_"
obj += conv::FmtUint(u64(c), 0xF)
ret obj
let mut obj = StrBuilder.New(30)
obj.WriteStr("_case_begin_")
obj.WriteStr(conv::FmtUint(u64(c), 0xF))
ret obj.Str()
}
}
Loading

0 comments on commit 24d542a

Please sign in to comment.