Skip to content

Commit

Permalink
core/fw/{classgen,dev_objs}: replace calling macros with functions
Browse files Browse the repository at this point in the history
Get some type safety.
  • Loading branch information
vs49688 committed May 13, 2024
1 parent c0e0897 commit ec7eabb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
7 changes: 5 additions & 2 deletions core/fw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ target_include_directories(brender-inc-ddi INTERFACE
)

add_custom_command(
OUTPUT dev_objs.cgh
OUTPUT dev_objs.cgh dev_objs.c
# This will work with MSVC and GCC
COMMAND "${CMAKE_C_COMPILER}" -D__CLASSGEN__ -E "${CMAKE_CURRENT_SOURCE_DIR}/dev_objs.hpp" > dev_objs.tmp
COMMAND "${PERL_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/classgen.pl" dev_objs dev_objs.tmp dev_objs.cgh
COMMAND "${PERL_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/classgen.pl" dev_objs dev_objs.tmp dev_objs.cgh dev_objs.c
MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/dev_objs.hpp"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/classgen.pl"
BYPRODUCTS dev_objs.tmp
)

set(GENERATED_FILES
${CMAKE_CURRENT_BINARY_DIR}/dev_objs.cgh
${CMAKE_CURRENT_BINARY_DIR}/dev_objs.c
)

set(FW_DEV_FILES
Expand Down Expand Up @@ -109,6 +110,8 @@ set(FW_FILES

fw.h
fw_ip.h

${CMAKE_CURRENT_BINARY_DIR}/dev_objs.c
)

add_library(fw OBJECT
Expand Down
53 changes: 39 additions & 14 deletions core/fw/classgen.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# makes alot of assumptions about the layout of the C++ file
#

die "Usage: $0 <header-define> <input.hpp> <output.cgh>\n" unless @ARGV == 3;
die "Usage: $0 <header-define> <input.hpp> <output.cgh> <output.c>\n" unless @ARGV == 4;

my ($header_define, $input_file, $output_file) = @ARGV;
my ($header_define, $input_file, $output_h, $output_c) = @ARGV;

open(INPUT, '<', $input_file);

Expand Down Expand Up @@ -110,7 +110,19 @@
# Start building output file
#
open(STDOUT, '>', $output_file);
open(my $fh_csource, '>', $output_c);
print $fh_csource <<END;
/*
* Automatically generated by classgen.pl
*/
#include "brddi.h"
#include "priminfo.h"

#define class

END

open(STDOUT, '>', $output_h);

print <<END;
/*
Expand Down Expand Up @@ -179,22 +191,33 @@ END
print "};\n";
};


# Generate C macros for calling methods
#
foreach $method (sort(keys(%method_return))) {
@args = split(",",$method_args{$method});
($m_class, $m_name) = split("::",$method);
foreach my $method (sort(keys(%method_return))) {
(my $m_class, my $m_name) = split("::",$method);
my $m_return = $method_return{$method};

# Construct comma seperated list of arguments for macro
#
$alist = "";
foreach $a ((1 .. @args)) {
$alist .= ", a$a";
}
my $fn_args = $method_args{$method}; # "int a, int b, char *c"
my $call_args = join(", ", $fn_args =~ /(\w+)\s*(?:,|$)/g); # "a, b, c"

$fn_args = ", $fn_args" if $fn_args ne '';
$call_args = ", $call_args" if $call_args ne '';

my $define_name = "$class_macro_names{$m_class}\u$m_name";
my $return_prefix = $method_return{$method} eq 'void' ? "" : "return ";

$define_name = "$class_macro_names{$m_class}\u$m_name";
print <<END;
$m_return $define_name(void *self$fn_args);
END

print "#define $define_name(self$alist) BR_CMETHOD_CALL($m_class,$m_name,self)(($m_class *)self$alist)\n";
print $fh_csource <<END;
$m_return $define_name(void *self$fn_args)
{
${return_prefix}BR_CMETHOD_CALL($m_class,$m_name,self)(($m_class *)self$call_args);
}

END
}

# Generate templates for dispatch tables
Expand Down Expand Up @@ -238,3 +261,5 @@ END
#endif
END


close($fh_csource);

0 comments on commit ec7eabb

Please sign in to comment.