Skip to content

Commit

Permalink
fix basic casts (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich authored Aug 8, 2024
1 parent f4d623a commit 47ff0e3
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 71 deletions.
24 changes: 24 additions & 0 deletions nautilus/include/nautilus/common/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,29 @@ constexpr bool isFloat(Type type) {
}
}

constexpr int8_t getBitWith(Type type) {
switch (type) {
case Type::v:
return -1;
case Type::b:
case Type::i8:
case Type::ui8:
return 8;
case Type::i16:
case Type::ui16:
return 16;
case Type::i32:
case Type::ui32:
case Type::f32:
return 32;
case Type::i64:
case Type::ui64:
case Type::f64:
case Type::ptr:
return 64;
}
return -1;
}

std::ostream& operator<<(std::ostream& os, const Type& type);
} // namespace nautilus
57 changes: 57 additions & 0 deletions nautilus/src/nautilus/compiler/backends/bc/BCInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,35 +264,83 @@ static Operation* OpTable[] = {
(Operation*) cast<int8_t, int16_t>,
(Operation*) cast<int8_t, int32_t>,
(Operation*) cast<int8_t, int64_t>,
(Operation*) cast<int16_t, int8_t>,
(Operation*) cast<int16_t, int32_t>,
(Operation*) cast<int16_t, int64_t>,
(Operation*) cast<int32_t, int8_t>,
(Operation*) cast<int32_t, int16_t>,
(Operation*) cast<int32_t, int64_t>,
(Operation*) cast<int64_t, int8_t>,
(Operation*) cast<int64_t, int16_t>,
(Operation*) cast<int64_t, int32_t>,
// uint to int
(Operation*) cast<uint8_t, int8_t>,
(Operation*) cast<uint8_t, int16_t>,
(Operation*) cast<uint8_t, int32_t>,
(Operation*) cast<uint8_t, int64_t>,
(Operation*) cast<uint16_t, int8_t>,
(Operation*) cast<uint16_t, int16_t>,
(Operation*) cast<uint16_t, int32_t>,
(Operation*) cast<uint16_t, int64_t>,
(Operation*) cast<uint32_t, int8_t>,
(Operation*) cast<uint32_t, int16_t>,
(Operation*) cast<uint32_t, int32_t>,
(Operation*) cast<uint32_t, int64_t>,
(Operation*) cast<uint64_t, int8_t>,
(Operation*) cast<uint64_t, int16_t>,
(Operation*) cast<uint64_t, int32_t>,
(Operation*) cast<uint64_t, int64_t>,
// uint to uint
(Operation*) cast<uint8_t, uint16_t>,
(Operation*) cast<uint8_t, uint32_t>,
(Operation*) cast<uint8_t, uint64_t>,
(Operation*) cast<uint16_t, uint8_t>,
(Operation*) cast<uint16_t, uint32_t>,
(Operation*) cast<uint16_t, uint64_t>,
(Operation*) cast<uint32_t, uint8_t>,
(Operation*) cast<uint32_t, uint16_t>,
(Operation*) cast<uint32_t, uint64_t>,
(Operation*) cast<uint64_t, uint8_t>,
(Operation*) cast<uint64_t, uint16_t>,
(Operation*) cast<uint64_t, uint32_t>,
// int to uint
(Operation*) cast<int8_t, uint8_t>,
(Operation*) cast<int8_t, uint16_t>,
(Operation*) cast<int8_t, uint32_t>,
(Operation*) cast<int8_t, uint64_t>,
(Operation*) cast<int16_t, uint8_t>,
(Operation*) cast<int16_t, uint16_t>,
(Operation*) cast<int16_t, uint32_t>,
(Operation*) cast<int16_t, uint64_t>,
(Operation*) cast<int32_t, uint8_t>,
(Operation*) cast<int32_t, uint16_t>,
(Operation*) cast<int32_t, uint32_t>,
(Operation*) cast<int32_t, uint64_t>,
(Operation*) cast<int64_t, uint8_t>,
(Operation*) cast<int64_t, uint16_t>,
(Operation*) cast<int64_t, uint32_t>,
(Operation*) cast<int64_t, uint64_t>,
// from float to int
(Operation*) cast<float, int8_t>,
(Operation*) cast<float, int16_t>,
(Operation*) cast<float, int32_t>,
(Operation*) cast<float, int64_t>,
(Operation*) cast<float, uint8_t>,
(Operation*) cast<float, uint16_t>,
(Operation*) cast<float, uint32_t>,
(Operation*) cast<float, uint64_t>,
(Operation*) cast<float, double>,
// from double to int
(Operation*) cast<double, int8_t>,
(Operation*) cast<double, int16_t>,
(Operation*) cast<double, int32_t>,
(Operation*) cast<double, int64_t>,
(Operation*) cast<double, uint8_t>,
(Operation*) cast<double, uint16_t>,
(Operation*) cast<double, uint32_t>,
(Operation*) cast<double, uint64_t>,
(Operation*) cast<double, float>,
// from int to floats
(Operation*) cast<int8_t, float>,
(Operation*) cast<int8_t, double>,
(Operation*) cast<int16_t, float>,
Expand All @@ -301,6 +349,15 @@ static Operation* OpTable[] = {
(Operation*) cast<int32_t, double>,
(Operation*) cast<int64_t, float>,
(Operation*) cast<int64_t, double>,
// uint to float
(Operation*) cast<uint8_t, float>,
(Operation*) cast<uint8_t, double>,
(Operation*) cast<uint16_t, float>,
(Operation*) cast<uint16_t, double>,
(Operation*) cast<uint32_t, float>,
(Operation*) cast<uint32_t, double>,
(Operation*) cast<uint64_t, float>,
(Operation*) cast<uint64_t, double>,
// FUNCTION CALLS
(Operation*) dyncallReset,
(Operation*) dyncallArgB,
Expand Down
197 changes: 190 additions & 7 deletions nautilus/src/nautilus/compiler/backends/bc/BCLoweringProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,12 +1334,18 @@ void BCLoweringProvider::LoweringContext::process(ir::CastOperation* castOp, sho
}
} else if (srcType == Type::i16) {
switch (tar) {
case Type::i8:
bc = ByteCode::CAST_i16_i8;
break;
case Type::i32:
bc = ByteCode::CAST_i16_i32;
break;
case Type::i64:
bc = ByteCode::CAST_i16_i64;
break;
case Type::ui8:
bc = ByteCode::CAST_i16_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_i16_ui16;
break;
Expand All @@ -1360,9 +1366,21 @@ void BCLoweringProvider::LoweringContext::process(ir::CastOperation* castOp, sho
}
} else if (srcType == Type::i32) {
switch (tar) {
case Type::i8:
bc = ByteCode::CAST_i32_i8;
break;
case Type::i16:
bc = ByteCode::CAST_i32_i16;
break;
case Type::i64:
bc = ByteCode::CAST_i32_i64;
break;
case Type::ui8:
bc = ByteCode::CAST_i32_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_i32_ui64;
break;
case Type::ui32:
bc = ByteCode::CAST_i32_ui32;
break;
Expand All @@ -1380,6 +1398,24 @@ void BCLoweringProvider::LoweringContext::process(ir::CastOperation* castOp, sho
}
} else if (srcType == Type::i64) {
switch (tar) {
case Type::i8:
bc = ByteCode::CAST_i64_i8;
break;
case Type::i16:
bc = ByteCode::CAST_i64_i16;
break;
case Type::i32:
bc = ByteCode::CAST_i64_i32;
break;
case Type::ui8:
bc = ByteCode::CAST_i64_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_i64_ui16;
break;
case Type::ui32:
bc = ByteCode::CAST_i64_ui32;
break;
case Type::ui64:
bc = ByteCode::CAST_i64_ui64;
break;
Expand All @@ -1403,6 +1439,9 @@ void BCLoweringProvider::LoweringContext::process(ir::CastOperation* castOp, sho
case Type::ui64:
bc = ByteCode::CAST_ui8_ui64;
break;
case Type::i8:
bc = ByteCode::CAST_ui8_i8;
break;
case Type::i16:
bc = ByteCode::CAST_ui8_i16;
break;
Expand All @@ -1412,34 +1451,178 @@ void BCLoweringProvider::LoweringContext::process(ir::CastOperation* castOp, sho
case Type::i64:
bc = ByteCode::CAST_ui8_i64;
break;
case Type::f32:
bc = ByteCode::CAST_ui8_f;
break;
case Type::f64:
bc = ByteCode::CAST_ui8_d;
break;
default:
throw NotImplementedException("This type is not supported.");
}
} else if (srcType == Type::ui16) {
switch (tar) {
case Type::ui8:
bc = ByteCode::CAST_ui16_ui8;
break;
case Type::ui32:
bc = ByteCode::CAST_ui16_ui32;
break;
case Type::ui64:
bc = ByteCode::CAST_ui16_ui64;
break;
case Type::i8:
bc = ByteCode::CAST_ui16_i8;
break;
case Type::i16:
bc = ByteCode::CAST_ui16_i16;
break;
case Type::i32:
bc = ByteCode::CAST_ui16_i32;
break;
case Type::i64:
bc = ByteCode::CAST_ui16_i64;
break;
case Type::f32:
bc = ByteCode::CAST_ui16_f;
break;
case Type::f64:
bc = ByteCode::CAST_ui16_d;
break;
default:
throw NotImplementedException("This type is not supported.");
}
} else if (srcType == Type::ui32) {
switch (tar) {
case Type::ui8:
bc = ByteCode::CAST_ui32_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_ui32_ui16;
break;
case Type::ui64:
bc = ByteCode::CAST_ui32_ui64;
break;
case Type::i8:
bc = ByteCode::CAST_ui32_i8;
break;
case Type::i16:
bc = ByteCode::CAST_ui32_i16;
break;
case Type::i32:
bc = ByteCode::CAST_ui32_i32;
break;
case Type::i64:
bc = ByteCode::CAST_ui32_i64;
break;
case Type::f32:
bc = ByteCode::CAST_ui32_f;
break;
case Type::f64:
bc = ByteCode::CAST_ui32_d;
break;
default:
throw NotImplementedException("This type is not supported.");
}

} else if (srcType == Type::ui64) {
switch (tar) {
case Type::ui8:
bc = ByteCode::CAST_ui64_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_ui64_ui16;
break;
case Type::ui32:
bc = ByteCode::CAST_ui64_ui32;
break;
case Type::i8:
bc = ByteCode::CAST_ui64_i8;
break;
case Type::i16:
bc = ByteCode::CAST_ui64_i16;
break;
case Type::i32:
bc = ByteCode::CAST_ui64_i32;
break;
case Type::i64:
bc = ByteCode::CAST_ui64_i64;
break;
case Type::f32:
bc = ByteCode::CAST_ui64_f;
break;
case Type::f64:
bc = ByteCode::CAST_ui64_d;
break;
default:
throw NotImplementedException("This type is not supported.");
}
} else if (srcType == Type::ui32 && tar == Type::ui64) {
bc = ByteCode::CAST_ui32_ui64;
} else if (srcType == Type::ui32 && tar == Type::i64) {
bc = ByteCode::CAST_ui32_i64;
} else if (srcType == Type::f32 && tar == Type::f64) {
bc = ByteCode::CAST_f_d;
} else {

} else if (srcType == Type::f32) {
switch (tar) {
case Type::ui8:
bc = ByteCode::CAST_f_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_f_ui16;
break;
case Type::ui32:
bc = ByteCode::CAST_f_ui32;
break;
case Type::ui64:
bc = ByteCode::CAST_f_ui64;
break;
case Type::i8:
bc = ByteCode::CAST_f_i8;
break;
case Type::i16:
bc = ByteCode::CAST_f_i16;
break;
case Type::i32:
bc = ByteCode::CAST_f_i32;
break;
case Type::i64:
bc = ByteCode::CAST_f_i64;
break;
case Type::f64:
bc = ByteCode::CAST_f_d;
break;
default:
throw NotImplementedException("This type is not supported.");
}
} else if (srcType == Type::f64) {
switch (tar) {
case Type::ui8:
bc = ByteCode::CAST_d_ui8;
break;
case Type::ui16:
bc = ByteCode::CAST_d_ui16;
break;
case Type::ui32:
bc = ByteCode::CAST_d_ui32;
break;
case Type::ui64:
bc = ByteCode::CAST_d_ui64;
break;
case Type::i8:
bc = ByteCode::CAST_d_i8;
break;
case Type::i16:
bc = ByteCode::CAST_d_i16;
break;
case Type::i32:
bc = ByteCode::CAST_d_i32;
break;
case Type::i64:
bc = ByteCode::CAST_d_i64;
break;
case Type::f32:
bc = ByteCode::CAST_d_f;
break;
default:
throw NotImplementedException("This type is not supported.");
}
} else {
throw NotImplementedException("This type is not supported.");
}
OpCode oc = {bc, input, -1, resultReg};
Expand Down
Loading

0 comments on commit 47ff0e3

Please sign in to comment.