Skip to content

Commit

Permalink
Adding support for fxd-point arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
mwolf76 committed Jan 16, 2013
1 parent 99799d8 commit e67ac77
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/expr/expr.hh
Expand Up @@ -55,6 +55,8 @@ typedef enum {
HCONST, // hex constants HCONST, // hex constants
OCONST, // octal constants OCONST, // octal constants


FCONST, // fract constant

IDENT, DOT, IDENT, DOT,


NIL, // reserved NIL, // reserved
Expand Down
30 changes: 24 additions & 6 deletions src/expr/expr_mgr.cc
Expand Up @@ -36,14 +36,32 @@ ExprMgr::ExprMgr()
false_expr = make_identifier(FALSE_TOKEN); false_expr = make_identifier(FALSE_TOKEN);
true_expr = make_identifier(TRUE_TOKEN); true_expr = make_identifier(TRUE_TOKEN);


// unsigned and signed base integer types identifiers // integer types identifiers
unsigned_expr = make_identifier(UNSIGNED_TOKEN); {
signed_expr = make_identifier(SIGNED_TOKEN); ostringstream oss; oss << UNSIGNED_TOKEN << " " << INT_TOKEN;
fixed_expr = make_identifier(FIXED_TOKEN); unsigned_int_expr = make_identifier(oss.str());
integer_expr = make_identifier(INTEGER_TOKEN); // abstract }
{
ostringstream oss; oss << SIGNED_TOKEN << " " << INT_TOKEN;
signed_int_expr = make_identifier(oss.str());
}
int_expr = make_identifier(INT_TOKEN);

// fixed-point real types identifiers
{
ostringstream oss; oss << UNSIGNED_TOKEN << " " << FXD_TOKEN;
unsigned_fxd_expr = make_identifier(oss.str());
}
{
ostringstream oss; oss << SIGNED_TOKEN << " " << FXD_TOKEN;
signed_fxd_expr = make_identifier(oss.str());
}
fxd_expr = make_identifier(FXD_TOKEN);


// main module identifier
main_expr = make_identifier(MAIN_TOKEN); main_expr = make_identifier(MAIN_TOKEN);
enum_expr = make_identifier(ENUM_TOKEN);
inst_expr = make_identifier(INST_TOKEN);
array_expr = make_identifier (ARRAY_TOKEN);


DEBUG << "ExprMgr @" << this << " initialized" << endl; DEBUG << "ExprMgr @" << this << " initialized" << endl;
} }
Expand Down
111 changes: 91 additions & 20 deletions src/expr/expr_mgr.hh
Expand Up @@ -204,31 +204,84 @@ public:
inline Expr_ptr make_boolean_type() const inline Expr_ptr make_boolean_type() const
{ return bool_expr; } { return bool_expr; }


inline Expr_ptr make_integer_type() const inline Expr_ptr make_int_const_type() const
{ return integer_expr; } { return int_expr; }

inline Expr_ptr make_fxd_const_type() const
{ return fxd_expr; }


inline Expr_ptr make_range_type(Expr_ptr a, Expr_ptr b) inline Expr_ptr make_range_type(Expr_ptr a, Expr_ptr b)
{ {
assert(is_numeric(a)); assert(is_int_numeric(a));
assert(is_numeric(b)); assert(is_int_numeric(b));
return make_expr(RANGE, a, b); return make_expr(RANGE, a, b);
} }


inline Expr_ptr make_unsigned_type(unsigned bits) inline Expr_ptr make_abstract_unsigned_int_type()
{ return make_params(unsigned_expr, make_iconst((value_t) bits)); } {
return unsigned_int_expr;
}

inline Expr_ptr make_unsigned_int_type(unsigned digits)
{
return make_params(unsigned_int_expr,
make_iconst((value_t) digits));
}

inline Expr_ptr make_abstract_signed_int_type()
{
return signed_int_expr;
}

inline Expr_ptr make_signed_int_type(unsigned digits)
{
return make_params(signed_int_expr,
make_iconst((value_t) digits));
}

inline Expr_ptr make_abstract_unsigned_fxd_type()
{
return unsigned_fxd_expr;
}

inline Expr_ptr make_unsigned_fxd_type(unsigned int_digits,
unsigned fract_digits)
{
return make_params(unsigned_fxd_expr,
make_comma(make_iconst((value_t) int_digits),
make_iconst((value_t) fract_digits)));
}


inline Expr_ptr make_signed_type(unsigned bits) inline Expr_ptr make_abstract_signed_fxd_type()
{ return make_params(signed_expr, make_iconst((value_t) bits)); } {
return signed_fxd_expr;
}


inline Expr_ptr make_fixed_type(unsigned int_digits, unsigned fract_digits) inline Expr_ptr make_signed_fxd_type(unsigned int_digits,
unsigned fract_digits)
{ {
return make_params(fixed_expr, return make_params(signed_fxd_expr,
make_comma(make_iconst((value_t) int_digits), make_comma(make_iconst((value_t) int_digits),
make_iconst((value_t) fract_digits))); make_iconst((value_t) fract_digits)));
} }


