Skip to content

Commit

Permalink
NL Writer: opcode IDs #30
Browse files Browse the repository at this point in the history
gen-expr-info.cc generates them in nl-opcodes.h
  • Loading branch information
glebbelov committed Nov 1, 2023
1 parent eb38ec2 commit 918e231
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Testing

.vscode
/src/expr-info.cc
/nl-writer2/include/mp/nl-opcodes.h
/test/end2end/.vs
/test/end2end/env
/test/end2end/cases/*.lp
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ add_mp_library(format OBJECT

# gen-expr-info
set(MP_EXPR_INFO_FILE ${MP_SOURCE_DIR}/src/expr-info.cc)
set(MP_NL_OPCODES_FILE
${MP_SOURCE_DIR}/nl-writer2/include/mp/nl-opcodes.h)
add_executable(gen-expr-info EXCLUDE_FROM_ALL
src/gen-expr-info.cc $<TARGET_OBJECTS:format>)
if (MINGW)
Expand All @@ -314,12 +316,15 @@ endif ()
if (CMAKE_CROSSCOMPILING)
# Produce a warning because expr-info.cc can be out of date but cannot be
# re-generated because we are cross compiling.
add_custom_command(OUTPUT ${MP_EXPR_INFO_FILE}
add_custom_command(OUTPUT
${MP_EXPR_INFO_FILE} ${MP_NL_OPCODES_FILE}
COMMAND ${CMAKE_COMMAND} -E echo
"warning: cannot re-generate ${MP_EXPR_INFO_FILE}")
"warning: cannot re-generate '${MP_EXPR_INFO_FILE}' and '${MP_NL_OPCODES_FILE}'")
else ()
add_custom_command(OUTPUT ${MP_EXPR_INFO_FILE}
COMMAND ${WINE} $<TARGET_FILE:gen-expr-info> ${MP_EXPR_INFO_FILE}
add_custom_command(OUTPUT
${MP_EXPR_INFO_FILE} ${MP_NL_OPCODES_FILE}
COMMAND ${WINE} $<TARGET_FILE:gen-expr-info>
${MP_EXPR_INFO_FILE} ${MP_NL_OPCODES_FILE}
DEPENDS gen-expr-info)
endif ()
add_to_folder(${MP_FOLDER_PREFIX}util gen-expr-info)
Expand Down
2 changes: 1 addition & 1 deletion include/mp/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ class ExprInfo {
const char *str;
};

// Maximum NL opcode.
/// Maximum NL opcode.
enum { MAX_OPCODE = 82 };

class OpCodeInfo {
Expand Down
42 changes: 23 additions & 19 deletions nl-writer2/examples/nlsol_ex_mdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cassert>

#include "mp/nl-header.h"
#include "mp/nl-opcodes.h"

/**
* @brief The ExampleModel struct.
Expand Down Expand Up @@ -289,30 +290,32 @@ struct ExampleModel {
/// Write dvar expressions.
template <class EWriter>
void WriteDVarExpr(int i, EWriter& ew) const {
using namespace mp::nl;
switch (i) {
case 0: // nl(t3)
{
auto ew01 = ew.OPut1(15, "abs");
{
auto ew0101 = ew01.OPut2(0, "+");
ew0101.NPut(-2);
ew0101.VPut(0, "y");
auto ew01 = ew.OPut1(ABS); // unary opcode
// The argument:
{ // Can also split code / name:
auto ew0101 = ew01.OPut2(ADD.code, "+"); // binary opcode
ew0101.NPut(-2); // 1st arg
ew0101.VPut(0, "y"); // 2nd arg
}
} break;
case 1: // t2
ew.NPut(0); // no non-linear part
break;
case 2: // t3
{
auto ew01 = ew.OPut2(0, "+");
auto ew01 = ew.OPut2(ADD);
ew01.VPut(2, "nl(t3)");
ew01.NPut(6.38);
} break;
case 3: // t1
{
auto ew01 = ew.OPut2(5, "^"); // binary opcode
ew01.VPut(0, "y"); // 1st arg
ew01.NPut(2); // 2nd arg
auto ew01 = ew.OPut2(POW);
ew01.VPut(0, "y");
ew01.NPut(2);
} break;
default:
assert(false);
Expand All @@ -323,44 +326,45 @@ struct ExampleModel {
/// Write constraint non-linear expressions.
template <class EWriter>
void WriteConExpr(int i, EWriter& ew) const {
using namespace mp::nl;
switch (i) {
case 0: // C1_t2
{
auto ew01 = ew.OPut2(2, "*");
auto ew01 = ew.OPut2(MUL);
ew01.NPut(5);
{
auto ew0101 = ew01.OPut2(5, "^");
auto ew0101 = ew01.OPut2(POW);
ew0101.VPut(3, "t2");
ew0101.NPut(2);
}
} break;
case 1: // C2_t1t2t3
{
auto ew01 = ew.OPut2(0, "+");
auto ew01 = ew.OPut2(ADD);
{
auto ew0101 = ew01.OPut2(2, "*");
auto ew0101 = ew01.OPut2(MUL);
ew0101.NPut(-38.2);
{
auto ew010101 = ew0101.OPut2(5, "^");
auto ew010101 = ew0101.OPut2(POW);
ew010101.VPut(0, "y");
ew010101.NPut(2);
}
}
{
auto ew0102 = ew01.OPut2(2, "*");
auto ew0102 = ew01.OPut2(MUL);
{
auto ew010201 = ew0102.OPut2(2, "*");
auto ew010201 = ew0102.OPut2(MUL);
ew010201.NPut(109);
{
auto ew01020101 = ew010201.OPut1(41, "sin");
auto ew01020101 = ew010201.OPut1(SIN);
ew01020101.VPut(3, "t2");
}
}
{
auto ew010202 = ew0102.OPut2(0, "+");
auto ew010202 = ew0102.OPut2(ADD.code, ADD.name);
ew010202.VPut(5, "t1");
{
auto ew01020201 = ew010202.OPut2(5, "^");
auto ew01020201 = ew010202.OPut2(POW);
ew01020201.VPut(4, "t3");
ew01020201.NPut(1.5);
}
Expand Down
28 changes: 28 additions & 0 deletions nl-writer2/include/mp/nl-writer2.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ class NLWriter2 :
ExprArgWriter OPutN(
int opcode, int nArgs, const char* descr="");

/// Shortcut: OPut1( struct Opcode )
template <class Opcode> ExprArgWriter OPut1(Opcode oc)
{ return OPut1(oc.code, oc.name); }
/// Shortcut: OPut2( struct Opcode )
template <class Opcode> ExprArgWriter OPut2(Opcode oc)
{ return OPut2(oc.code, oc.name); }
/// Shortcut: OPut3( struct Opcode )
template <class Opcode> ExprArgWriter OPut3(Opcode oc)
{ return OPut3(oc.code, oc.name); }
/// Shortcut: OPutN( struct Opcode, int nArgs )
template <class Opcode> ExprArgWriter OPutN(
Opcode oc, int nArgs)
{ return OPutN(oc.code, nArgs, oc.name); }

protected:
/// Add an argument explicitly.
/// @return the expr writer for the argument.
Expand Down Expand Up @@ -355,6 +369,20 @@ class NLWriter2 :
ExprArgWriter OPutN(
int opcode, int nArgs, const char* descr="");

/// Shortcut: OPut1( struct Opcode )
template <class Opcode> ExprArgWriter OPut1(Opcode oc)
{ return OPut1(oc.code, oc.name); }
/// Shortcut: OPut2( struct Opcode )
template <class Opcode> ExprArgWriter OPut2(Opcode oc)
{ return OPut2(oc.code, oc.name); }
/// Shortcut: OPut3( struct Opcode )
template <class Opcode> ExprArgWriter OPut3(Opcode oc)
{ return OPut3(oc.code, oc.name); }
/// Shortcut: OPutN( struct Opcode, int nArgs )
template <class Opcode> ExprArgWriter OPutN(
Opcode oc, int nArgs)
{ return OPutN(oc.code, nArgs, oc.name); }

private:
NLWriter2& nlw_;
};
Expand Down
Loading

0 comments on commit 918e231

Please sign in to comment.