Nicolas Winkler 6 gadi atpakaļ
vecāks
revīzija
fe34a7a2ed
7 mainītis faili ar 66 papildinājumiem un 21 dzēšanām
  1. 39 3
      src/Mangling.cpp
  2. 4 6
      src/Mangling.h
  3. 6 0
      src/Scope.cpp
  4. 1 4
      src/Scope.h
  5. 3 8
      src/sem/CodeGeneration.cpp
  6. 11 0
      src/sem/Semantic.cpp
  7. 2 0
      src/sem/Semantic.h

+ 39 - 3
src/Mangling.cpp

@@ -1,11 +1,47 @@
 #include "Mangling.h"
-//#include "Semantic.h"
+#include "Semantic.h"
+#include "Type.h"
 
-using qlow::Mangler;
 
+std::string numberEncode(const std::string& x)
+{
+    return std::to_string(x.length()) + x;
+}
 
-std::string Mangler::mangle(const qlow::sem::Method& method)
+
+std::string qlow::mangle(const qlow::sem::Method& method)
 {
+    static const std::string prefix = "_Q";
+
+    std::string mangled = prefix;
+    sem::Class* parent = method.containingClass;
+
+    if (parent != nullptr) {
+        mangled += numberEncode(parent->name);
+    }
+
+    mangled += numberEncode(method.name);
+
+    for (const sem::Variable* arg : method.arguments) {
+        mangled += mangle(arg->type);
+    }
+
+    return mangled;
+}
+
+
+std::string qlow::mangle(const qlow::sem::Type* type)
+{
+    if (type->isClassType()) {
+        return "C" + numberEncode(type->getClass()->name);
+    }
+    else if (type->isArrayType()) {
+        return "A" + mangle(type->getArrayOf());
+    }
+
+    else {
+        return "N";
+    }
 }
 
 

+ 4 - 6
src/Mangling.h

@@ -10,16 +10,14 @@ namespace qlow
     namespace sem
     {
         struct Method;
+        class Type;
     }
-}
 
 
-class qlow::Mangler
-{
-public:
-    Mangler(void) = default;
 
     std::string mangle(const sem::Method& method);
-};
+    std::string mangle(const sem::Type* type);
+}
+
 
 #endif // QLOW_MANGLING_H_

+ 6 - 0
src/Scope.cpp

@@ -318,6 +318,12 @@ bool sem::TypeScope::isNativeTypeScope(void) const
 }
 
 
+sem::NativeTypeScope::NativeTypeScope(Context& context, Type* type) :
+    TypeScope{ context, type }
+{
+}
+
+
 sem::NativeTypeScope::NativeTypeScope(NativeTypeScope&&) = default;
 //sem::NativeTypeScope& sem::NativeTypeScope::operator=(NativeTypeScope&&) = default;
 

+ 1 - 4
src/Scope.h

@@ -172,10 +172,7 @@ class qlow::sem::NativeTypeScope : public TypeScope
 {
 public:
     SymbolTable<NativeMethod> nativeMethods;
-    inline NativeTypeScope(Context& context, Type* type) :
-        TypeScope{ context, type }
-    {
-    }
+    NativeTypeScope(Context& context, Type* type);
 
     NativeTypeScope(NativeTypeScope&&);
     //NativeTypeScope& operator=(NativeTypeScope&&);

+ 3 - 8
src/sem/CodeGeneration.cpp

@@ -1,4 +1,5 @@
 #include "CodeGeneration.h"
+#include "Mangling.h"
 #include "Linking.h"
 
 #include <llvm/IR/LLVMContext.h>
@@ -155,13 +156,7 @@ llvm::Function* generateFunction(llvm::Module* module, sem::Method* method)
 #endif 
     if (returnType == nullptr)
         throw "invalid return type";
-    std::string symbolName;
-    if (method->isExtern) {
-        symbolName = qlow::getExternalSymbol(method->name);
-    }
-    else {
-        symbolName = method->name;
-    }
+    std::string symbolName = method->getMangledName();
     Function* func = Function::Create(funcType, Function::ExternalLinkage, symbolName, module);
     method->llvmNode = func;
     
@@ -333,7 +328,7 @@ llvm::Function* qlow::gen::FunctionGenerator::generate(void)
     printf("generate function %s\n", method.name.c_str()); 
 #endif
 
-    Function* func = module->getFunction(method.name);
+    Function* func = module->getFunction(method.getMangledName());
 
     if (func == nullptr) {
         throw "internal error: function not found";

+ 11 - 0
src/sem/Semantic.cpp

@@ -2,6 +2,8 @@
 #include "Type.h"
 #include "Ast.h"
 #include "AstVisitor.h"
+#include "Mangling.h"
+#include "Linking.h"
 
 #include "CodegenVisitor.h"
 
@@ -166,6 +168,15 @@ void Method::generateThisExpression(void)
 }
 
 
+std::string Method::getMangledName(void) const
+{
+    if (isExtern)
+        return qlow::getExternalSymbol(name);
+    else
+        return qlow::mangle(*this);
+}
+
+
 std::string Method::toString(void) const
 {
     return "Method[" + this->name + "]";

+ 2 - 0
src/sem/Semantic.h

@@ -188,6 +188,8 @@ struct qlow::sem::Method : public SemanticObject
     }
     
     void generateThisExpression(void);
+
+    std::string getMangledName(void) const;
     
     virtual std::string toString(void) const override;
 };