Skip to content

Commit

Permalink
Merge pull request #24 from Ttimofeyka/newllvm
Browse files Browse the repository at this point in the history
Merging a newllvm branch into the main
  • Loading branch information
Ttimofeyka authored Aug 27, 2024
2 parents bd8eb41 + 6f09927 commit d751310
Show file tree
Hide file tree
Showing 100 changed files with 1,296 additions and 1,575 deletions.
6 changes: 2 additions & 4 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/lib/llvm-11/include",
"/usr/lib/llvm-13/include",
"/usr/lib/llvm-14/include",
"/usr/lib/llvm-15/include",
"/usr/lib/llvm-16/include"
"/usr/lib/llvm-16/include",
"/usr/lib/llvm-18/include"
],
"defines": [],
"cStandard": "c17",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ For maximum performance, use the `-Ofast` or `-O3 --noChecks`. Also, don't forge
## Dependencies

* `llvm-16`
**You can also use LLVM from 11 to 15.**
**You can also use LLVM from 14 to 18.**
* `clang` or `gcc`
* C++ compiler (with support of C++17 and higher)
* Make
Expand Down
12 changes: 0 additions & 12 deletions specifications/grammar/simd.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ int4 vector;
int value = @vGet(vector, 0); // 2
```

**@vAdd, @vSub, @vMul, @vDiv (vector1, vector2)** - Performs an arithmetic operation on all elements between two vectors, returning the output vector as a result.

NOTE: You can use standard arithmetic operators for most types of vectors instead of builtins.

Example:

```d
float4 vector = @vFrom(float4, 10f);
vector = @vMul(vector, vector);
float value = @vGet(vector, 0); // 100f
```

**@vShuffle(vector1, vector2, array)** - Returns a shuffled vector with the specified mask.

Example:
Expand Down
16 changes: 14 additions & 2 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <llvm-c/IRReader.h>
#include <llvm-c/Orc.h>

#if LLVM_VERSION >= 14
#include <llvm-c/OrcEE.h>
#include <llvm-c/Transforms/PassBuilder.h>
#endif

#include <llvm-c/Remarks.h>
#include <llvm-c/Linker.h>

#if LLVM_VERSION < 17
#include <llvm-c/Transforms/InstCombine.h>
#include <llvm-c/Transforms/Utils.h>
#include <llvm-c/Transforms/Vectorize.h>
#include <llvm-c/Transforms/PassManagerBuilder.h>
#include <llvm-c/Transforms/IPO.h>
#include <llvm-c/Transforms/Scalar.h>
#endif

#include <llvm-c/Target.h>

#ifdef _WIN32
Expand Down Expand Up @@ -337,6 +339,7 @@ void Compiler::compile(std::string file) {

for(int i=0; i<parser->nodes.size(); i++) parser->nodes[i]->generate();

#if LLVM_VERSION < 17
LLVMPassManagerRef pm = LLVMCreatePassManager();

if(Compiler::settings.optLevel > 0) {
Expand Down Expand Up @@ -387,6 +390,15 @@ void Compiler::compile(std::string file) {
}

LLVMRunPassManager(pm, generator->lModule);
#else
LLVMPassBuilderOptionsRef pbOptions = LLVMCreatePassBuilderOptions();

if(Compiler::settings.optLevel == 1) LLVMRunPasses(generator->lModule, "default<O1>", machine, pbOptions);
else if(Compiler::settings.optLevel == 2) LLVMRunPasses(generator->lModule, "default<O2>", machine, pbOptions);
else if(Compiler::settings.optLevel == 3) LLVMRunPasses(generator->lModule, "default<O3>", machine, pbOptions);

LLVMDisposePassBuilderOptions(pbOptions);
#endif

LLVMTargetMachineEmitToFile(machine, generator->lModule, (char*)(std::regex_replace(file, std::regex("\\.rave"), ".o")).c_str(), LLVMObjectFile, &errors);
if(errors != nullptr) {
Expand Down
25 changes: 0 additions & 25 deletions src/include/callconv.hpp

This file was deleted.

28 changes: 17 additions & 11 deletions src/include/llvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

class Type;

struct RaveValue {
LLVMValueRef value;
Type* type;
};

namespace LLVM {
extern LLVMValueRef load(LLVMValueRef value, const char* name);
extern LLVMValueRef call(LLVMValueRef fn, LLVMValueRef* args, unsigned int argsCount, const char* name);
extern LLVMValueRef gep(LLVMValueRef ptr, LLVMValueRef* indices, unsigned int indicesCount, const char* name);
extern LLVMValueRef inboundsGep(LLVMValueRef ptr, LLVMValueRef* indices, unsigned int indicesCount, const char* name);
extern LLVMValueRef constInboundsGep(LLVMValueRef ptr, LLVMValueRef* indices, unsigned int indicesCount);
extern LLVMValueRef structGep(LLVMValueRef ptr, unsigned int idx, const char* name);
extern LLVMValueRef alloc(LLVMTypeRef type, const char* name);
extern LLVMValueRef alloc(LLVMValueRef size, const char* name);
extern bool isPointerType(LLVMTypeRef type);
extern bool isPointer(LLVMValueRef value);
extern LLVMTypeRef getPointerElType(LLVMValueRef value);
extern RaveValue load(RaveValue value, const char* name, int loc);
extern RaveValue alloc(Type* type, const char* name);
extern RaveValue alloc(RaveValue size, const char* name);
extern RaveValue call(RaveValue fn, LLVMValueRef* args, unsigned int argsCount, const char* name);
extern RaveValue call(RaveValue fn, std::vector<RaveValue> args, const char* name);
extern RaveValue gep(RaveValue ptr, LLVMValueRef* indices, unsigned int indicesCount, const char* name);
extern RaveValue cInboundsGep(RaveValue ptr, LLVMValueRef* indices, unsigned int indicesCount);
extern RaveValue structGep(RaveValue ptr, unsigned int idx, const char* name);

extern void setFastMath(LLVMBuilderRef builder, bool infs, bool nans, bool arcp, bool nsz);
extern void setFastMathAll(LLVMBuilderRef builder, bool value);

extern LLVMValueRef makeInt(size_t n, unsigned long long value, bool isUnsigned);
extern RaveValue makeCArray(Type* ty, std::vector<RaveValue> values);
}
1 change: 1 addition & 0 deletions src/include/parser/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ class Type {
virtual Type* check(Type* parent) = 0;
virtual std::string toString() = 0;
virtual Type* copy() = 0;
virtual Type* getElType() = 0;
};
18 changes: 17 additions & 1 deletion src/include/parser/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TypeBasic : public Type {
Type* check(Type* parent) override;
bool isFloat();
std::string toString() override;
Type* getElType() override;
};

class TypePointer : public Type {
Expand All @@ -56,6 +57,7 @@ class TypePointer : public Type {
Type* copy() override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeArray : public Type {
Expand All @@ -68,6 +70,7 @@ class TypeArray : public Type {
Type* copy() override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeAlias : public Type {
Expand All @@ -77,6 +80,7 @@ class TypeAlias : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeVoid : public Type {
Expand All @@ -86,6 +90,7 @@ class TypeVoid : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeConst : public Type {
Expand All @@ -97,6 +102,7 @@ class TypeConst : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeStruct : public Type {
Expand All @@ -111,6 +117,7 @@ class TypeStruct : public Type {
void updateByTypes();
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeFuncArg : public Type {
Expand All @@ -123,18 +130,21 @@ class TypeFuncArg : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeFunc : public Type {
public:
Type* main;
std::vector<TypeFuncArg*> args;
bool isVarArg;

TypeFunc(Type* main, std::vector<TypeFuncArg*> args);
TypeFunc(Type* main, std::vector<TypeFuncArg*> args, bool isVarArg);
Type* copy() override;
int getSize() override;
std::string toString() override;
Type* check(Type* parent) override;
Type* getElType() override;
};

class TypeBuiltin : public Type {
Expand All @@ -148,6 +158,7 @@ class TypeBuiltin : public Type {
int getSize() override;
std::string toString() override;
Type* check(Type* parent) override;
Type* getElType() override;
};

class TypeCall : public Type {
Expand All @@ -160,6 +171,7 @@ class TypeCall : public Type {
Type* copy() override;
int getSize() override;
Type* check(Type* parent) override;
Type* getElType() override;
};

class TypeAuto : public Type {
Expand All @@ -169,6 +181,7 @@ class TypeAuto : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeLLVM : public Type {
Expand All @@ -179,6 +192,7 @@ class TypeLLVM : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeVector : public Type {
Expand All @@ -191,6 +205,7 @@ class TypeVector : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

class TypeDivided : public Type {
Expand All @@ -203,6 +218,7 @@ class TypeDivided : public Type {
Type* check(Type* parent) override;
int getSize() override;
std::string toString() override;
Type* getElType() override;
};

Type* getType(std::string id);
32 changes: 19 additions & 13 deletions src/include/parser/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <string>
#include <map>
#include <llvm-c/Core.h>

#if LLVM_VERSION < 17
#include <llvm-c/Initialization.h>
#endif

#include <llvm-c/lto.h>
#include <llvm-c/Target.h>
#include "../utils.hpp"
#include "./Type.hpp"
#include "./Types.hpp"
#include "./nodes/Node.hpp"
#include "../json.hpp"
#include "../llvm.hpp"
#include <vector>

class NodeVar;
Expand Down Expand Up @@ -59,7 +64,7 @@ namespace AST {

extern std::string typesToString(std::vector<FuncArgSet> args);
extern std::string typesToString(std::vector<Type*> args);
extern std::vector<Type*> parametersToTypes(std::vector<LLVMValueRef> params);
extern std::vector<Type*> parametersToTypes(std::vector<RaveValue> params);

class LLVMGen {
public:
Expand All @@ -71,13 +76,13 @@ class LLVMGen {
genSettings settings;
nlohmann::json options;

std::map<std::string,LLVMValueRef> globals;
std::map<std::string,LLVMValueRef> functions;
std::map<std::string,LLVMTypeRef> structures;
std::map<int32_t,Loop> activeLoops;
std::map<std::string, RaveValue> globals;
std::map<std::string, RaveValue> functions;
std::map<std::string, LLVMTypeRef> structures;
std::map<int32_t, Loop> activeLoops;

std::map<std::string,std::string> neededFunctions;
std::map<std::string,Type*> toReplace;
std::map<std::string, std::string> neededFunctions;
std::map<std::string, Type*> toReplace;

LLVMBasicBlockRef currBB;

Expand All @@ -92,7 +97,7 @@ class LLVMGen {
std::string mangle(std::string name, bool isFunc, bool isMethod);

LLVMTypeRef genType(Type* type, int loc);
LLVMValueRef byIndex(LLVMValueRef value, std::vector<LLVMValueRef> indexes);
RaveValue byIndex(RaveValue value, std::vector<LLVMValueRef> indexes);
void addAttr(std::string name, LLVMAttributeIndex index, LLVMValueRef ptr, int loc, unsigned long value = 0);
void addStrAttr(std::string name, LLVMAttributeIndex index, LLVMValueRef ptr, int loc, std::string value = "");
Type* setByTypeList(std::vector<Type*> list);
Expand All @@ -102,24 +107,26 @@ class LLVMGen {

class Scope {
public:
std::map<std::string, LLVMValueRef> localScope;
std::map<std::string, RaveValue> localScope;
std::map<std::string, int> args;
std::string funcName;
LLVMBasicBlockRef blockExit;
bool funcHasRet = false;
std::map<std::string, NodeVar*> localVars;
std::map<std::string, NodeVar*> argVars;
std::map<std::string, Node*> aliasTable;
bool inTry = false;
LLVMBasicBlockRef fnEnd;
LLVMBasicBlockRef elseIfEnd = nullptr;
bool detectMemoryLeaks = false;

Scope(std::string funcName, std::map<std::string, int> args, std::map<std::string, NodeVar*> argVars);

LLVMValueRef get(std::string name, int loc = -1);
LLVMValueRef getWithoutLoad(std::string name, int loc = -1);
// Old functions (not recommended)

RaveValue get(std::string name, int loc = -1);
RaveValue getWithoutLoad(std::string name, int loc = -1);
NodeVar* getVar(std::string name, int loc = -1);

bool has(std::string name);
bool hasAtThis(std::string name);
bool locatedAtThis(std::string name);
Expand All @@ -131,7 +138,6 @@ extern LLVMGen* generator;
extern Scope* currScope;
extern LLVMTargetDataRef dataLayout;

extern Type* lTypeToType(LLVMTypeRef t, LLVMValueRef value);
extern TypeFunc* callToTFunc(NodeCall* call);
extern Scope* copyScope(Scope* original);
std::string typeToString(LLVMTypeRef type);
Loading

0 comments on commit d751310

Please sign in to comment.