Commit a0b2bd9d authored by 李晓奇's avatar 李晓奇

finish part1: hand write .ll file

parent 3b086a0e
......@@ -11,7 +11,8 @@ class ArrayType;
class PointerType;
class FloatType;
class Type {
class Type
{
public:
enum TypeID {
VoidTyID, // Void
......@@ -73,11 +74,12 @@ class Type {
std::string print();
private:
TypeID tid_;
TypeID tid_;
Module *m_;
};
class IntegerType : public Type {
class IntegerType : public Type
{
public:
explicit IntegerType(unsigned num_bits, Module *m);
......@@ -89,7 +91,8 @@ class IntegerType : public Type {
unsigned num_bits_;
};
class FunctionType : public Type {
class FunctionType : public Type
{
public:
FunctionType(Type *result, std::vector<Type *> params);
......@@ -100,17 +103,18 @@ class FunctionType : public Type {
unsigned get_num_of_args() const;
Type *get_param_type(unsigned i) const;
Type *get_param_type(unsigned i) const;
std::vector<Type *>::iterator param_begin() { return args_.begin(); }
std::vector<Type *>::iterator param_end() { return args_.end(); }
Type *get_return_type() const;
Type *get_return_type() const;
private:
Type *result_;
Type *result_;
std::vector<Type *> args_;
};
class ArrayType : public Type {
class ArrayType : public Type
{
public:
ArrayType(Type *contained, unsigned num_elements);
......@@ -118,15 +122,16 @@ class ArrayType : public Type {
static ArrayType *get(Type *contained, unsigned num_elements);
Type *get_element_type() const { return contained_; }
Type *get_element_type() const { return contained_; }
unsigned get_num_of_elements() const { return num_elements_; }
private:
Type *contained_; // The element type of the array.
Type *contained_; // The element type of the array.
unsigned num_elements_; // Number of elements in the array.
};
class PointerType : public Type {
class PointerType : public Type
{
public:
PointerType(Type *contained);
Type *get_element_type() const { return contained_; }
......@@ -137,7 +142,8 @@ class PointerType : public Type {
Type *contained_; // The element type of the ptr.
};
class FloatType : public Type {
class FloatType : public Type
{
public:
FloatType(Module *m);
static FloatType *get(Module *m);
......@@ -145,4 +151,4 @@ class FloatType : public Type {
private:
};
#endif // SYSYC_TYPE_H
\ No newline at end of file
#endif // SYSYC_TYPE_H
define dso_local i32 @main() #0 {
%a = alloca [10 x i32], align 4
; ptr is the pointer with type i32*
%ptr = getelementptr [10 x i32], [10 x i32]* %a, i64 0, i64 0
%a0 = getelementptr i32, i32* %ptr, i64 0
%a1 = getelementptr i32, i32* %ptr, i64 1
store i32 10, i32* %a0
%v1 = load i32, i32* %a0
%v2 = mul i32 %v1, 2
store i32 %v2, i32* %a1
%r = load i32, i32* %a1
ret i32 %r
}
define dso_local i32 @callee(i32 %0) #0 {
%r = mul i32 %0, 2
ret i32 %r
}
define dso_local i32 @main() #0 {
%r = call i32 @callee(i32 110)
ret i32 %r
}
define i32 @main(){
%a_ptr = alloca float
store float 0x40163851E0000000, float* %a_ptr
%a = load float, float* %a_ptr
%cond = fcmp ugt float %a, 1.000000e+00
br i1 %cond, label %1, label %2
1:
ret i32 233
2:
ret i32 0
}
define dso_local i32 @main() #0 {
%pa = alloca i32
%pi = alloca i32
store i32 10, i32* %pa
store i32 0, i32* %pi
br label %loop_head
loop_head:
%i = load i32, i32* %pi
%cond = icmp slt i32 %i, 10
br i1 %cond, label %loop_body, label %over
loop_body:
%i_1 = add i32 %i, 1
store i32 %i_1, i32* %pi
%newi = load i32, i32* %pi
%a = load i32, i32* %pa
%a_i = add i32 %a, %newi
store i32 %a_i, i32* %pa
br label %loop_head
over:
%r = load i32, i32* %pa
ret i32 %r
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment