diff --git a/TellC/CMakeLists.txt b/TellC/CMakeLists.txt index c238833..b39b8a0 100644 --- a/TellC/CMakeLists.txt +++ b/TellC/CMakeLists.txt @@ -88,6 +88,12 @@ if(BUILD_TESTING) "${CMAKE_CURRENT_SOURCE_DIR}/tests/standalone_macos.py" "$" "${CMAKE_CURRENT_SOURCE_DIR}/examples/objects.tell") + add_library(tell_test_cpp_static STATIC tests/cpp_library.cpp) + add_library(tell_test_cpp_dynamic SHARED tests/cpp_library.cpp) + set_target_properties(tell_test_cpp_dynamic PROPERTIES BUILD_WITH_INSTALL_NAME_DIR ON INSTALL_NAME_DIR "${CMAKE_CURRENT_BINARY_DIR}") + add_test(NAME cpp_integration COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tests/cpp_integration.py" "$" + "$" "$") endif() add_executable(tell_parser_recursion_test tests/recursion.cpp) diff --git a/TellC/README.md b/TellC/README.md index a07247e..b2991ce 100644 --- a/TellC/README.md +++ b/TellC/README.md @@ -13,7 +13,7 @@ say name Newlines terminate statements; semicolons separate statements on the same line. Keywords and built-in type names are case insensitive. Object type names are case sensitive. -The parser recognizes typed declarations, assignment statements, output, returns, functions, if/otherwise, while, literals, lists, calls, member and index access, and common symbolic and word operators. The executable backend intentionally implements a smaller subset. Syntax outside the parser subset is reported as a parse error; syntax represented by the parser but not yet implemented by the backend is rejected during compilation with a source-located diagnostic. Natural-language comparison phrases, classes, imports, and named call arguments are not currently supported. Strings support basic escapes; source columns count bytes. +The parser recognizes typed declarations, assignment statements, output, returns, functions, if/otherwise, while, repeat, for each, literals, lists, calls, member and index access, and common symbolic and word operators. The executable backend intentionally implements a smaller subset. Syntax outside the parser subset is reported as a parse error; syntax represented by the parser but not yet implemented by the backend is rejected during compilation with a source-located diagnostic. Natural-language comparison phrases, classes, imports, and named call arguments are not currently supported. Strings support basic escapes; source columns count bytes. **Compiled functions and methods are strongly typed.** Parameters must use `type: name` and every function must declare its result with `out type` (use `out void` for no result). For example: @@ -60,23 +60,58 @@ Run parser and compiler integration tests with `ctest --test-dir + +extern "C" int64_t library_double(int64_t value) { + return value * 2; +} +``` + +Declare the entry point in Tell and pass the library to `tellc`: ```tell -extern c function add(int32 left, int32 right) returns int32 as "c_add" -extern swift function double_value(float64 value) returns float64 as "swift_double_value" -let number result be add(2, 3) +extern cpp function twice(int64 value) returns int64 as "library_double" +say twice(21) ``` -The optional `as` string gives the exact external symbol; without it, the Tell name is used. Both forms describe a **C ABI** function: a Swift implementation needs an explicitly exported C-compatible entry point. Accepted ABI types are `int32`, `int64`, `float32`, and `float64`, with `void` allowed as a return type. These represent C `int32_t`, `int64_t`, `float`, and `double`. Pointers, strings, structures, callbacks, variadic functions, ownership, and Swift-native calling conventions are not supported by these declarations. +```sh +tellc program.tell --link /path/to/library.a -o program +``` -This is parser and AST support only. There is no foreign function type checking, conversion policy between Tell and ABI types, foreign library linking, or foreign call code generation yet; declaring a function does not make calls executable. Those pieces must be implemented before C or Swift interoperability works at runtime. +Repeat `--link` for additional libraries. On Apple silicon, the embedded linker +accepts ARM64 Mach-O object files, static archives (`.a`), and dynamic libraries +(`.dylib`). The library must already be built for a compatible macOS and CPU; +tellc does not compile C++ source. Static libraries are incorporated into the +executable. Dynamic libraries remain runtime dependencies, so their install +names and any of their own dependencies must be available on the target Mac. +The linker resolves macOS system and C++ runtime symbols from libraries available +on the compiling Mac without requiring a separately installed SDK. A library +or system symbol introduced after macOS 11 can require a newer macOS release. + +`extern c`, `extern cpp`, and `extern swift` all declare **C ABI** functions. +Use a C-compatible exported wrapper for C++ or Swift; C++ name mangling, classes, +methods, templates, exceptions crossing the boundary, pointers, strings, +structures, callbacks, and variadic functions are not supported. Catch C++ +exceptions inside the wrapper. The optional `as` string specifies the exact +symbol name; without it, the Tell name is used. + +Arguments and results can be `int32`, `int64`, `float32`, or `float64`; `void` +is also allowed as a return type. Tell `number` maps to signed integers, and +`decimal` maps to floating-point values. An `int32` argument outside its range +terminates the generated program; `float32` arguments are rounded to single +precision. Returned `int32` and `float32` values are widened to Tell types. +Tell checks argument counts and Tell types, but the declaration must match the +actual library's ABI exactly. ## Executable backend -The backend currently supports typed declarations, output with `{name}` interpolation, typed functions and methods, calls, returns, object construction, field access, equality, signed integer arithmetic, and `if` blocks. `number` is a signed 64-bit integer and `decimal` is a 64-bit floating-point value. Text equality compares contents; object equality compares identity. Assignment, loops, decimal arithmetic, lists/indexing, unary operators, and foreign calls still require code generation support. +The backend currently supports typed declarations, output with `{name}` interpolation, typed functions and methods, calls, returns, object construction, field access, equality, signed integer arithmetic, `if` blocks, variable reassignment, typed lists and vectors, indexed reads and writes, and `while`, `repeat`, and `for each` loops. `number` is a signed 64-bit integer and `decimal` is a 64-bit floating-point value. Text equality compares contents; object equality compares identity. Decimal arithmetic and unary operators still require code generation support. Dictionaries, tuples, and sets are not yet implemented. ## Objects and typed function signatures @@ -103,3 +138,89 @@ say laptop.check password("wrong") This program prints `true` and then `false`. `MacBook()` constructs an instance using the field initializers. Initializers run in declaration order without a `self` receiver. Instances live until the process exits. Inside methods, use `self.password` for explicit access or `password` when a local name does not shadow it. Parameters use `type: name` and are separated by commas. `out` declares the return type, including `out void` for functions without a result. Arguments, initializers, and return values must match the declared types. A function with a result must return a value on every path. The parser still accepts the legacy `to name with parameter:` form for parser compatibility, but the executable backend intentionally rejects untyped function signatures. + +## Switch + +A switch evaluates its expression once and executes the first matching case. +Cases use the same type and equality rules as `=`, and their values may be +expressions. If no case matches, execution continues after the switch. +Indent cases beneath the switch; case bodies may be inline or indented on +following lines. An `end switch` at the switch's indentation is optional. + +```tell +let number water_amount be 50 +switch water_amount: + if it is 0: say "tank is empty" + if it is 25: say "Quarter of the tank is full" + if it is 50: say "Half the tank is full" + if it is 75: say "3/4 of the tank is full" + if it is 100: say "Tank is full" +``` + +## Lists, vectors, and for each + +Lists and vectors are names for the same fixed-length, homogeneous collection type. +Use `list of TYPE` or `vector of TYPE` anywhere a type is accepted, including +function signatures and object fields. They may be nested, and element types may +be primitive types or objects. Literals evaluate their elements from left to right. +An empty literal needs a collection type from its declaration, argument, return, +or enclosing literal. Collection values are references and remain allocated +until the process exits; equality compares identity. + +```tell +let vector of number scores be [10, 20, 30] +say scores[0] # 10, indices start at zero +for each score in scores: + say score +end for +let list of text names be [] +``` + +An out-of-bounds index raises an error. `for each` evaluates its +collection expression once, visits elements in order, and runs zero times for an +empty collection. Its iteration variable is local to the loop; `break` exits the +innermost loop and `continue` advances it. An iteration variable cannot reuse an +existing local name. Collections cannot currently be resized, and +collection values cannot be printed directly. + +## Reassignment and loops + +Use `set name to expression` to reassign a local variable or parameter; types must match and constant bindings cannot be reassigned. List elements can be updated with `set values[index] to expression`, including nested indexes. Indexes are checked at runtime. + +`while condition:` repeats while a Boolean condition is true. `repeat count times:` evaluates its numeric count once and runs the body that many times (zero times for a nonpositive count). End them with `end while` and `end repeat`. `break` and `continue` operate on the innermost loop. Variables declared inside a loop body are local to that body. + +## Input, conversion, files, and errors + +`input()` reads one line as `text`, removing its line ending. An optional text +argument prints a prompt without a newline and flushes output before reading. +Convert the text explicitly with `number(text)` for a signed 64-bit integer or +`decimal(text)` for a 64-bit floating-point value: + +```tell +try: + let text answer be input("Age: ") + let number age be number(answer) + say age +if something goes wrong: + say error_message +end try +``` + +Conversion rejects empty input, trailing characters and out-of-range values. +It follows the C library's numeric parsing rules, including leading whitespace; +`decimal` also accepts the C library's infinity and NaN forms. End of input is an +error. `input` rejects NUL bytes. Returned text lives for the process lifetime. + +`write_file(path, text)` creates or truncates a text file, and +`append_file(path, text)` creates or appends to one. `read_file(path)` returns +its contents as `text`. Paths are relative to the program's working directory. +Reads require a seekable file and reject NUL bytes; these operations are for +text, not arbitrary binary data. File errors can be caught using `try`. + +`raise an error saying expression` raises a text message. A `try` block's +optional `if something goes wrong` handler receives it as `error_message`. +Errors propagate through compiled functions and methods to the nearest handler; +without one, the program writes an error to stderr and exits with status 1. +Conversions, input, file operations, and list index bounds produce catchable +errors. Memory allocation failures and errors from foreign libraries are not +caught. Typed catches, `finally`, and exception objects are not implemented. diff --git a/TellC/include/tell/parser.hpp b/TellC/include/tell/parser.hpp index 54c708f..7c6f674 100644 --- a/TellC/include/tell/parser.hpp +++ b/TellC/include/tell/parser.hpp @@ -20,7 +20,7 @@ struct Expr { struct Parameter { std::string type; std::string name; Location location; }; struct ForeignParameter { std::string type; std::string name; Location location; }; struct Stmt { - enum class Kind { Let, Set, Say, Check, Return, Expression, If, ElseIf, Else, While, Repeat, For, Function, Object, AccessSection, ForeignFunction, Break, Continue }; + enum class Kind { Let, Set, Say, Check, Return, Expression, If, ElseIf, Else, Switch, Case, While, Repeat, For, Function, Object, AccessSection, ForeignFunction, Break, Continue, Try, Raise }; Kind kind; Location location; std::string name; @@ -34,6 +34,7 @@ struct Stmt { bool constant = false; std::string type; std::unique_ptr expression; + std::unique_ptr target; std::vector body; std::vector branches; }; diff --git a/TellC/src/codegen.cpp b/TellC/src/codegen.cpp index 64735c6..c08396f 100644 --- a/TellC/src/codegen.cpp +++ b/TellC/src/codegen.cpp @@ -18,7 +18,7 @@ namespace tell { namespace { -struct Value { llvm::Value* value; std::string type; }; +struct Value { llvm::Value* value; std::string type; bool slot = false; bool constant = false; }; struct Routine { const Stmt* syntax; llvm::Function* function; @@ -50,10 +50,204 @@ class Generator { llvm::FunctionCallee printf_fn; std::map objects; std::map functions; + std::map foreign_functions; Value receiver{nullptr, ""}; + std::vector> loops; + bool is_list(const std::string& type) { return type.rfind("list of ", 0) == 0; } + llvm::StructType* list_layout() { return llvm::StructType::get(context, {ir.getInt64Ty(), ir.getPtrTy()}); } + llvm::AllocaInst* entry_alloca(llvm::Type* type) { + auto& entry = ir.GetInsertBlock()->getParent()->getEntryBlock(); +#if LLVM_VERSION_MAJOR >= 24 + llvm::IRBuilder<> builder(entry.begin()); +#else + llvm::IRBuilder<> builder(&entry, entry.begin()); +#endif + return builder.CreateAlloca(type); + } + llvm::Value* allocate(llvm::Value* size) { + auto signature = llvm::FunctionType::get(ir.getPtrTy(), {ir.getInt64Ty()}, false); + auto ptr = ir.CreateCall(module.getOrInsertFunction("malloc", signature), {size}); + auto fn = ir.GetInsertBlock()->getParent(); + auto ok = llvm::BasicBlock::Create(context, "allocated", fn); + auto bad = llvm::BasicBlock::Create(context, "out_of_memory", fn); + ir.CreateCondBr(ir.CreateIsNull(ptr), bad, ok); + ir.SetInsertPoint(bad); + ir.CreateCall(module.getOrInsertFunction("abort", llvm::FunctionType::get(ir.getVoidTy(), false))); + ir.CreateUnreachable(); + ir.SetInsertPoint(ok); + return ptr; + } const Routine* current_routine = nullptr; + llvm::GlobalVariable* pending_error = nullptr; + llvm::GlobalVariable* input_stream = nullptr; + std::vector handlers; + + void propagate_error() { + if (!handlers.empty()) { ir.CreateBr(handlers.back()); return; } + auto fn = ir.GetInsertBlock()->getParent(); + if (fn->getName() == "main") { + auto message = ir.CreateLoad(ir.getPtrTy(), pending_error); + auto dprintf_type = llvm::FunctionType::get(ir.getInt32Ty(), {ir.getInt32Ty(), ir.getPtrTy()}, true); + ir.CreateCall(module.getOrInsertFunction("dprintf", dprintf_type), {ir.getInt32(2), text("Error: %s\n"), message}); + ir.CreateRet(ir.getInt32(1)); + } else if (fn->getReturnType()->isVoidTy()) ir.CreateRetVoid(); + else ir.CreateRet(llvm::Constant::getNullValue(fn->getReturnType())); + } + void raise_if(llvm::Value* bad, const std::string& message) { + auto fn = ir.GetInsertBlock()->getParent(); + auto error = llvm::BasicBlock::Create(context, "error", fn); + auto ok = llvm::BasicBlock::Create(context, "ok", fn); + ir.CreateCondBr(bad, error, ok); + ir.SetInsertPoint(error); + ir.CreateStore(text(message), pending_error); + propagate_error(); + ir.SetInsertPoint(ok); + } + void check_pending_error() { + auto fn = ir.GetInsertBlock()->getParent(); + auto error = llvm::BasicBlock::Create(context, "call.error", fn); + auto ok = llvm::BasicBlock::Create(context, "call.ok", fn); + ir.CreateCondBr(ir.CreateIsNotNull(ir.CreateLoad(ir.getPtrTy(), pending_error)), error, ok); + ir.SetInsertPoint(error); + propagate_error(); + ir.SetInsertPoint(ok); + } + Value builtin_input(const Expr& expr) { + if (expr.children.size() > 2) fail(expr.location, "input takes zero or one argument"); + if (expr.children.size() == 2) { + auto prompt = expression(*expr.children[1]); + require_type(prompt, "text", expr.children[1]->location); + ir.CreateCall(printf_fn, {text("%s"), prompt.value}); + } + auto fn = ir.GetInsertBlock()->getParent(); + auto stream = ir.CreateLoad(ir.getPtrTy(), input_stream); + auto open = llvm::BasicBlock::Create(context, "input.open", fn); + auto ready = llvm::BasicBlock::Create(context, "input.ready", fn); + ir.CreateCondBr(ir.CreateIsNull(stream), open, ready); + ir.SetInsertPoint(open); + auto fdopen_type = llvm::FunctionType::get(ir.getPtrTy(), {ir.getInt32Ty(), ir.getPtrTy()}, false); + auto created = ir.CreateCall(module.getOrInsertFunction("fdopen", fdopen_type), {ir.getInt32(0), text("r")}); + ir.CreateStore(created, input_stream); + ir.CreateBr(ready); + ir.SetInsertPoint(ready); + stream = ir.CreateLoad(ir.getPtrTy(), input_stream); + raise_if(ir.CreateIsNull(stream), "cannot open standard input"); + auto buffer = entry_alloca(ir.getPtrTy()); + auto size = entry_alloca(ir.getInt64Ty()); + ir.CreateStore(llvm::ConstantPointerNull::get(ir.getPtrTy()), buffer); + ir.CreateStore(ir.getInt64(0), size); + auto fflush_type = llvm::FunctionType::get(ir.getInt32Ty(), {ir.getPtrTy()}, false); + ir.CreateCall(module.getOrInsertFunction("fflush", fflush_type), {llvm::ConstantPointerNull::get(ir.getPtrTy())}); + auto getline_type = llvm::FunctionType::get(ir.getInt64Ty(), {ir.getPtrTy(), ir.getPtrTy(), ir.getPtrTy()}, false); + auto length = ir.CreateCall(module.getOrInsertFunction("getline", getline_type), {buffer, size, stream}); + raise_if(ir.CreateICmpSLT(length, ir.getInt64(0)), "cannot read input"); + auto str = ir.CreateLoad(ir.getPtrTy(), buffer); + auto memchr_type = llvm::FunctionType::get(ir.getPtrTy(), {ir.getPtrTy(), ir.getInt32Ty(), ir.getInt64Ty()}, false); + auto nul = ir.CreateCall(module.getOrInsertFunction("memchr", memchr_type), {str, ir.getInt32(0), length}); + raise_if(ir.CreateIsNotNull(nul), "input contains NUL bytes"); + auto strip = llvm::BasicBlock::Create(context, "input.strip", fn); + auto done = llvm::BasicBlock::Create(context, "input.done", fn); + ir.CreateCondBr(ir.CreateICmpSGT(length, ir.getInt64(0)), strip, done); + ir.SetInsertPoint(strip); + auto last = ir.CreateGEP(ir.getInt8Ty(), str, ir.CreateSub(length, ir.getInt64(1))); + auto newline = ir.CreateICmpEQ(ir.CreateLoad(ir.getInt8Ty(), last), ir.getInt8(10)); + auto remove = llvm::BasicBlock::Create(context, "input.remove", fn); + ir.CreateCondBr(newline, remove, done); + ir.SetInsertPoint(remove); ir.CreateStore(ir.getInt8(0), last); + auto check_cr = llvm::BasicBlock::Create(context, "input.check_cr", fn); + ir.CreateCondBr(ir.CreateICmpSGT(length, ir.getInt64(1)), check_cr, done); + ir.SetInsertPoint(check_cr); + auto previous = ir.CreateGEP(ir.getInt8Ty(), str, ir.CreateSub(length, ir.getInt64(2))); + auto remove_cr = llvm::BasicBlock::Create(context, "input.remove_cr", fn); + ir.CreateCondBr(ir.CreateICmpEQ(ir.CreateLoad(ir.getInt8Ty(), previous), ir.getInt8(13)), remove_cr, done); + ir.SetInsertPoint(remove_cr); ir.CreateStore(ir.getInt8(0), previous); ir.CreateBr(done); + ir.SetInsertPoint(done); + return {str, "text"}; + } + Value builtin_file(const Expr& expr, const std::string& operation) { + auto argc = operation == "read_file" ? 1u : 2u; + if (expr.children.size() != argc + 1) fail(expr.location, operation + " has wrong number of arguments"); + auto path = expression(*expr.children[1]); + require_type(path, "text", expr.children[1]->location); + Value content{nullptr, ""}; + if (argc == 2) { + content = expression(*expr.children[2]); + require_type(content, "text", expr.children[2]->location); + } + auto ptr = ir.getPtrTy(); auto i64 = ir.getInt64Ty(); auto i32 = ir.getInt32Ty(); + auto fopen_fn = module.getOrInsertFunction("fopen", llvm::FunctionType::get(ptr, {ptr, ptr}, false)); + auto fclose_fn = module.getOrInsertFunction("fclose", llvm::FunctionType::get(i32, {ptr}, false)); + auto file = ir.CreateCall(fopen_fn, {path.value, text(operation == "read_file" ? "rb" : operation == "write_file" ? "wb" : "ab")}); + raise_if(ir.CreateIsNull(file), "cannot open file"); + auto file_error = [&](llvm::Value* bad, const std::string& message) { + auto fn = ir.GetInsertBlock()->getParent(); + auto error = llvm::BasicBlock::Create(context, "file.error", fn); + auto ok = llvm::BasicBlock::Create(context, "file.ok", fn); + ir.CreateCondBr(bad, error, ok); + ir.SetInsertPoint(error); ir.CreateCall(fclose_fn, {file}); + ir.CreateStore(text(message), pending_error); propagate_error(); + ir.SetInsertPoint(ok); + }; + if (argc == 2) { + auto strlen_fn = module.getOrInsertFunction("strlen", llvm::FunctionType::get(i64, {ptr}, false)); + auto length = ir.CreateCall(strlen_fn, {content.value}); + auto fwrite_fn = module.getOrInsertFunction("fwrite", llvm::FunctionType::get(i64, {ptr, i64, i64, ptr}, false)); + auto written = ir.CreateCall(fwrite_fn, {content.value, ir.getInt64(1), length, file}); + file_error(ir.CreateICmpNE(written, length), "cannot write file"); + auto closed = ir.CreateCall(fclose_fn, {file}); + raise_if(ir.CreateICmpNE(closed, ir.getInt32(0)), "cannot close file"); + return {nullptr, "void"}; + } + auto seek = module.getOrInsertFunction("fseek", llvm::FunctionType::get(i32, {ptr, i64, i32}, false)); + auto tell = module.getOrInsertFunction("ftell", llvm::FunctionType::get(i64, {ptr}, false)); + auto seek_end = ir.CreateCall(seek, {file, ir.getInt64(0), ir.getInt32(2)}); + file_error(ir.CreateICmpNE(seek_end, ir.getInt32(0)), "cannot read file"); + auto length = ir.CreateCall(tell, {file}); + file_error(ir.CreateOr(ir.CreateICmpSLT(length, ir.getInt64(0)), ir.CreateICmpEQ(length, ir.getInt64(INT64_MAX))), "cannot read file"); + auto seek_start = ir.CreateCall(seek, {file, ir.getInt64(0), ir.getInt32(0)}); + file_error(ir.CreateICmpNE(seek_start, ir.getInt32(0)), "cannot read file"); + auto data = allocate(ir.CreateAdd(length, ir.getInt64(1))); + auto fread_fn = module.getOrInsertFunction("fread", llvm::FunctionType::get(i64, {ptr, i64, i64, ptr}, false)); + auto bytes = ir.CreateCall(fread_fn, {data, ir.getInt64(1), length, file}); + file_error(ir.CreateICmpNE(bytes, length), "cannot read file"); + ir.CreateStore(ir.getInt8(0), ir.CreateGEP(ir.getInt8Ty(), data, length)); + auto closed = ir.CreateCall(fclose_fn, {file}); + raise_if(ir.CreateICmpNE(closed, ir.getInt32(0)), "cannot close file"); + auto strlen_fn = module.getOrInsertFunction("strlen", llvm::FunctionType::get(i64, {ptr}, false)); + raise_if(ir.CreateICmpNE(ir.CreateCall(strlen_fn, {data}), length), "file contains NUL bytes"); + return {data, "text"}; + } + Value builtin_number(const Expr& expr, const std::string& type) { + if (expr.children.size() != 2) fail(expr.location, type + " requires one argument"); + auto value = expression(*expr.children[1]); + require_type(value, "text", expr.children[1]->location); + auto end = entry_alloca(ir.getPtrTy()); +#if defined(__APPLE__) + auto errno_fn = "__error"; +#else + auto errno_fn = "__errno_location"; +#endif + auto errno_ptr = ir.CreateCall(module.getOrInsertFunction(errno_fn, llvm::FunctionType::get(ir.getPtrTy(), false))); + ir.CreateStore(ir.getInt32(0), errno_ptr); + llvm::Value* result; + if (type == "number") { + auto signature = llvm::FunctionType::get(ir.getInt64Ty(), {ir.getPtrTy(), ir.getPtrTy(), ir.getInt32Ty()}, false); + result = ir.CreateCall(module.getOrInsertFunction("strtoll", signature), {value.value, end, ir.getInt32(10)}); + } else { + auto signature = llvm::FunctionType::get(ir.getDoubleTy(), {ir.getPtrTy(), ir.getPtrTy()}, false); + result = ir.CreateCall(module.getOrInsertFunction("strtod", signature), {value.value, end}); + } + auto endptr = ir.CreateLoad(ir.getPtrTy(), end); + auto empty = ir.CreateICmpEQ(value.value, endptr); + auto trailing = ir.CreateICmpNE(ir.CreateLoad(ir.getInt8Ty(), endptr), ir.getInt8(0)); + auto range = ir.CreateICmpNE(ir.CreateLoad(ir.getInt32Ty(), errno_ptr), ir.getInt32(0)); + raise_if(ir.CreateOr(empty, ir.CreateOr(trailing, range)), "invalid " + type); + return {result, type}; + } + llvm::Type* llvm_type(const std::string& type, Location loc) { + if (is_list(type)) { llvm_type(type.substr(8), loc); return ir.getPtrTy(); } if (type == "text" || objects.count(type)) return ir.getPtrTy(); if (type == "number") return ir.getInt64Ty(); if (type == "decimal") return ir.getDoubleTy(); @@ -81,7 +275,11 @@ class Generator { Value lookup(const std::string& name, Location loc) { auto local = variables.find(name); - if (local != variables.end()) return local->second; + if (local != variables.end()) { + auto value = local->second; + if (value.slot) value.value = ir.CreateLoad(llvm_type(value.type, loc), value.value); + return value; + } if (receiver.value) { if (name == "self") return receiver; if (objects.at(receiver.type).fields.count(name)) return field(receiver, name, loc); @@ -105,6 +303,9 @@ class Generator { fail(callee.location, "method '" + callee.value + "' is private"); args.push_back(object.value); } else if (callee.kind == Expr::Kind::Name) { + if (callee.value == "input") return builtin_input(expr); + if (callee.value == "read_file" || callee.value == "write_file" || callee.value == "append_file") return builtin_file(expr, callee.value); + if (callee.value == "number" || callee.value == "decimal") return builtin_number(expr, callee.value); if (variables.count(callee.value)) fail(callee.location, "variable '" + callee.value + "' is not callable"); if (receiver.value) { @@ -119,8 +320,12 @@ class Generator { auto object = objects.find(callee.value); if (object != objects.end()) { if (expr.children.size() != 1) fail(expr.location, "object construction takes no arguments"); - return {ir.CreateCall(object->second.constructor), callee.value}; + auto instance = ir.CreateCall(object->second.constructor); + check_pending_error(); + return {instance, callee.value}; } + auto foreign = foreign_functions.find(callee.value); + if (foreign != foreign_functions.end()) return call_foreign(expr, *foreign->second); auto function = functions.find(callee.value); if (function == functions.end()) fail(callee.location, "unknown function '" + callee.value + "'"); routine = &function->second; @@ -133,16 +338,83 @@ class Generator { fail(expr.location, "wrong number of arguments to '" + callee.value + "'"); for (size_t i = 0; i < parameters.size(); ++i) { const auto& argument = *expr.children[i + 1]; - auto value = expression(argument); + auto value = expression(argument, parameters[i].type); require_type(value, parameters[i].type, argument.location); args.push_back(value.value); } - return {ir.CreateCall(routine->function, args), routine->syntax->return_type}; + auto result = ir.CreateCall(routine->function, args); + check_pending_error(); + return {result, routine->syntax->return_type}; + } + + llvm::Type* foreign_type(const std::string& type) { + if (type == "int32") return ir.getInt32Ty(); + if (type == "int64") return ir.getInt64Ty(); + if (type == "float32") return ir.getFloatTy(); + if (type == "float64") return ir.getDoubleTy(); + return ir.getVoidTy(); + } + + Value call_foreign(const Expr& expr, const Stmt& declaration) { + if (expr.children.size() - 1 != declaration.foreign_parameters.size()) + fail(expr.location, "wrong number of arguments to '" + declaration.name + "'"); + std::vector args; + for (size_t i = 0; i < declaration.foreign_parameters.size(); ++i) { + const auto& param = declaration.foreign_parameters[i]; + const auto& argument = *expr.children[i + 1]; + auto value = expression(argument); + bool integer = param.type == "int32" || param.type == "int64"; + require_type(value, integer ? "number" : "decimal", argument.location); + if (param.type == "int32") { + auto narrowed = ir.CreateTrunc(value.value, ir.getInt32Ty()); + auto fn = ir.GetInsertBlock()->getParent(); + auto valid = llvm::BasicBlock::Create(context, "foreign.int32.ok", fn); + auto invalid = llvm::BasicBlock::Create(context, "foreign.int32.overflow", fn); + ir.CreateCondBr(ir.CreateICmpEQ(value.value, ir.CreateSExt(narrowed, ir.getInt64Ty())), valid, invalid); + ir.SetInsertPoint(invalid); + ir.CreateCall(module.getOrInsertFunction("abort", llvm::FunctionType::get(ir.getVoidTy(), false))); + ir.CreateUnreachable(); + ir.SetInsertPoint(valid); + value.value = narrowed; + } else if (param.type == "float32") { + value.value = ir.CreateFPTrunc(value.value, ir.getFloatTy()); + } + args.push_back(value.value); + } + auto result = ir.CreateCall(module.getFunction(declaration.foreign_symbol), args); + if (declaration.type == "void") return {result, "void"}; + if (declaration.type == "int32") return {ir.CreateSExt(result, ir.getInt64Ty()), "number"}; + if (declaration.type == "int64") return {result, "number"}; + if (declaration.type == "float32") return {ir.CreateFPExt(result, ir.getDoubleTy()), "decimal"}; + return {result, "decimal"}; } [[noreturn]] void fail(tell::Location loc, const std::string& message) { throw tell::Error(loc, message); } llvm::Value* text(const std::string& s) { return ir.CreateGlobalString(s); } - Value expression(const tell::Expr& e) { + llvm::Value* equal_values(Value lhs, Value rhs, Location loc) { + require_type(rhs, lhs.type, loc); + if (lhs.type == "void") fail(loc, "cannot compare void values"); + if (lhs.type == "text") { + auto signature = llvm::FunctionType::get(ir.getInt32Ty(), {ir.getPtrTy(), ir.getPtrTy()}, false); + auto strcmp_fn = module.getOrInsertFunction("strcmp", signature); + return ir.CreateICmpEQ(ir.CreateCall(strcmp_fn, {lhs.value, rhs.value}), ir.getInt32(0)); + } + if (lhs.type == "decimal") return ir.CreateFCmpOEQ(lhs.value, rhs.value); + return ir.CreateICmpEQ(lhs.value, rhs.value); + } + Value index_address(const Expr& e) { + auto list = expression(*e.children[0]); + if (!is_list(list.type)) fail(e.location, "indexing requires a list or vector"); + auto index = expression(*e.children[1]); + require_type(index, "number", e.children[1]->location); + auto layout = list_layout(); + auto length = ir.CreateLoad(ir.getInt64Ty(), ir.CreateStructGEP(layout, list.value, 0)); + raise_if(ir.CreateICmpUGE(index.value, length), "index out of bounds"); + auto data = ir.CreateLoad(ir.getPtrTy(), ir.CreateStructGEP(layout, list.value, 1)); + auto type = list.type.substr(8); + return {ir.CreateGEP(llvm_type(type, e.location), data, index.value), type}; + } + Value expression(const tell::Expr& e, const std::string& expected = "") { using K = tell::Expr::Kind; if(e.kind==K::String) { if (e.value.find(char(0)) != std::string::npos) fail(e.location, "text literals cannot contain NUL"); @@ -155,6 +427,34 @@ class Generator { return {llvm::ConstantInt::getSigned(ir.getInt64Ty(),std::stoll(e.value)),"number"}; } catch(const std::exception&) { fail(e.location,"number out of range"); } } + if (e.kind == K::List) { + std::string element = is_list(expected) ? expected.substr(8) : ""; + std::vector values; + for (const auto& child : e.children) { + auto value = expression(*child, element); + if (element.empty()) element = value.type; + if (element == "void") fail(child->location, "collection element cannot be void"); + require_type(value, element, child->location); + values.push_back(value); + } + if (element.empty()) fail(e.location, "empty list requires an explicit collection type"); + auto element_type = llvm_type(element, e.location); + auto null = llvm::ConstantPointerNull::get(ir.getPtrTy()); + auto size = ir.CreatePtrToInt(ir.CreateGEP(element_type, null, ir.getInt64(values.empty() ? 1 : values.size())), ir.getInt64Ty()); + auto data = allocate(size); + for (size_t i = 0; i < values.size(); ++i) + ir.CreateStore(values[i].value, ir.CreateGEP(element_type, data, ir.getInt64(i))); + auto layout = list_layout(); + auto header_size = ir.CreatePtrToInt(ir.CreateGEP(layout, null, ir.getInt64(1)), ir.getInt64Ty()); + auto header = allocate(header_size); + ir.CreateStore(ir.getInt64(values.size()), ir.CreateStructGEP(layout, header, 0)); + ir.CreateStore(data, ir.CreateStructGEP(layout, header, 1)); + return {header, "list of " + element}; + } + if (e.kind == K::Index) { + auto address = index_address(e); + return {ir.CreateLoad(llvm_type(address.type, e.location), address.value), address.type}; + } if (e.kind == K::Name) return lookup(e.value, e.location); if (e.kind == K::Member) return field(expression(*e.children[0]), e.value, e.location); if (e.kind == K::Call) return call(e); @@ -163,17 +463,7 @@ class Generator { auto rhs = expression(*e.children[1]); require_type(rhs, lhs.type, e.children[1]->location); if (e.value == "=" || e.value == "==" || e.value == "!=") { - llvm::Value* equal; - if (lhs.type == "void") fail(e.location, "cannot compare void values"); - if (lhs.type == "text") { - auto signature = llvm::FunctionType::get(ir.getInt32Ty(), {ir.getPtrTy(), ir.getPtrTy()}, false); - auto strcmp_fn = module.getOrInsertFunction("strcmp", signature); - equal = ir.CreateICmpEQ(ir.CreateCall(strcmp_fn, {lhs.value, rhs.value}), ir.getInt32(0)); - } else if (lhs.type == "decimal") { - equal = ir.CreateFCmpOEQ(lhs.value, rhs.value); - } else { - equal = ir.CreateICmpEQ(lhs.value, rhs.value); - } + auto equal = equal_values(lhs, rhs, e.children[1]->location); return {e.value == "!=" ? ir.CreateNot(equal) : equal, "bool"}; } if (lhs.type == "number") { @@ -234,10 +524,27 @@ class Generator { } for (const auto& stmt : program.statements) { if (stmt.kind == Stmt::Kind::Function) { - if (objects.count(stmt.name) || functions.count(stmt.name)) + if (objects.count(stmt.name) || functions.count(stmt.name) || foreign_functions.count(stmt.name)) fail(stmt.location, "duplicate callable '" + stmt.name + "'"); functions.emplace(stmt.name, declare_routine(stmt, "", false)); } + if (stmt.kind == Stmt::Kind::ForeignFunction) { + if (objects.count(stmt.name) || functions.count(stmt.name) || foreign_functions.count(stmt.name)) + fail(stmt.location, "duplicate callable '" + stmt.name + "'"); + if (stmt.foreign_symbol == "main" || stmt.foreign_symbol == "malloc" || + stmt.foreign_symbol == "strcmp" || stmt.foreign_symbol == "abort" || + module.getNamedValue(stmt.foreign_symbol)) + fail(stmt.location, "duplicate or reserved external symbol '" + stmt.foreign_symbol + "'"); + std::vector args; + std::set names; + for (const auto& param : stmt.foreign_parameters) { + if (!names.insert(param.name).second) fail(param.location, "duplicate foreign parameter '" + param.name + "'"); + args.push_back(foreign_type(param.type)); + } + auto signature = llvm::FunctionType::get(foreign_type(stmt.type), args, false); + llvm::Function::Create(signature, llvm::Function::ExternalLinkage, stmt.foreign_symbol, module); + foreign_functions.emplace(stmt.name, &stmt); + } if (stmt.kind != Stmt::Kind::Object) continue; auto& object = objects.at(stmt.name); std::vector fields; @@ -280,7 +587,7 @@ class Generator { for (const auto& section : stmt.body) { for (const auto& member : section.body) { if (member.kind != Stmt::Kind::Let) continue; - auto value = expression(*member.expression); + auto value = expression(*member.expression, member.type); require_type(value, member.type, member.expression->location); auto address = ir.CreateStructGEP(object.layout, instance, object.fields.at(member.name).index); ir.CreateStore(value.value, address); @@ -322,6 +629,25 @@ class Generator { } } + void emit_switch(const Stmt& stmt) { + auto value = expression(*stmt.expression); + auto fn = ir.GetInsertBlock()->getParent(); + auto merge = llvm::BasicBlock::Create(context, "switch.end", fn); + bool falls_through = false; + for (const auto& arm : stmt.branches) { + auto candidate = expression(*arm.expression); + auto equal = equal_values(value, candidate, arm.expression->location); + auto yes = llvm::BasicBlock::Create(context, "switch.case", fn); + auto next = llvm::BasicBlock::Create(context, "switch.next", fn); + ir.CreateCondBr(equal, yes, next); + ir.SetInsertPoint(yes); + emit_branch(arm.body, merge, falls_through); + ir.SetInsertPoint(next); + } + ir.CreateBr(merge); + ir.SetInsertPoint(merge); + } + void emit_branch(const std::vector& statements, llvm::BasicBlock* merge, bool& falls_through) { auto outer = variables; emit_body(statements); @@ -344,9 +670,110 @@ class Generator { if (stmt.name == "self" || variables.count(stmt.name)) fail(stmt.location, "invalid or duplicate variable '" + stmt.name + "'"); llvm_type(stmt.type, stmt.location); - auto value = expression(*stmt.expression); + auto value = expression(*stmt.expression, stmt.type); require_type(value, stmt.type, stmt.expression->location); - variables.emplace(stmt.name, value); + auto slot = entry_alloca(llvm_type(stmt.type, stmt.location)); + ir.CreateStore(value.value, slot); + variables.emplace(stmt.name, Value{slot, stmt.type, true, stmt.constant}); + break; + } + case Stmt::Kind::Raise: { + auto message = expression(*stmt.expression); + require_type(message, "text", stmt.expression->location); + ir.CreateStore(message.value, pending_error); + propagate_error(); + ir.ClearInsertionPoint(); + break; + } + case Stmt::Kind::Try: { + auto fn = ir.GetInsertBlock()->getParent(); + auto caught = llvm::BasicBlock::Create(context, "try.error", fn); + auto done = llvm::BasicBlock::Create(context, "try.end", fn); + auto outer = variables; + handlers.push_back(caught); + emit_body(stmt.body); + handlers.pop_back(); + variables = outer; + bool falls_through = ir.GetInsertBlock() != nullptr; + if (falls_through) ir.CreateBr(done); + ir.SetInsertPoint(caught); + if (stmt.branches.empty()) { + propagate_error(); + } else { + auto message = ir.CreateLoad(ir.getPtrTy(), pending_error); + ir.CreateStore(llvm::ConstantPointerNull::get(ir.getPtrTy()), pending_error); + variables["error_message"] = Value{message, "text", false, true}; + emit_body(stmt.branches[0].body); + variables = outer; + if (ir.GetInsertBlock()) { ir.CreateBr(done); falls_through = true; } + } + if (falls_through) ir.SetInsertPoint(done); + else { ir.ClearInsertionPoint(); done->eraseFromParent(); } + break; + } + case Stmt::Kind::Set: { + if (stmt.target) { + auto address = index_address(*stmt.target); + auto value = expression(*stmt.expression, address.type); + require_type(value, address.type, stmt.expression->location); + ir.CreateStore(value.value, address.value); + break; + } + auto it = variables.find(stmt.name); + if (it == variables.end()) fail(stmt.location, "unknown variable '" + stmt.name + "'"); + if (it->second.constant) fail(stmt.location, "cannot assign to constant '" + stmt.name + "'"); + if (!it->second.slot) fail(stmt.location, "cannot assign to loop variable '" + stmt.name + "'"); + auto value = expression(*stmt.expression, it->second.type); + require_type(value, it->second.type, stmt.expression->location); + ir.CreateStore(value.value, it->second.value); + break; + } + case Stmt::Kind::Repeat: { + auto count = expression(*stmt.expression); + require_type(count, "number", stmt.expression->location); + auto fn = ir.GetInsertBlock()->getParent(); + auto preheader = ir.GetInsertBlock(); + auto condition = llvm::BasicBlock::Create(context, "repeat.condition", fn); + auto body = llvm::BasicBlock::Create(context, "repeat.body", fn); + auto step = llvm::BasicBlock::Create(context, "repeat.step", fn); + auto done = llvm::BasicBlock::Create(context, "repeat.end", fn); + ir.CreateBr(condition); + ir.SetInsertPoint(condition); + auto index = ir.CreatePHI(ir.getInt64Ty(), 2); + index->addIncoming(ir.getInt64(0), preheader); + ir.CreateCondBr(ir.CreateICmpSLT(index, count.value), body, done); + ir.SetInsertPoint(body); + auto outer = variables; + loops.emplace_back(done, step); + emit_body(stmt.body); + loops.pop_back(); + variables = std::move(outer); + if (ir.GetInsertBlock()) ir.CreateBr(step); + ir.SetInsertPoint(step); + auto next = ir.CreateAdd(index, ir.getInt64(1)); + ir.CreateBr(condition); + index->addIncoming(next, step); + ir.SetInsertPoint(done); + break; + } + case Stmt::Kind::While: { + auto fn = ir.GetInsertBlock()->getParent(); + auto condition = llvm::BasicBlock::Create(context, "while.condition", fn); + auto body = llvm::BasicBlock::Create(context, "while.body", fn); + auto done = llvm::BasicBlock::Create(context, "while.end", fn); + ir.CreateBr(condition); + ir.SetInsertPoint(condition); + auto test = expression(*stmt.expression); + require_type(test, "bool", stmt.expression->location); + ir.CreateCondBr(test.value, body, done); + ir.SetInsertPoint(body); + auto outer = variables; + loops.emplace_back(done, condition); + emit_body(stmt.body); + loops.pop_back(); + variables = std::move(outer); + if (ir.GetInsertBlock()) ir.CreateBr(condition); + ir.SetInsertPoint(done); break; } case Stmt::Kind::Say: @@ -357,6 +784,52 @@ class Generator { case Stmt::Kind::Expression: expression(*stmt.expression); break; + case Stmt::Kind::For: { + if (stmt.name == "self" || variables.count(stmt.name)) + fail(stmt.location, "invalid or duplicate variable '" + stmt.name + "'"); + auto list = expression(*stmt.expression); + if (!is_list(list.type)) fail(stmt.expression->location, "for each requires a list or vector"); + auto layout = list_layout(); + auto length = ir.CreateLoad(ir.getInt64Ty(), ir.CreateStructGEP(layout, list.value, 0)); + auto data = ir.CreateLoad(ir.getPtrTy(), ir.CreateStructGEP(layout, list.value, 1)); + auto fn = ir.GetInsertBlock()->getParent(); + auto preheader = ir.GetInsertBlock(); + auto condition = llvm::BasicBlock::Create(context, "for.condition", fn); + auto body = llvm::BasicBlock::Create(context, "for.body", fn); + auto step = llvm::BasicBlock::Create(context, "for.step", fn); + auto done = llvm::BasicBlock::Create(context, "for.end", fn); + ir.CreateBr(condition); + ir.SetInsertPoint(condition); + auto index = ir.CreatePHI(ir.getInt64Ty(), 2); + index->addIncoming(ir.getInt64(0), preheader); + ir.CreateCondBr(ir.CreateICmpULT(index, length), body, done); + ir.SetInsertPoint(body); + auto type = list.type.substr(8); + auto element_type = llvm_type(type, stmt.location); + auto value = ir.CreateLoad(element_type, ir.CreateGEP(element_type, data, index)); + auto outer = variables; + variables.emplace(stmt.name, Value{value, type}); + loops.emplace_back(done, step); + emit_body(stmt.body); + loops.pop_back(); + variables = std::move(outer); + if (ir.GetInsertBlock()) ir.CreateBr(step); + ir.SetInsertPoint(step); + auto next = ir.CreateAdd(index, ir.getInt64(1)); + ir.CreateBr(condition); + index->addIncoming(next, step); + ir.SetInsertPoint(done); + break; + } + case Stmt::Kind::Break: + case Stmt::Kind::Continue: + if (loops.empty()) fail(stmt.location, "break and continue require a loop"); + ir.CreateBr(stmt.kind == Stmt::Kind::Break ? loops.back().first : loops.back().second); + ir.ClearInsertionPoint(); + break; + case Stmt::Kind::Switch: + emit_switch(stmt); + break; case Stmt::Kind::If: emit_if(stmt); break; @@ -368,7 +841,7 @@ class Generator { ir.CreateRetVoid(); } else { if (!stmt.expression) fail(stmt.location, "expected a return value of type '" + type + "'"); - auto value = expression(*stmt.expression); + auto value = expression(*stmt.expression, type); require_type(value, type, stmt.expression->location); ir.CreateRet(value.value); } @@ -384,8 +857,11 @@ class Generator { ir.SetInsertPoint(llvm::BasicBlock::Create(context, "entry", routine.function)); auto arg = routine.function->arg_begin(); if (!owner.empty()) receiver = {&*arg++, owner}; - for (const auto& parameter : routine.syntax->parameters) - variables.emplace(parameter.name, Value{&*arg++, parameter.type}); + for (const auto& parameter : routine.syntax->parameters) { + auto slot = ir.CreateAlloca(llvm_type(parameter.type, parameter.location)); + ir.CreateStore(&*arg++, slot); + variables.emplace(parameter.name, Value{slot, parameter.type, true}); + } current_routine = &routine; emit_body(routine.syntax->body); if (ir.GetInsertBlock()) { @@ -402,6 +878,8 @@ class Generator { Generator() { auto signature = llvm::FunctionType::get(ir.getInt32Ty(), {ir.getPtrTy()}, true); printf_fn = module.getOrInsertFunction("printf", signature); + pending_error = new llvm::GlobalVariable(module, ir.getPtrTy(), false, llvm::GlobalValue::InternalLinkage, llvm::ConstantPointerNull::get(ir.getPtrTy()), "tell.error"); + input_stream = new llvm::GlobalVariable(module, ir.getPtrTy(), false, llvm::GlobalValue::InternalLinkage, llvm::ConstantPointerNull::get(ir.getPtrTy()), "tell.input"); } void generate(const Program& program, const std::string& path, bool check_only = false @@ -421,7 +899,8 @@ class Generator { llvm::Function::ExternalLinkage, "main", module); ir.SetInsertPoint(llvm::BasicBlock::Create(context, "entry", fn)); for (const auto& stmt : program.statements) { - if (stmt.kind != Stmt::Kind::Object && stmt.kind != Stmt::Kind::Function) + if (stmt.kind != Stmt::Kind::Object && stmt.kind != Stmt::Kind::Function && + stmt.kind != Stmt::Kind::ForeignFunction) emit_statement(stmt); } ir.CreateRet(ir.getInt32(0)); diff --git a/TellC/src/main.cpp b/TellC/src/main.cpp index f1bba10..6c10c5c 100644 --- a/TellC/src/main.cpp +++ b/TellC/src/main.cpp @@ -1,3 +1,7 @@ +#ifdef TELL_STANDALONE_MACOS +#include +#include +#endif #include "tell/parser.hpp" #include "codegen.hpp" @@ -21,6 +25,11 @@ #ifdef TELL_STANDALONE_MACOS #include +#include +#include +#include +#include +#include #include #include #include @@ -40,7 +49,7 @@ namespace { void print_usage(std::ostream& out) { out << "TellC - experimental strongly typed Tell compiler\n" "Usage:\n" - " tellc [-o executable]\n" + " tellc [-o executable] [--link library]...\n" " tellc --help\n" " tellc --version\n" " tellc --llvm-version\n"; @@ -114,6 +123,69 @@ int run(const char* executable, const std::vector& arguments) { return 1; } #else +// Build small import stubs for system symbols actually referenced by inputs. +// dlsym checks the running macOS system instead of depending on an installed SDK. +void collect_system_symbols(llvm::object::ObjectFile& object, void* system, void* cpp, + std::set& systemSymbols, std::set& cppSymbols) { + for (const auto& symbol : object.symbols()) { + auto flags = symbol.getFlags(); + if (!flags) { llvm::consumeError(flags.takeError()); continue; } + if (!(*flags & llvm::object::SymbolRef::SF_Undefined)) continue; + auto name = symbol.getName(); + if (!name) { llvm::consumeError(name.takeError()); continue; } + if (!name->starts_with("_") || name->size() < 2) continue; + std::string raw = name->drop_front().str(); + if (dlsym(system, raw.c_str())) systemSymbols.insert(name->str()); + else if (dlsym(cpp, raw.c_str())) cppSymbols.insert(name->str()); + } +} + +void scan_binary(llvm::object::Binary& binary, void* system, void* cpp, + std::set& systemSymbols, std::set& cppSymbols) { + if (auto* object = llvm::dyn_cast(&binary)) { + collect_system_symbols(*object, system, cpp, systemSymbols, cppSymbols); + } else if (auto* universal = llvm::dyn_cast(&binary)) { + for (const auto& slice : universal->objects()) { + if (slice.getArchFlagName() != "arm64") continue; + auto object = slice.getAsObjectFile(); + if (object) { + scan_binary(**object, system, cpp, systemSymbols, cppSymbols); + } else { + llvm::consumeError(object.takeError()); + auto archive = slice.getAsArchive(); + if (!archive) throw std::runtime_error("cannot inspect ARM64 library slice: " + llvm::toString(archive.takeError())); + scan_binary(**archive, system, cpp, systemSymbols, cppSymbols); + } + return; + } + throw std::runtime_error("library does not contain an ARM64 slice"); + } else if (auto* archive = llvm::dyn_cast(&binary)) { + llvm::Error error = llvm::Error::success(); + for (const auto& child : archive->children(error)) { + auto member = child.getAsBinary(); + if (!member) { llvm::consumeError(member.takeError()); continue; } + if (auto* object = llvm::dyn_cast(member->get())) + collect_system_symbols(*object, system, cpp, systemSymbols, cppSymbols); + } + if (error) throw std::runtime_error("cannot read archive: " + llvm::toString(std::move(error))); + } +} + +std::string make_stub(const char* library, const std::set& symbols) { + std::string stub = std::string("--- !tapi-tbd\ntbd-version: 4\ntargets: [ arm64-macos ]\ninstall-name: ") + + library + "\nexports:\n - targets: [ arm64-macos ]\n symbols: [ "; + bool first = true; + for (const auto& name : symbols) { + if (!first) stub += ", "; + first = false; + // Mach-O symbols can contain characters with special YAML meanings. + stub += "\""; + for (char c : name) { if (c == '\\' || c == '"') stub += '\\'; stub += c; } + stub += "\""; + } + return stub + " ]\n...\n"; +} + class StdoutRedirect { int saved_stdout_ = -1; @@ -179,19 +251,32 @@ class StdoutRedirect { } }; -void link_macos(llvm::StringRef object, const std::string& output) { +void link_macos(llvm::StringRef object, const std::string& output, const std::vector& libraries) { constexpr const char* arch = "arm64"; constexpr const char* minimum = "11.0"; constexpr const char* target = "arm64-macos"; constexpr const char* object_name = "tell-generated.o"; constexpr const char* stub_name = "tell-libSystem.tbd"; - // TellC's current backend only imports these stable libSystem C ABI symbols. - const std::string stub = - std::string("--- !tapi-tbd\ntbd-version: 4\ntargets: [ ") + target + - " ]\ninstall-name: /usr/lib/libSystem.B.dylib\nexports:\n" - " - targets: [ " + target + - " ]\n symbols: [ _printf, _strcmp, _malloc, _abort, dyld_stub_binder ]\n...\n"; + void* system = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY | RTLD_LOCAL); + void* cpp = dlopen("/usr/lib/libc++.1.dylib", RTLD_LAZY | RTLD_LOCAL); + if (!system || !cpp) throw std::runtime_error("cannot load macOS system libraries"); + std::set systemSymbols = {"_printf", "_strcmp", "_malloc", "_abort", "dyld_stub_binder"}; + std::set cppSymbols; + auto generated = llvm::object::createBinary(llvm::MemoryBufferRef(object, object_name)); + if (!generated) throw std::runtime_error("cannot inspect generated object: " + llvm::toString(generated.takeError())); + scan_binary(**generated, system, cpp, systemSymbols, cppSymbols); + for (const auto& path : libraries) { + auto binary = llvm::object::createBinary(path); + if (!binary) throw std::runtime_error("cannot read library '" + path + "': " + llvm::toString(binary.takeError())); + scan_binary(*binary->getBinary(), system, cpp, systemSymbols, cppSymbols); + } + dlclose(cpp); dlclose(system); + const std::string stub = make_stub("/usr/lib/libSystem.B.dylib", systemSymbols); + const std::string cpp_stub = make_stub("/usr/lib/libc++.1.dylib", cppSymbols); + constexpr const char* cpp_stub_name = "tell-libc++.tbd"; + const llvm::CachedHashStringRef cpp_stub_key(cpp_stub_name); + lld::macho::cachedReads[cpp_stub_key] = llvm::MemoryBufferRef(cpp_stub, cpp_stub_name); const llvm::CachedHashStringRef object_key(object_name); const llvm::CachedHashStringRef stub_key(stub_name); @@ -203,10 +288,13 @@ void link_macos(llvm::StringRef object, const std::string& output) { bool output_created = false; try { StdoutRedirect redirect(output, output_created); - const char* args[] = { + std::vector args = { "ld64.lld", "-arch", arch, "-platform_version", "macos", - minimum, minimum, "-o", "-", object_name, stub_name + minimum, minimum, "-o", "-", object_name }; + for (const auto& library : libraries) args.push_back(library.c_str()); + args.push_back(stub_name); + if (!cppSymbols.empty()) args.push_back(cpp_stub_name); const lld::DriverDef drivers[] = {{lld::Darwin, &lld::macho::link}}; const auto result = lld::lldMain(args, llvm::outs(), llvm::errs(), drivers); const bool output_ok = redirect.restore(); @@ -218,6 +306,7 @@ void link_macos(llvm::StringRef object, const std::string& output) { } catch (...) { lld::macho::cachedReads.erase(object_key); lld::macho::cachedReads.erase(stub_key); + lld::macho::cachedReads.erase(cpp_stub_key); if (output_created) { unlink(output.c_str()); } @@ -228,6 +317,7 @@ void link_macos(llvm::StringRef object, const std::string& output) { // leave those references in LLD's process-global cache after the link. lld::macho::cachedReads.erase(object_key); lld::macho::cachedReads.erase(stub_key); + lld::macho::cachedReads.erase(cpp_stub_key); } #endif @@ -270,10 +360,14 @@ int main(int argc, char** argv) { std::string input; std::string output = "a.out"; bool output_option_seen = false; + std::vector libraries; for (int i = 1; i < argc; ++i) { const std::string arg = argv[i]; - if (arg == "-o") { + if (arg == "--link") { + if (++i == argc || std::string(argv[i]).empty()) { std::cerr << "tellc: --link requires a library filename\n"; return 2; } + libraries.emplace_back(argv[i]); + } else if (arg == "-o") { if (output_option_seen) { std::cerr << "tellc: -o may only be specified once\n"; return 2; @@ -300,6 +394,12 @@ int main(int argc, char** argv) { return 2; } + for (const auto& library : libraries) { + std::error_code ec; + if (std::filesystem::equivalent(library, output, ec) && !ec) { + std::cerr << "tellc: output must differ from library " << library << '\n'; return 2; + } + } std::error_code same_error; if (std::filesystem::equivalent(input, output, same_error) && !same_error) { std::cerr << "tellc: output must differ from input\n"; @@ -323,7 +423,7 @@ int main(int argc, char** argv) { #ifdef TELL_STANDALONE_MACOS llvm::SmallVector object; tell::generate_object(program, object); - link_macos(llvm::StringRef(object.data(), object.size()), output); + link_macos(llvm::StringRef(object.data(), object.size()), output, libraries); #else TempFile ir_file; TempFile object_file; @@ -363,7 +463,15 @@ int main(int argc, char** argv) { throw std::runtime_error("LLVM machine code generation failed"); } - if (run("clang", {"clang", object_file.path.c_str(), "-o", output.c_str()}) != 0) { + std::vector link_args = {"clang", object_file.path.c_str()}; + for (const auto& library : libraries) link_args.push_back(library.c_str()); +#ifdef __APPLE__ + if (!libraries.empty()) link_args.push_back("-lc++"); +#else + if (!libraries.empty()) link_args.push_back("-lstdc++"); +#endif + link_args.push_back("-o"); link_args.push_back(output.c_str()); + if (run("clang", link_args) != 0) { throw std::runtime_error("linking failed"); } #endif diff --git a/TellC/src/parser.cpp b/TellC/src/parser.cpp index 2b6c17e..272d93f 100644 --- a/TellC/src/parser.cpp +++ b/TellC/src/parser.cpp @@ -31,7 +31,7 @@ class Parser { Token get(){return t[p++];} void expect(const std::string& s){if(!take(s)) throw Error(peek().loc,"expected '"+s+"'");} std::string name(){if(peek().kind!=Token::Word) throw Error(peek().loc,"expected identifier"); return get().text;} - std::string type_name(){ auto value=name(); auto canonical=lower(value); if(canonical=="text"||canonical=="number"||canonical=="decimal"||canonical=="bool"||canonical=="void") return canonical; return value; } + std::string type_name(){ auto value=name(); auto canonical=lower(value); if(canonical=="list"||canonical=="vector") { expect("of"); auto element=type_name(); if(element=="void") throw Error(peek().loc,"collection element type cannot be void"); return "list of " + element; } if(canonical=="text"||canonical=="number"||canonical=="decimal"||canonical=="bool"||canonical=="void") return canonical; return value; } // A phrase is a name only when followed by a call. Operator words still // delimit expressions, for example in "value and ready()". std::string call_name(std::string value) { @@ -62,6 +62,7 @@ class Parser { if(is("(")){Token op=get();auto n=node(Expr::Kind::Call,op);n->children.push_back(std::move(x));if(!take(")")){do{n->children.push_back(expr());}while(take(","));expect(")");}x=std::move(n);continue;} if(is("[")){Token op=get();auto n=node(Expr::Kind::Index,op);n->children.push_back(std::move(x));n->children.push_back(expr());expect("]");x=std::move(n);continue;} if(is(".") && p+1children.push_back(std::move(x));x=std::move(n);continue;} + if(is("times") && p+1children.push_back(std::move(x));n->children.push_back(expr(bp+(op.text=="**"?0:1)));x=std::move(n); } return x; } @@ -70,7 +71,7 @@ class Parser { if(take("extern")){ s.kind=Stmt::Kind::ForeignFunction; s.foreign_language=lower(name()); - if(s.foreign_language!="c" && s.foreign_language!="swift") throw Error(first.loc,"expected foreign language c or swift"); + if(s.foreign_language!="c" && s.foreign_language!="cpp" && s.foreign_language!="swift") throw Error(first.loc,"expected foreign language c, cpp, or swift"); expect("function"); s.name=name(); expect("("); auto foreign_type=[&](bool allow_void) { Token tok=peek(); auto type=lower(name()); if(type!="int32" && type!="int64" && type!="float32" && type!="float64" && !(allow_void && type=="void")) @@ -83,10 +84,38 @@ class Parser { end();return s; } if(take("let")){s.kind=Stmt::Kind::Let;s.constant=take("constant");s.type=type_name();if(s.type=="void")throw Error(first.loc,"variable type cannot be void");s.instance_member=take("self");s.name=name();expect("be");s.expression=expr();end();return s;} - if(take("set")){s.kind=Stmt::Kind::Set;s.name=name();expect("to");s.expression=expr();end();return s;} + if(take("set")){s.kind=Stmt::Kind::Set; Token target=peek(); s.name=name(); + if(is("[")){s.target=node(Expr::Kind::Name,target);while(is("[")){Token op=get();auto index=node(Expr::Kind::Index,op);index->children.push_back(std::move(s.target));index->children.push_back(expr());expect("]");s.target=std::move(index);}} + expect("to");s.expression=expr();end();return s;} if(take("say")||take("show")||take("print")){s.kind=Stmt::Kind::Say;s.expression=expr();end();return s;} if(take("return")||take("give")){s.kind=Stmt::Kind::Return;if(lower(first.text)=="give")expect("back");if(!is("\n")&&!is(";")&&peek().kind!=Token::End)s.expression=expr();end();return s;} + if(take("raise")){expect("an");expect("error");expect("saying");s.kind=Stmt::Kind::Raise;s.expression=expr();end();return s;} + if(take("try")){s.kind=Stmt::Kind::Try;expect(":");end();separators(); + while(peek().kind!=Token::End && !is("end") && !(is("if") && p+1first.loc.column){ + Token start=peek(); + if(start.loc.column!=case_column || !take("if")) throw Error(start.loc,"expected switch case at the same indentation"); + expect("it");expect("is");Stmt arm{};arm.kind=Stmt::Kind::Case;arm.location=start.loc;arm.expression=expr();expect(":"); + if(peek().kind!=Token::End && !is("\n") && !is(";")) arm.body.push_back(statement()); + else { end(); while(peek().kind!=Token::End && peek().loc.column>case_column) arm.body.push_back(statement()); } + if(arm.body.empty()) throw Error(start.loc,"switch case requires a body"); + s.branches.push_back(std::move(arm)); + } + if(is("end") && peek().loc.column==first.loc.column && p+1 +#include +#include + +extern "C" int64_t cpp_value(int64_t n) { + std::vector words; + words.emplace_back("library"); + return n + static_cast(words.front().size()); +} +extern "C" int32_t cpp_twice(int32_t n) { return n * 2; } +extern "C" float cpp_half(float n) { return n / 2; } +extern "C" double cpp_add(double n) { return n + 1.25; } +extern "C" void cpp_noop() {} diff --git a/TellC/tests/fixtures/assign_constant.tell b/TellC/tests/fixtures/assign_constant.tell new file mode 100644 index 0000000..623bbb4 --- /dev/null +++ b/TellC/tests/fixtures/assign_constant.tell @@ -0,0 +1,2 @@ +let constant number value be 1 +set value to 2 diff --git a/TellC/tests/fixtures/assign_index_type.tell b/TellC/tests/fixtures/assign_index_type.tell new file mode 100644 index 0000000..9b50905 --- /dev/null +++ b/TellC/tests/fixtures/assign_index_type.tell @@ -0,0 +1,2 @@ +let list of number values be [1] +set values[0] to "bad" diff --git a/TellC/tests/fixtures/assign_type.tell b/TellC/tests/fixtures/assign_type.tell new file mode 100644 index 0000000..b576c83 --- /dev/null +++ b/TellC/tests/fixtures/assign_type.tell @@ -0,0 +1,2 @@ +let number value be 1 +set value to "bad" diff --git a/TellC/tests/fixtures/collection_bounds.tell b/TellC/tests/fixtures/collection_bounds.tell new file mode 100644 index 0000000..02ac4d7 --- /dev/null +++ b/TellC/tests/fixtures/collection_bounds.tell @@ -0,0 +1,2 @@ +let vector of number values be [1] +say values[1] diff --git a/TellC/tests/fixtures/collection_empty_infer.tell b/TellC/tests/fixtures/collection_empty_infer.tell new file mode 100644 index 0000000..215222f --- /dev/null +++ b/TellC/tests/fixtures/collection_empty_infer.tell @@ -0,0 +1,3 @@ +for each item in []: + say item +end for diff --git a/TellC/tests/fixtures/collection_iterate_type.tell b/TellC/tests/fixtures/collection_iterate_type.tell new file mode 100644 index 0000000..de11d2e --- /dev/null +++ b/TellC/tests/fixtures/collection_iterate_type.tell @@ -0,0 +1,3 @@ +for each item in 1: + say item +end for diff --git a/TellC/tests/fixtures/collection_type.tell b/TellC/tests/fixtures/collection_type.tell new file mode 100644 index 0000000..de8f30a --- /dev/null +++ b/TellC/tests/fixtures/collection_type.tell @@ -0,0 +1 @@ +let list of number values be [1, "bad"] diff --git a/TellC/tests/fixtures/collections.tell b/TellC/tests/fixtures/collections.tell new file mode 100644 index 0000000..201218c --- /dev/null +++ b/TellC/tests/fixtures/collections.tell @@ -0,0 +1,27 @@ +to values() out vector of number: + say "evaluated" + return [1, 2, 3] +end function +let list of text words be ["one", "two"] +say words[0] +for each item in values(): + if item = 2: + continue + end if + say item +end for +for each word in words: + say word + break +end for +let vector of number empty be [] +for each unused in empty: + say unused +end for +let list of list of number nested be [[4], []] +say nested[0][0] +for each row in nested: + for each value in row: + say value + end for +end for diff --git a/TellC/tests/fixtures/io_errors.tell b/TellC/tests/fixtures/io_errors.tell new file mode 100644 index 0000000..57ddea3 --- /dev/null +++ b/TellC/tests/fixtures/io_errors.tell @@ -0,0 +1,32 @@ +to convert(text: value) out number: + return number(value) +end function +try: + let text raw be input("Value? ") + let number value be convert(raw) + say value +if something goes wrong: + say error_message +end try +write_file("io-test.txt", "hello") +append_file("io-test.txt", " world") +say read_file("io-test.txt") +try: + try: + raise an error saying "custom error" + end try +if something goes wrong: + say error_message +end try +try: + say read_file("file-that-does-not-exist.txt") +if something goes wrong: + say error_message +end try +try: + let list of number values be [1] + say values[5] +if something goes wrong: + say error_message +end try +say decimal("2.5") diff --git a/TellC/tests/fixtures/loops_assignment.tell b/TellC/tests/fixtures/loops_assignment.tell new file mode 100644 index 0000000..6b36cd2 --- /dev/null +++ b/TellC/tests/fixtures/loops_assignment.tell @@ -0,0 +1,33 @@ +to countdown(number: n) out number: + while n > 0: + set n to n - 1 + if n = 2: + continue + end if + if n = 1: + break + end if + say n + end while + return n +end function +let number total be 0 +repeat 4 times: + set total to total + 1 + if total = 2: + continue + end if + if total = 3: + break + end if + say total +end repeat +say total +say countdown(4) +let list of list of number rows be [[1, 2], [3]] +set rows[0][1] to 9 +say rows[0][1] +if true: + set total to 8 +end if +say total diff --git a/TellC/tests/fixtures/switch.tell b/TellC/tests/fixtures/switch.tell new file mode 100644 index 0000000..f403d5c --- /dev/null +++ b/TellC/tests/fixtures/switch.tell @@ -0,0 +1,38 @@ +to amount() out number: + say "evaluated" + return 50 +end function + +switch amount(): + if it is 0: say "tank is empty" + if it is 25: say "Quarter of the tank is full" + if it is 50: say "Half the tank is full" + if it is 75: say "3/4 of the tank is full" + if it is 100: say "Tank is full" +say "after" +switch "water": + if it is "water": + say "matched text" + switch 1: + if it is 1: say "nested" + if it is "water": say "should not run" +end switch +switch 3: + if it is 2: say "should not run" +say "done" +to candidate(number: n) out number: + say n + return n +end function +to describe(number: n) out text: + switch n: + if it is candidate(1): return "one" + if it is candidate(2): return "two" + if it is candidate(3): return "three" + return "other" +end function +say describe(2) +switch 1.5: + if it is 1.5: say "decimal" +switch true: + if it is true: say "boolean" diff --git a/TellC/tests/fixtures/switch_empty.tell b/TellC/tests/fixtures/switch_empty.tell new file mode 100644 index 0000000..e29c016 --- /dev/null +++ b/TellC/tests/fixtures/switch_empty.tell @@ -0,0 +1,2 @@ +switch 1: +say "wrong" diff --git a/TellC/tests/fixtures/switch_type.tell b/TellC/tests/fixtures/switch_type.tell new file mode 100644 index 0000000..4d9d609 --- /dev/null +++ b/TellC/tests/fixtures/switch_type.tell @@ -0,0 +1,2 @@ +switch 1: + if it is "1": say "wrong" diff --git a/TellC/tests/fixtures/uncaught_error.tell b/TellC/tests/fixtures/uncaught_error.tell new file mode 100644 index 0000000..2f686f4 --- /dev/null +++ b/TellC/tests/fixtures/uncaught_error.tell @@ -0,0 +1,4 @@ +to fail() out number: + return number("no") +end function +say fail()