Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix constant expressions sometimes having the wrong value #1198

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions compiler/mir/datatables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ proc cmp(a, b: ConstrTree): bool =
exprStructuralEquivalent(a.lit, b.lit)
of mnkProc:
a.prc == b.prc
else:
# all other nodes are equal when their kind is the same
true
of mnkConstr, mnkObjConstr:
a.len == b.len
of mnkField:
a.field.id == b.field.id
of mnkArg, mnkEnd:
true # same node kind -> equal nodes
of AllNodeKinds - ConstrTreeNodes:
unreachable(a.kind)

if not a[0].typ.sameBackendType(b[0].typ) or a.len != b.len:
# the (backend-)type is different -> not the same constant expressions
Expand Down
87 changes: 87 additions & 0 deletions tests/compiler/tdatatables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
discard """
description: "Tests for the compiler/mir/datatables module"
targets: native
"""

import compiler/ast/ast
include compiler/mir/datatables

# some placeholder types to assing to the nodes. For object types, a different
# ID means that it's a different type
let
t1 = PType(itemId: ItemId(item: 1), kind: tyObject, sons: @[PType nil])
t2 = PType(itemId: ItemId(item: 2), kind: tyObject, sons: @[PType nil])
t3 = PType(itemId: ItemId(item: 3), kind: tyObject, sons: @[PType nil])
field1 = PSym(itemId: ItemId(item: 1))
field2 = PSym(itemId: ItemId(item: 2))

# node constructor
template node(k: MirNodeKind, t: PType, field, val: untyped): MirNode =
MirNode(kind: k, typ: t, field: val)
template node(k: MirNodeKind, field, val: untyped): MirNode =
MirNode(kind: k, field: val)
template node(k: MirNodeKind): MirNode =
MirNode(kind: k)
template literal(val: PNode): MirNode =
MirNode(kind: mnkLiteral, lit: val)

block tree_equality:
# the type is only relevant for the head of the tree (the first node)

# setup a list of structurally valid and unique (in terms of equality) trees
let trees = @[
# --- literals
@[node(mnkLiteral, t1, lit, newIntNode(nkIntLit, 0))],
@[node(mnkLiteral, t2, lit, newIntNode(nkIntLit, 0))],
@[node(mnkLiteral, t1, lit, newStrNode(nkStrLit, ""))],
@[node(mnkLiteral, t1, lit, newStrNode(nkStrLit, "a"))],
@[node(mnkLiteral, t1, lit, newFloatNode(nkFloatLit, 0.0))],
# 0.0 and -0.0 are different float values
# FIXME: doesn't work yet
#@[node(mnkLiteral, t1, lit, newFloatNode(nkFloatLit, -0.0))],

# --- ordered aggregates
@[node(mnkConstr, t1, len, 0), node(mnkEnd)],
@[node(mnkConstr, t2, len, 0), node(mnkEnd)],
@[node(mnkConstr, t1, len, 1),
node(mnkArg), literal(newIntNode(nkIntLit, 0)),
node(mnkEnd)],
@[node(mnkConstr, t1, len, 2),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkEnd)],

# --- aggregates with fields
@[node(mnkObjConstr, t1, len, 0), node(mnkEnd)],
@[node(mnkObjConstr, t2, len, 0), node(mnkEnd)],
@[node(mnkObjConstr, t1, len, 1),
node(mnkField, field, field1),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkEnd)],
# same field value, different field:
@[node(mnkObjConstr, t1, len, 1),
node(mnkField, field, field2),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkEnd)],
@[node(mnkObjConstr, t1, len, 1),
node(mnkField, field, field1),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkField, field, field2),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkEnd)],
# swapped fields
@[node(mnkObjConstr, t1, len, 1),
node(mnkField, field, field2),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkField, field, field1),
node(mnkArg), literal(newIntNode(nkIntLit, 0)), node(mnkEnd),
node(mnkEnd)]
]

# compare all trees with each other
for i in 0..<trees.len:
doAssert cmp(trees[i], trees[i]) # tree must be equal to itself
for j in (i+1)..<trees.len:
if cmp(trees[i], trees[j]):
echo "compared equal, but shouldn't: ", i, " vs. ", j
doAssert false
Loading