1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef ULMAS1_EXPR_H
#define ULMAS1_EXPR_H

#include <stdbool.h>
#include <stdint.h>

#include <utils/loc.h>
#include <utils/ustr.h>

struct Expr;

enum ExprType
{
    ABS = 'a',
    REL_TEXT = 't',
    REL_DATA = 'd',
    REL_BSS = 'b',
    UNKNOWN = 'U',
};

enum ExprBinaryOperand
{
    EO_PLUS = '+',
    EO_MINUS = '-',
    EO_ASTERISK = '*',
    EO_SLASH = '/',
    EO_PERCENT = '%',
};

struct Expr *makeValExpr(struct Loc loc, enum ExprType type, uint64_t val);
struct Expr *makeSymExpr(struct Loc loc, const struct UStr *sym);
struct Expr *makeUnaryMinusExpr(struct Loc loc, const struct Expr *x);
struct Expr *makeUnaryWordExpr(struct Loc loc, size_t word,
                               const struct Expr *x);
struct Expr *makeBinaryExpr(struct Loc loc, enum ExprBinaryOperand exprOperand,
                            const struct Expr *x, const struct Expr *y);

void freeExpr(struct Expr *x);

uint64_t evalExpr(const struct Expr *x);
uint64_t evalExprWithRecGuard(const struct Expr *x, const struct UStr *guard);
enum ExprType typeExpr(const struct Expr *x);
struct Loc locExpr(const struct Expr *x);

bool symInExpr(const struct UStr *ident, const struct Expr *x);
void printTreeExpr(const struct Expr *x);
const char *strExpr(const struct Expr *x);
const char *relocStrExpr(const struct Expr *x);
void printExpr(const struct Expr *x);

#endif // ULMAS1_EXPR_H