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

Add V1xGenerator and V3xGenerator. #1

Merged
merged 8 commits into from
Jan 24, 2024
20 changes: 9 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ include(GNUInstallDirs)
# Include the generate_tree functions
include(cmake/generate_funcs.cmake)

add_executable(func-gen)
add_executable(${PROJECT_NAME})

add_subdirectory(src)

target_include_directories(func-gen
target_include_directories(${PROJECT_NAME}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/"
)

target_compile_features(func-gen PRIVATE
cxx_std_11
target_compile_features(${PROJECT_NAME} PRIVATE
cxx_std_23
)

# fPIC: otherwise some weirdness happens with pthreads or something when linking statically.
if(CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(func-gen PRIVATE
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall -Wextra -Werror -Wfatal-errors
-fPIC
-Wno-error=deprecated-declarations
-Wno-error=restrict
-Wno-error=sign-compare
)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(func-gen PRIVATE
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall -Wextra -Werror -Wfatal-errors
-fPIC
-Wno-error=sign-compare
-Wno-error=unused-private-field
-Wno-error=unused-but-set-variable
)
elseif(MSVC)
target_compile_options(func-gen PRIVATE
target_compile_options(${PROJECT_NAME} PRIVATE
/WX
/D_CRT_NONSTDC_NO_DEPRECATE
/D_CRT_SECURE_NO_WARNINGS
Expand All @@ -68,15 +68,14 @@ if(ASAN_ENABLED)
endif()
endif()


#=============================================================================#
# Installation #
#=============================================================================#

# Install the generator tool only if this is the top level project
if (${CMAKE_PROJECT_NAME} STREQUAL func-gen)
if (${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
install(
TARGETS func-gen
TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
Expand All @@ -86,5 +85,4 @@ if (${CMAKE_PROJECT_NAME} STREQUAL func-gen)
)
endif()


endif() # NOT TARGET ${PROJECT_NAME}
3 changes: 1 addition & 2 deletions conan/profiles/debug
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
include(default)

[settings]
compiler.cppstd=17
compiler.cppstd=23
func-gen/*:build_type=Debug

[options]
func-gen/*:asan_enabled=False
func-gen/*:build_tests=False
3 changes: 1 addition & 2 deletions conan/profiles/release
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
include(default)

[settings]
compiler.cppstd=17
compiler.cppstd=23
func-gen/*:build_type=Release

[options]
func-gen/*:asan_enabled=False
func-gen/*:build_tests=False
3 changes: 1 addition & 2 deletions conan/profiles/tests-debug
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
include(default)

[settings]
compiler.cppstd=17
compiler.cppstd=23
func-gen/*:build_type=Debug

[options]
func-gen/*:asan_enabled=True
func-gen/*:build_tests=True
3 changes: 1 addition & 2 deletions conan/profiles/tests-release
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
include(default)

[settings]
compiler.cppstd=17
compiler.cppstd=23
func-gen/*:build_type=Release

[options]
func-gen/*:asan_enabled=True
func-gen/*:build_tests=True
63 changes: 25 additions & 38 deletions include/generator.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include "function.hpp" // Function

#include <fstream>
#include <string>
#include <string_view>
#include <vector>

#include "function.hpp" // Function


/**
* Namespace for the \ref func-gen program.
Expand All @@ -16,6 +17,7 @@ namespace func_gen {
* Generator class.
*/
class Generator {
protected:
/**
* Vector of all functions generated so far.
*/
Expand All @@ -39,52 +41,38 @@ class Generator {
/**
* Generates the function that registers the functions in a FunctionTable.
*/
void generate_register_function();
void generate_register_default_functions_into();

/**
* Generates the function header in both the header and source file. The
* start of the function body must follow. This also registers the function
* to be added to the function that populates FunctionTables.
* Generates the function header in both the header and source file.
* The start of the function body must follow.
* This also registers the function to be added to the function that populates FunctionTables.
*/
void generate_impl_header(const Function &func);

/**
* Like generate_impl_header(), but in addition, generates some boilerplate
* code for safely casting all the arguments to constant values (throwing
* a compile error if they're not constant) and storing them in variables
* a..z. This is, of course, only applicable for functions that are only
* evaluable during semantic analysis.
* Like generate_impl_header(), but in addition,
* generates some boilerplate code for safely casting all the arguments to constant values
* (throwing a compile error if they're not constant) and
* storing them in variables a..z.
* This is, of course, only applicable for functions that are only evaluable during semantic analysis.
*/
void generate_const_impl_header(const Function &func);
virtual void generate_const_impl_header(const Function &func) = 0;

/**
* Generates the end of a function body, with an appropriate return
* statement. return_type must be one of:
*
* - 'b': returns a ConstBool, return_expr must be a primitive::Bool.
* - 'a': returns a ConstAxis, return_expr must be a primitive::Axis.
* - 'i': returns a ConstInt, return_expr must be a primitive::Int.
* - 'r': returns a ConstReal, return_expr must be a primitive::Real.
* - 'c': returns a ConstComplex, return_expr must be a primitive::Complex.
* - 'm': returns a ConstRealMatrix, return_expr must be a primitive::RMatrix.
* - 'n': returns a ConstComplexMatrix, return_expr must be a primitive::CMatrix.
* - 's': returns a ConstString, return_expr must be a primitive::Str.
* - 'j': returns a ConstJson, return_expr must be a primitive::Str.
* - 'Q': returns a QubitRefs, return_expr must be a Many<ConstInt>.
* - 'B': returns a BitRefs, return_expr must be a Many<ConstInt>.
* Generates the end of a function body, with an appropriate return statement.
*/
void generate_impl_footer(const std::string &return_expr, char return_type);
virtual void generate_impl_footer(const std::string &return_expr, char return_type) = 0;

public:

/**
* Generates a basic constant scalar function, such as integer addition for
* instance. name must be the cQASM name for the function (either just the
* function name or operator+ etc), return_type must be the return type
* code, arg_types must be the argument type codes, and impl must be a
* C++ expression implementing the function, operating on variables a..z
* representing the arguments, and returning the primitive value associated
* with return_type.
* Generates a basic constant scalar function, such as integer addition for instance.
* name must be the cQASM name for the function (either just the function name or operator+ etc),
* return_type must be the return type code,
* arg_types must be the argument type codes, and
* impl must be a C++ expression implementing the function,
* operating on variables a..z representing the arguments, and
* returning the primitive value associated with return_type.
*/
void generate_const_scalar_op(
const std::string &name,
Expand All @@ -98,13 +86,12 @@ class Generator {
Generator(
const std::string &header_filename,
const std::string &source_filename,
const std::string &version);
std::string_view version);

/**
* Finishes writing the header & source file, then destroys the generator.
*/
~Generator();

virtual ~Generator();
};

} // namespace func_gen
58 changes: 58 additions & 0 deletions include/v1x/generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include "../generator.hpp"

#include <string_view>


/**
* Namespace for the \ref func-gen program.
*/
namespace func_gen {

constinit const std::string_view v1x_version{ "v1x" };

/**
* Generator class.
*/
class V1xGenerator : public Generator {
/**
* Like generate_impl_header(), but in addition,
* generates some boilerplate code for safely casting all the arguments to constant values
* (throwing a compile error if they're not constant) and
* storing them in variables a..z.
* This is, of course, only applicable for functions that are only evaluable during semantic analysis.
*/
void generate_const_impl_header(const Function &func) override;

/**
* Generates the end of a function body, with an appropriate return statement.
* return_type must be one of:
*
* - 'a': returns a ConstAxis, return_expr must be a primitive::Axis.
* - 'b': returns a ConstBool, return_expr must be a primitive::Bool.
* - 'i': returns a ConstInt, return_expr must be a primitive::Int.
* - 'r': returns a ConstReal, return_expr must be a primitive::Real.
* - 'c': returns a ConstComplex, return_expr must be a primitive::Complex.
* - 'm': returns a ConstRealMatrix, return_expr must be a primitive::RMatrix.
* - 'n': returns a ConstComplexMatrix, return_expr must be a primitive::CMatrix.
* - 's': returns a ConstString, return_expr must be a primitive::Str.
* - 'j': returns a ConstJson, return_expr must be a primitive::Str.
* - 'Q': returns a QubitRefs, return_expr must be a Many<ConstInt>.
* - 'B': returns a BitRefs, return_expr must be a Many<ConstInt>.
*/
void generate_impl_footer(const std::string &return_expr, char return_type) override;

public:
/**
* Constructs a v1x generator for the function table.
*/
V1xGenerator(const std::string &header_filename, const std::string &source_filename);

/**
* Finishes writing the header & source file, then destroys the v1x generator.
*/
~V1xGenerator() override = default;
};

} // namespace func_gen
56 changes: 56 additions & 0 deletions include/v3x/generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include "../generator.hpp"

#include <string_view>


/**
* Namespace for the \ref func-gen program.
*/
namespace func_gen {

constinit const std::string_view v3x_version{ "v3x" };

/**
* Generator class.
*/
class V3xGenerator : public Generator {
/**
* Like generate_impl_header(), but in addition,
* generates some boilerplate code for safely casting all the arguments to constant values
* (throwing a compile error if they're not constant) and
* storing them in variables a..z.
* This is, of course, only applicable for functions that are only evaluable during semantic analysis.
*/
void generate_const_impl_header(const Function &func) override;

/**
* Generates the end of a function body, with an appropriate return statement.
* return_type must be one of:
*
* - 'a': returns a ConstAxis, return_expr must be a primitive::Axis.
* - 'b': returns a ConstBool, return_expr must be a primitive::Bool.
* - 'i': returns a ConstInt, return_expr must be a primitive::Int.
* - 'r': returns a ConstReal, return_expr must be a primitive::Real.
* - 'c': returns a ConstComplex, return_expr must be a primitive::Complex.
* - 'X': returns a ConstBoolArray, return_expr must be a Many<ConstBool>.
* - 'Y': returns a ConstIntArray, return_expr must be a Many<ConstInt>.
* - 'Z': returns a ConstRealArray, return_expr must be a Many<ConstReal>.
* - 'I': returns an IndexRef, return_expr must be a Many<ConstReal>.
*/
void generate_impl_footer(const std::string &return_expr, char return_type) override;

public:
/**
* Constructs a v3x generator for the function table.
*/
V3xGenerator(const std::string &header_filename, const std::string &source_filename);

/**
* Finishes writing the header & source file, then destroys the v3x generator.
*/
~V3xGenerator() override = default;
};

} // namespace func_gen
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target_sources(${PROJECT_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/generator.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
)
add_subdirectory(v1x)
add_subdirectory(v3x)
Loading
Loading