Skip to content

Commit

Permalink
Add unsigned integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-gc committed Apr 12, 2016
1 parent 2bfa436 commit 847a281
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/compiler/ac_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ LLVMValueRef ac_convert_const(LLVMValueRef val, EagleTypeType *to, EagleTypeType
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
{
switch(to->type)
{
Expand All @@ -67,6 +71,11 @@ LLVMValueRef ac_convert_const(LLVMValueRef val, EagleTypeType *to, EagleTypeType
case ETInt32:
case ETInt64:
return LLVMConstIntCast(val, ett_llvm_type(to), 1);
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMConstIntCast(val, ett_llvm_type(to), 0);
case ETDouble:
return LLVMConstSIToFP(val, ett_llvm_type(to));
default:
Expand All @@ -86,6 +95,11 @@ LLVMValueRef ac_convert_const(LLVMValueRef val, EagleTypeType *to, EagleTypeType
case ETInt32:
case ETInt64:
return LLVMConstFPToSI(val, ett_llvm_type(to));
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMConstFPToUI(val, ett_llvm_type(to));
default:
return NULL;
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/ac_control_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ int ac_type_is_valid_for_switch(EagleTypeType *type)
{
case ETInt1:
case ETInt8:
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
case ETEnum:
return 1;
default:
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/ac_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,18 @@ LLVMValueRef ac_compile_unary(AST *ast, CompilerBundle *cb)
fmt = LLVMBuildGlobalStringPtr(cb->builder, "(Bool) %d\n", "prfB");
break;
case ETInt8:
case ETInt16:
case ETInt32:
fmt = LLVMBuildGlobalStringPtr(cb->builder, "%d\n", "prfI");
break;
case ETUInt8:
case ETUInt16:
case ETUInt32:
fmt = LLVMBuildGlobalStringPtr(cb->builder, "%u\n", "prfU");
break;
case ETUInt64:
fmt = LLVMBuildGlobalStringPtr(cb->builder, "%lu\n", "prfU");
break;
case ETInt64:
fmt = LLVMBuildGlobalStringPtr(cb->builder, "%ld\n", "prfLI");
break;
Expand Down
59 changes: 59 additions & 0 deletions src/compiler/ac_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ LLVMValueRef ac_build_conversion(CompilerBundle *cb, LLVMValueRef val, EagleType
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
switch(to->type)
{
case ETInt1:
Expand All @@ -122,6 +126,10 @@ LLVMValueRef ac_build_conversion(CompilerBundle *cb, LLVMValueRef val, EagleType
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildIntCast(builder, val, ett_llvm_type(to), "conv");
case ETFloat:
case ETDouble:
Expand All @@ -144,6 +152,11 @@ LLVMValueRef ac_build_conversion(CompilerBundle *cb, LLVMValueRef val, EagleType
case ETInt32:
case ETInt64:
return LLVMBuildFPToSI(builder, val, ett_llvm_type(to), "conv");
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildFPToSI(builder, val, ett_llvm_type(to), "conv");
default:
die(lineno, "Invalid implicit conversion from double.");
break;
Expand Down Expand Up @@ -175,6 +188,10 @@ LLVMValueRef ac_make_add(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef b
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildAdd(builder, left, right, "addtmp");
default:
die(lineno, "The given types may not be summed.");
Expand All @@ -193,6 +210,10 @@ LLVMValueRef ac_make_sub(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef b
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildSub(builder, left, right, "subtmp");
default:
die(lineno, "The given types may not be subtracted.");
Expand All @@ -211,6 +232,10 @@ LLVMValueRef ac_make_mul(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef b
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildMul(builder, left, right, "multmp");
default:
die(lineno, "The given types may not be multiplied.");
Expand All @@ -230,6 +255,11 @@ LLVMValueRef ac_make_div(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef b
case ETInt32:
case ETInt64:
return LLVMBuildSDiv(builder, left, right, "divtmp");
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildUDiv(builder, left, right, "divtmp");
default:
die(lineno, "The given types may not be divided.");
return NULL;
Expand All @@ -248,6 +278,11 @@ LLVMValueRef ac_make_mod(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef b
case ETInt32:
case ETInt64:
return LLVMBuildSRem(builder, left, right, "modtmp");
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildURem(builder, left, right, "modtmp");
default:
die(lineno, "The given types may not have modulo applied.");
return NULL;
Expand All @@ -266,6 +301,10 @@ LLVMValueRef ac_make_bitor(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRef
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildOr(builder, left, right, "OR");
default:
die(lineno, "The given types may not be OR'd");
Expand All @@ -285,6 +324,10 @@ LLVMValueRef ac_make_bitand(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRe
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildAnd(builder, left, right, "AND");
default:
die(lineno, "The given types may not be AND'd");
Expand All @@ -304,6 +347,10 @@ LLVMValueRef ac_make_bitxor(LLVMValueRef left, LLVMValueRef right, LLVMBuilderRe
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildXor(builder, left, right, "XOR");
default:
die(lineno, "The given types may not be XOR'd");
Expand All @@ -330,6 +377,10 @@ LLVMValueRef ac_make_bitshift(LLVMValueRef left, LLVMValueRef right, LLVMBuilder
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return shift_func(builder, left, right, "shift");
default:
die(lineno, "The given types may not be shifted");
Expand All @@ -348,6 +399,10 @@ LLVMValueRef ac_make_neg(LLVMValueRef val, LLVMBuilderRef builder, EagleType typ
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildNeg(builder, val, "negtmp");
default:
die(lineno, "The given type may not have negation applied (%d).", type);
Expand All @@ -367,6 +422,10 @@ LLVMValueRef ac_make_bitnot(LLVMValueRef val, LLVMBuilderRef builder, EagleType
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return LLVMBuildNot(builder, val, "NOT");
default:
die(lineno, "The given type may not be NOT'd");
Expand Down
36 changes: 18 additions & 18 deletions src/core/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ EagleTypeType *et_parse_string(char *text)
TTEST(text, "any", ETAny);
TTEST(text, "bool", ETInt1);
TTEST(text, "byte", ETInt8);
TTEST(text, "ubyte", ETUInt8);
TTEST(text, "short", ETInt16);
TTEST(text, "ushort", ETUInt16);
TTEST(text, "int", ETInt32);
TTEST(text, "uint", ETUInt32);
TTEST(text, "long", ETInt64);
TTEST(text, "ulong", ETUInt64);
TTEST(text, "float", ETFloat);
TTEST(text, "double", ETDouble);
TTEST(text, "void", ETVoid);
Expand Down Expand Up @@ -185,9 +189,13 @@ LLVMValueRef ett_default_value(EagleTypeType *type)
{
case ETInt1:
case ETInt8:
case ETUInt8:
case ETInt16:
case ETUInt16:
case ETInt32:
case ETUInt32:
case ETInt64:
case ETUInt64:
case ETEnum:
return LLVMConstInt(ett_llvm_type(type), 0, 0);
case ETFloat:
Expand Down Expand Up @@ -235,12 +243,16 @@ LLVMTypeRef ett_llvm_type(EagleTypeType *type)
return LLVMInt1TypeInContext(utl_get_current_context());
case ETAny:
case ETInt8:
case ETUInt8:
return LLVMInt8TypeInContext(utl_get_current_context());
case ETInt16:
case ETUInt16:
return LLVMInt16TypeInContext(utl_get_current_context());
case ETInt32:
case ETUInt32:
return LLVMInt32TypeInContext(utl_get_current_context());
case ETInt64:
case ETUInt64:
return LLVMInt64TypeInContext(utl_get_current_context());
case ETCString:
return LLVMPointerType(LLVMInt8TypeInContext(utl_get_current_context()), 0);
Expand Down Expand Up @@ -327,6 +339,7 @@ LLVMTypeRef ett_llvm_type(EagleTypeType *type)
}
}

/*
EagleType et_eagle_type(LLVMTypeRef ty)
{
ETEST(ETDouble, ty, LLVMDoubleTypeInContext(utl_get_current_context()));
Expand All @@ -336,6 +349,7 @@ EagleType et_eagle_type(LLVMTypeRef ty)
return ETNone;
}
*/

#define TT(t) {ET ## t }
static EagleTypeType base_types[] = {
Expand All @@ -350,6 +364,10 @@ static EagleTypeType base_types[] = {
TT(Int64),
TT(Float),
TT(Double),
TT(UInt8),
TT(UInt16),
TT(UInt32),
TT(UInt64),
TT(CString),
TT(Pointer),
TT(Array),
Expand Down Expand Up @@ -684,24 +702,6 @@ int ett_pointer_depth(EagleTypeType *t)
return i;
}

int ett_is_numeric(EagleTypeType *t)
{
EagleType type = t->type;
switch(type)
{
case ETInt1:
case ETInt8:
case ETInt16:
case ETInt32:
case ETInt64:
case ETFloat:
case ETDouble:
return 1;
default:
return 0;
}
}

int ett_size_of_type(EagleTypeType *t)
{
return LLVMStoreSizeOfType(etTargetData, ett_llvm_type(t));
Expand Down
35 changes: 34 additions & 1 deletion src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ typedef enum {
ETInt64,
ETFloat,
ETDouble,
ETUInt8,
ETUInt16,
ETUInt32,
ETUInt64,
ETCString,
ETPointer,
ETArray,
Expand Down Expand Up @@ -131,7 +135,6 @@ LLVMTypeRef ett_llvm_type(EagleTypeType *type);
LLVMValueRef ett_default_value(EagleTypeType *type);
int ett_are_same(EagleTypeType *left, EagleTypeType *right);
int ett_pointer_depth(EagleTypeType *t);
int ett_is_numeric(EagleTypeType *t);
int ett_size_of_type(EagleTypeType *t);
int ett_array_has_counted(EagleTypeType *t);
int ett_array_count(EagleTypeType *t);
Expand Down Expand Up @@ -181,6 +184,10 @@ static inline int ET_IS_INT(EagleType t)
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return 1;
default:
return 0;
Expand All @@ -196,6 +203,10 @@ static inline int ET_IS_LLVM_INT(EagleType t)
case ETInt16:
case ETInt32:
case ETInt64:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return 1;
default:
return 0;
Expand All @@ -214,4 +225,26 @@ static inline int ET_IS_REAL(EagleType t)
}
}

static inline int ett_is_numeric(EagleTypeType *t)
{
EagleType type = t->type;
switch(type)
{
case ETInt1:
case ETInt8:
case ETInt16:
case ETInt32:
case ETInt64:
case ETFloat:
case ETDouble:
case ETUInt8:
case ETUInt16:
case ETUInt32:
case ETUInt64:
return 1;
default:
return 0;
}
}

#endif /* defined(__Eagle__types__) */
Loading

0 comments on commit 847a281

Please sign in to comment.