Skip to content

Commit

Permalink
Merge branch 'master' of https://forge.ispras.ru/git/microtesk
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtsenkoDev committed Feb 3, 2021
2 parents 5449934 + e043698 commit ad5b780
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import ru.ispras.microtesk.translator.nml.ir.primitive.Instance;
import ru.ispras.microtesk.translator.nml.ir.primitive.Primitive;
import ru.ispras.microtesk.translator.nml.ir.primitive.PrimitiveAnd;
import ru.ispras.microtesk.translator.nml.ir.shared.LetConstant;
import ru.ispras.microtesk.translator.nml.ir.shared.MemoryResource;
import ru.ispras.microtesk.translator.nml.ir.shared.Struct;
import ru.ispras.microtesk.translator.nml.ir.shared.Type;
Expand Down Expand Up @@ -64,7 +65,14 @@ public Expr location(

// Hack to deal with internal variables described by string constants.
if (NmlSymbolKind.LET_CONST == kind) {
final Expr expr = getIr().getConstants().get(name).getExpr();
final LetConstant constant = getIr().getConstants().get(name);
if (null == constant) {
// Forward definition.
raiseError(where, String.format(
"Constant %s is not found. Forward definitions are not allowed.", name));
}

final Expr expr = constant.getExpr();
if (expr.isInternalVariable()) {
return new Expr(expr);
}
Expand All @@ -82,7 +90,7 @@ public Expr location(
? newLocationMemory(where, name, null)
: newLocationArgument(where, name);

return newLocationExpr(location);
return newLocationExpr(where, location);
}

public Expr location(
Expand All @@ -102,7 +110,7 @@ public Expr location(
final Location location = newLocationMemory(where, name, index);
final Location locationField = namedField(where, location, fields);

return newLocationExpr(locationField);
return newLocationExpr(where, locationField);
}

public Expr location(
Expand All @@ -124,7 +132,7 @@ public Expr location(
if (NmlSymbolKind.MEMORY == kind) {
final Location location = newLocationMemory(where, name, null);
final Location locationField = namedField(where, location, fields);
return newLocationExpr(locationField);
return newLocationExpr(where, locationField);
}

final Primitive argument = getThisArgs().get(name);
Expand All @@ -133,7 +141,7 @@ public Expr location(
}

final Location location = argumentField(where, argument, name, fields);
return newLocationExpr(location);
return newLocationExpr(where, location);
}

public Expr location(
Expand All @@ -154,7 +162,7 @@ public Expr location(
final Location location = null;

raiseError(where, "Unsupported construct.");
return newLocationExpr(location);
return newLocationExpr(where, location);
}

private Location namedField(
Expand Down Expand Up @@ -244,7 +252,7 @@ public Expr bitfield(
final Type bitfieldType = type.resize(1);
final Location result = createBitfield(where, location, pos, pos, bitfieldType);

return newLocationExpr(result);
return newLocationExpr(where, result);
}

public Expr bitfield(
Expand All @@ -270,7 +278,7 @@ public Expr bitfield(
final int bitfieldSize = Math.abs(toPos - fromPos) + 1;
final Type bitfieldType = location.getType().resize(bitfieldSize);

return newLocationExpr(createBitfield(
return newLocationExpr(where, createBitfield(
where,
location,
fromPos < toPos ? from : to,
Expand All @@ -293,7 +301,7 @@ public Expr bitfield(
final int bitfieldSize = Math.abs(reducedTo.constant - reducedFrom.constant) + 1;
final Type bitfieldType = location.getType().resize(bitfieldSize);

return newLocationExpr(createBitfield(
return newLocationExpr(where, createBitfield(
where,
location,
reducedFrom.constant < reducedTo.constant ? from : to,
Expand Down Expand Up @@ -357,14 +365,18 @@ private Symbol findSymbol(Where where, String name) throws SemanticException {
return symbol;
}

private static Expr newLocationExpr(final Location source) {
private Expr newLocationExpr(final Where where, final Location source) throws SemanticException {
InvariantChecks.checkNotNull(source);

final NodeInfo nodeInfo = NodeInfo.newLocation(source);

final String name = source.toString();
final DataType dataType = TypeCast.getFortressDataType(nodeInfo.getType());
final Type type = nodeInfo.getType();

if (null == type) {
raiseError(where, String.format("Location %s is of unknown type", name));
}

final DataType dataType = TypeCast.getFortressDataType(type);
final Node node = new NodeVariable(name, dataType);
node.setUserData(nodeInfo);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020 ISP RAS (http://www.ispras.ru)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package ru.ispras.microtesk.translator.nml;

import org.junit.Test;
import ru.ispras.microtesk.translator.TranslatorTest;
import ru.ispras.microtesk.translator.nml.ir.Ir;

public class LetForwardTestCase extends TranslatorTest<Ir> {

@Test
public void test() {
final NmlTranslator translator = new NmlTranslator();
translate(translator, "./src/test/nml/let_forward.nml");
}
}
2 changes: 2 additions & 0 deletions src/test/nml/let_forward.nml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let CONST_1 = CONST_2 // ERROR: Forward definition.
let CONST_2 = 0x1

0 comments on commit ad5b780

Please sign in to comment.