inline Expr_ptr make_abstract_enum_type()
{
return enum_expr;
}

Expr_ptr make_enum_type(ExprSet& literals); Expr_ptr make_enum_type(ExprSet& literals);


inline Expr_ptr make_abstract_inst_type()
{
return inst_expr;
}

inline Expr_ptr make_abstract_array_type()
{
return array_expr;
}

/* builtin identifiers */ /* builtin identifiers */
inline Expr_ptr make_main() const inline Expr_ptr make_main() const
{ return main_expr; } { return main_expr; }
Expand Down Expand Up @@ -282,13 +335,18 @@ public:
return expr->f_symb == NEXT; return expr->f_symb == NEXT;
} }


inline bool is_numeric(const Expr_ptr expr) const { inline bool is_int_numeric(const Expr_ptr expr) const {
assert(expr); assert(expr);
return (expr->f_symb == ICONST) return (expr->f_symb == ICONST)
|| (expr->f_symb == HCONST) || (expr->f_symb == HCONST)
|| (expr->f_symb == OCONST) ; || (expr->f_symb == OCONST) ;
} }


inline bool is_fxd_numeric(const Expr_ptr expr) const {
assert(expr);
return (expr->f_symb == FCONST);
}

// expr inspectors, used by compiler as helpers to determine operands type // expr inspectors, used by compiler as helpers to determine operands type
inline bool is_unary_logical(const Expr_ptr expr) const { inline bool is_unary_logical(const Expr_ptr expr) const {
assert(expr); assert(expr);
Expand Down Expand Up @@ -402,22 +460,35 @@ private:


/* -- data ------------------------------------------------------------- */ /* -- data ------------------------------------------------------------- */


// temporal exprs type // // temporal exprs type
Expr_ptr temporal_expr; // Expr_ptr temporal_expr;


// boolean exprs type and constants /* boolean exprs type and constants */
Expr_ptr bool_expr; Expr_ptr bool_expr;
Expr_ptr false_expr; Expr_ptr false_expr;
Expr_ptr true_expr; Expr_ptr true_expr;


// main module /* main module */
Expr_ptr main_expr; Expr_ptr main_expr;


// base for (un-)signed integer /* reserved for abstract enum types */
Expr_ptr unsigned_expr; Expr_ptr enum_expr;
Expr_ptr signed_expr;
Expr_ptr fixed_expr; /* reserved for abstract instance types */
Expr_ptr integer_expr; // reserved for abstract integer-type Expr_ptr inst_expr;

/* reserved for abstract array types */
Expr_ptr array_expr;

/* integers */
Expr_ptr unsigned_int_expr;
Expr_ptr signed_int_expr;
Expr_ptr int_expr;

/* fixed-point reals */
Expr_ptr unsigned_fxd_expr;
Expr_ptr signed_fxd_expr;
Expr_ptr fxd_expr;


/* shared pools */ /* shared pools */
ExprPool f_expr_pool; ExprPool f_expr_pool;
Expand Down
6 changes: 3 additions & 3 deletions src/expr/tests/test_expr.cc
Expand Up @@ -73,11 +73,11 @@ BOOST_AUTO_TEST_CASE(expr_makers)
BOOST_CHECK (x_ite_y->f_symb == ITE && x_ite_y->u.f_lhs == x && x_ite_y->u.f_rhs == y); BOOST_CHECK (x_ite_y->f_symb == ITE && x_ite_y->u.f_lhs == x && x_ite_y->u.f_rhs == y);


Expr_ptr iconst_42 = em.make_iconst(42); Expr_ptr iconst_42 = em.make_iconst(42);
BOOST_CHECK (em.is_numeric(iconst_42) && iconst_42->value() == 42); BOOST_CHECK (em.is_int_numeric(iconst_42) && iconst_42->value() == 42);
Expr_ptr hconst_42 = em.make_hconst(0x2a); Expr_ptr hconst_42 = em.make_hconst(0x2a);
BOOST_CHECK (em.is_numeric(hconst_42) && hconst_42->value() == 42); BOOST_CHECK (em.is_int_numeric(hconst_42) && hconst_42->value() == 42);
Expr_ptr oconst_42 = em.make_oconst(052); Expr_ptr oconst_42 = em.make_oconst(052);
BOOST_CHECK (em.is_numeric(oconst_42) && oconst_42->value() == 42); BOOST_CHECK (em.is_int_numeric(oconst_42) && oconst_42->value() == 42);


Expr_ptr x_dot_y = em.make_dot(x, y); Expr_ptr x_dot_y = em.make_dot(x, y);
BOOST_CHECK (x_dot_y->f_symb == DOT && x_dot_y->u.f_lhs == x && x_dot_y->u.f_rhs == y); BOOST_CHECK (x_dot_y->f_symb == DOT && x_dot_y->u.f_lhs == x && x_dot_y->u.f_rhs == y);
Expand Down

0 comments on commit e67ac77

Please sign in to comment.