Skip to content

Commit f3b0e51

Browse files
Merge pull request #3432 from xlsynth:meheff/2025-11-26-outstanding-vast-changes
PiperOrigin-RevId: 838961509
2 parents f92dd68 + 84be741 commit f3b0e51

File tree

4 files changed

+299
-9
lines changed

4 files changed

+299
-9
lines changed

xls/public/c_api_symbols.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ xls_vast_data_type_width
332332
xls_vast_data_type_width_as_int64
333333
xls_vast_def_get_data_type
334334
xls_vast_def_get_name
335+
xls_vast_expression_emit
335336
xls_vast_generate_loop_add_always_comb
336337
xls_vast_generate_loop_add_always_ff
337338
xls_vast_generate_loop_add_blank_line
@@ -354,9 +355,12 @@ xls_vast_logic_ref_get_name
354355
xls_vast_make_verilog_file
355356
xls_vast_parameter_ref_as_expression
356357
xls_vast_slice_as_expression
358+
xls_vast_statement_block_add_blank_line
357359
xls_vast_statement_block_add_blocking_assignment
358360
xls_vast_statement_block_add_case
361+
xls_vast_statement_block_add_comment_text
359362
xls_vast_statement_block_add_conditional
363+
xls_vast_statement_block_add_inline_text
360364
xls_vast_statement_block_add_nonblocking_assignment
361365
xls_vast_verilog_file_add_include
362366
xls_vast_verilog_file_add_module
@@ -365,6 +369,7 @@ xls_vast_verilog_file_free
365369
xls_vast_verilog_file_make_binary
366370
xls_vast_verilog_file_make_bit_vector_type
367371
xls_vast_verilog_file_make_bit_vector_type_with_expression
372+
xls_vast_verilog_file_make_blank_line
368373
xls_vast_verilog_file_make_blocking_assignment
369374
xls_vast_verilog_file_make_comment
370375
xls_vast_verilog_file_make_concat
@@ -384,6 +389,8 @@ xls_vast_verilog_file_make_nonblocking_assignment
384389
xls_vast_verilog_file_make_packed_array_type
385390
xls_vast_verilog_file_make_plain_literal
386391
xls_vast_verilog_file_make_pos_edge
392+
xls_vast_verilog_file_make_replicated_concat
393+
xls_vast_verilog_file_make_replicated_concat_i64
387394
xls_vast_verilog_file_make_scalar_type
388395
xls_vast_verilog_file_make_slice
389396
xls_vast_verilog_file_make_slice_i64
@@ -405,8 +412,10 @@ xls_vast_verilog_module_add_localparam_with_def
405412
xls_vast_verilog_module_add_logic
406413
xls_vast_verilog_module_add_logic_input
407414
xls_vast_verilog_module_add_logic_output
415+
xls_vast_verilog_module_add_member_blank_line
408416
xls_vast_verilog_module_add_member_comment
409417
xls_vast_verilog_module_add_member_continuous_assignment
418+
xls_vast_verilog_module_add_member_inline_statement
410419
xls_vast_verilog_module_add_member_instantiation
411420
xls_vast_verilog_module_add_output
412421
xls_vast_verilog_module_add_parameter

xls/public/c_api_vast.cc

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ char* xls_vast_verilog_file_emit(const struct xls_vast_verilog_file* f) {
289289
return xls::ToOwnedCString(result);
290290
}
291291

292+
char* xls_vast_expression_emit(struct xls_vast_expression* expr) {
293+
CHECK_NE(expr, nullptr);
294+
auto* cpp_expr = reinterpret_cast<xls::verilog::Expression*>(expr);
295+
std::string result = cpp_expr->Emit(/*line_info=*/nullptr);
296+
return xls::ToOwnedCString(result);
297+
}
298+
292299
struct xls_vast_data_type* xls_vast_verilog_file_make_scalar_type(
293300
struct xls_vast_verilog_file* f) {
294301
auto* cpp_file = reinterpret_cast<xls::verilog::VerilogFile*>(f);
@@ -408,6 +415,20 @@ void xls_vast_verilog_module_add_member_comment(
408415
auto* cpp_comment = reinterpret_cast<xls::verilog::Comment*>(comment);
409416
cpp_module->AddModuleMember(cpp_comment);
410417
}
418+
void xls_vast_verilog_module_add_member_blank_line(
419+
struct xls_vast_verilog_module* m, struct xls_vast_blank_line* blank) {
420+
auto* cpp_module = reinterpret_cast<xls::verilog::Module*>(m);
421+
auto* cpp_blank = reinterpret_cast<xls::verilog::BlankLine*>(blank);
422+
cpp_module->AddModuleMember(cpp_blank);
423+
}
424+
void xls_vast_verilog_module_add_member_inline_statement(
425+
struct xls_vast_verilog_module* m,
426+
struct xls_vast_inline_verilog_statement* stmt) {
427+
auto* cpp_module = reinterpret_cast<xls::verilog::Module*>(m);
428+
auto* cpp_stmt =
429+
reinterpret_cast<xls::verilog::InlineVerilogStatement*>(stmt);
430+
cpp_module->AddModuleMember(cpp_stmt);
431+
}
411432

412433
struct xls_vast_literal* xls_vast_verilog_file_make_plain_literal(
413434
struct xls_vast_verilog_file* f, int32_t value) {
@@ -478,13 +499,20 @@ struct xls_vast_comment* xls_vast_verilog_file_make_comment(
478499
cpp_file->Make<xls::verilog::Comment>(xls::SourceInfo(), text);
479500
return reinterpret_cast<xls_vast_comment*>(cpp_comment);
480501
}
481-
502+
struct xls_vast_blank_line* xls_vast_verilog_file_make_blank_line(
503+
struct xls_vast_verilog_file* f) {
504+
auto* cpp_file = reinterpret_cast<xls::verilog::VerilogFile*>(f);
505+
xls::verilog::BlankLine* cpp_blank =
506+
cpp_file->Make<xls::verilog::BlankLine>(xls::SourceInfo());
507+
return reinterpret_cast<xls_vast_blank_line*>(cpp_blank);
508+
}
482509
struct xls_vast_inline_verilog_statement*
483510
xls_vast_verilog_file_make_inline_verilog_statement(
484511
struct xls_vast_verilog_file* f, const char* text) {
485512
auto* cpp_file = reinterpret_cast<xls::verilog::VerilogFile*>(f);
486-
auto* cpp_stmt = cpp_file->Make<xls::verilog::InlineVerilogStatement>(
487-
xls::SourceInfo(), text);
513+
xls::verilog::InlineVerilogStatement* cpp_stmt =
514+
cpp_file->Make<xls::verilog::InlineVerilogStatement>(xls::SourceInfo(),
515+
text);
488516
return reinterpret_cast<xls_vast_inline_verilog_statement*>(cpp_stmt);
489517
}
490518

@@ -843,6 +871,39 @@ struct xls_vast_concat* xls_vast_verilog_file_make_concat(
843871
return reinterpret_cast<xls_vast_concat*>(cpp_concat);
844872
}
845873

874+
struct xls_vast_concat* xls_vast_verilog_file_make_replicated_concat(
875+
struct xls_vast_verilog_file* f, struct xls_vast_expression* replication,
876+
struct xls_vast_expression** elements, size_t element_count) {
877+
auto* cpp_file = reinterpret_cast<xls::verilog::VerilogFile*>(f);
878+
auto* cpp_rep = reinterpret_cast<xls::verilog::Expression*>(replication);
879+
std::vector<xls::verilog::Expression*> cpp_elements;
880+
cpp_elements.reserve(element_count);
881+
for (size_t i = 0; i < element_count; ++i) {
882+
cpp_elements.push_back(
883+
reinterpret_cast<xls::verilog::Expression*>(elements[i]));
884+
}
885+
xls::verilog::Concat* cpp_concat = cpp_file->Make<xls::verilog::Concat>(
886+
xls::SourceInfo(), cpp_rep, absl::MakeConstSpan(cpp_elements));
887+
return reinterpret_cast<xls_vast_concat*>(cpp_concat);
888+
}
889+
890+
struct xls_vast_concat* xls_vast_verilog_file_make_replicated_concat_i64(
891+
struct xls_vast_verilog_file* f, int64_t replication_count,
892+
struct xls_vast_expression** elements, size_t element_count) {
893+
auto* cpp_file = reinterpret_cast<xls::verilog::VerilogFile*>(f);
894+
xls::verilog::Expression* cpp_rep =
895+
cpp_file->PlainLiteral(replication_count, xls::SourceInfo());
896+
std::vector<xls::verilog::Expression*> cpp_elements;
897+
cpp_elements.reserve(element_count);
898+
for (size_t i = 0; i < element_count; ++i) {
899+
cpp_elements.push_back(
900+
reinterpret_cast<xls::verilog::Expression*>(elements[i]));
901+
}
902+
xls::verilog::Concat* cpp_concat = cpp_file->Make<xls::verilog::Concat>(
903+
xls::SourceInfo(), cpp_rep, absl::MakeConstSpan(cpp_elements));
904+
return reinterpret_cast<xls_vast_concat*>(cpp_concat);
905+
}
906+
846907
struct xls_vast_index* xls_vast_verilog_file_make_index_i64(
847908
struct xls_vast_verilog_file* f,
848909
struct xls_vast_indexable_expression* subject, int64_t index) {
@@ -1072,6 +1133,28 @@ struct xls_vast_statement* xls_vast_statement_block_add_nonblocking_assignment(
10721133
cpp_lhs, cpp_rhs);
10731134
return reinterpret_cast<xls_vast_statement*>(cpp_assignment);
10741135
}
1136+
struct xls_vast_statement* xls_vast_statement_block_add_comment_text(
1137+
struct xls_vast_statement_block* block, const char* text) {
1138+
auto* cpp_block = reinterpret_cast<xls::verilog::StatementBlock*>(block);
1139+
xls::verilog::Comment* cpp_comment =
1140+
cpp_block->Add<xls::verilog::Comment>(xls::SourceInfo(), text);
1141+
return reinterpret_cast<xls_vast_statement*>(cpp_comment);
1142+
}
1143+
struct xls_vast_statement* xls_vast_statement_block_add_blank_line(
1144+
struct xls_vast_statement_block* block) {
1145+
auto* cpp_block = reinterpret_cast<xls::verilog::StatementBlock*>(block);
1146+
xls::verilog::BlankLine* cpp_blank =
1147+
cpp_block->Add<xls::verilog::BlankLine>(xls::SourceInfo());
1148+
return reinterpret_cast<xls_vast_statement*>(cpp_blank);
1149+
}
1150+
struct xls_vast_statement* xls_vast_statement_block_add_inline_text(
1151+
struct xls_vast_statement_block* block, const char* text) {
1152+
auto* cpp_block = reinterpret_cast<xls::verilog::StatementBlock*>(block);
1153+
xls::verilog::InlineVerilogStatement* cpp_stmt =
1154+
cpp_block->Add<xls::verilog::InlineVerilogStatement>(xls::SourceInfo(),
1155+
text);
1156+
return reinterpret_cast<xls_vast_statement*>(cpp_stmt);
1157+
}
10751158

10761159
struct xls_vast_statement* xls_vast_statement_block_add_blocking_assignment(
10771160
struct xls_vast_statement_block* block, struct xls_vast_expression* lhs,

xls/public/c_api_vast.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ struct xls_vast_parameter_ref;
5757
struct xls_vast_conditional;
5858
struct xls_vast_case_statement;
5959
struct xls_vast_localparam_ref;
60+
struct xls_vast_blank_line;
61+
struct xls_vast_inline_verilog_statement;
62+
6063
// Note: We define the enum with a fixed width integer type for clarity of the
6164
// exposed ABI.
6265
typedef int32_t xls_vast_file_type;
@@ -154,6 +157,11 @@ void xls_vast_verilog_module_add_member_continuous_assignment(
154157
struct xls_vast_continuous_assignment* member);
155158
void xls_vast_verilog_module_add_member_comment(
156159
struct xls_vast_verilog_module* m, struct xls_vast_comment* comment);
160+
void xls_vast_verilog_module_add_member_blank_line(
161+
struct xls_vast_verilog_module* m, struct xls_vast_blank_line* blank);
162+
void xls_vast_verilog_module_add_member_inline_statement(
163+
struct xls_vast_verilog_module* m,
164+
struct xls_vast_inline_verilog_statement* stmt);
157165

158166
struct xls_vast_logic_ref* xls_vast_verilog_module_add_input(
159167
struct xls_vast_verilog_module* m, const char* name,
@@ -197,7 +205,6 @@ struct xls_vast_localparam_ref* xls_vast_verilog_module_add_localparam(
197205

198206
// Note: returned value is owned by the caller, free via `xls_c_str_free`.
199207
char* xls_vast_verilog_module_get_name(struct xls_vast_verilog_module* m);
200-
201208
// Returns the ports that are present on the given module.
202209
//
203210
// Note: the returned array is owned by the caller, to be freed by
@@ -277,6 +284,9 @@ xls_vast_verilog_file_make_continuous_assignment(
277284
struct xls_vast_comment* xls_vast_verilog_file_make_comment(
278285
struct xls_vast_verilog_file* f, const char* text);
279286

287+
struct xls_vast_blank_line* xls_vast_verilog_file_make_blank_line(
288+
struct xls_vast_verilog_file* f);
289+
280290
struct xls_vast_inline_verilog_statement*
281291
xls_vast_verilog_file_make_inline_verilog_statement(
282292
struct xls_vast_verilog_file* f, const char* text);
@@ -296,6 +306,17 @@ struct xls_vast_concat* xls_vast_verilog_file_make_concat(
296306
struct xls_vast_verilog_file* f, struct xls_vast_expression** elements,
297307
size_t element_count);
298308

309+
// Creates a replicated concatenation expression: {replication{elements...}}.
310+
// For single-element replication, pass element_count=1.
311+
struct xls_vast_concat* xls_vast_verilog_file_make_replicated_concat(
312+
struct xls_vast_verilog_file* f, struct xls_vast_expression* replication,
313+
struct xls_vast_expression** elements, size_t element_count);
314+
315+
// Convenience: replicated concatenation with an integer replication count.
316+
struct xls_vast_concat* xls_vast_verilog_file_make_replicated_concat_i64(
317+
struct xls_vast_verilog_file* f, int64_t replication_count,
318+
struct xls_vast_expression** elements, size_t element_count);
319+
299320
struct xls_vast_slice* xls_vast_verilog_file_make_slice_i64(
300321
struct xls_vast_verilog_file* f,
301322
struct xls_vast_indexable_expression* subject, int64_t hi, int64_t lo);
@@ -456,17 +477,24 @@ struct xls_vast_statement* xls_vast_statement_block_add_blocking_assignment(
456477
struct xls_vast_statement_block* block, struct xls_vast_expression* lhs,
457478
struct xls_vast_expression* rhs);
458479

459-
// Adds a blocking assignment statement (lhs = rhs) to a statement block and
460-
// returns a pointer to the created statement.
461-
struct xls_vast_statement* xls_vast_statement_block_add_blocking_assignment(
462-
struct xls_vast_statement_block* block, struct xls_vast_expression* lhs,
463-
struct xls_vast_expression* rhs);
480+
struct xls_vast_statement* xls_vast_statement_block_add_comment_text(
481+
struct xls_vast_statement_block* block, const char* text);
482+
483+
struct xls_vast_statement* xls_vast_statement_block_add_blank_line(
484+
struct xls_vast_statement_block* block);
485+
486+
struct xls_vast_statement* xls_vast_statement_block_add_inline_text(
487+
struct xls_vast_statement_block* block, const char* text);
464488

465489
// Emits/formats the contents of the given verilog file to a string.
466490
//
467491
// Note: caller owns the returned string, to be freed by `xls_c_str_free`.
468492
char* xls_vast_verilog_file_emit(const struct xls_vast_verilog_file* f);
469493

494+
// Emits an expression to a string and returns an owned C string; caller must
495+
// free with xls_c_str_free.
496+
char* xls_vast_expression_emit(struct xls_vast_expression* expr);
497+
470498
// Adds an always_ff block to the module.
471499
// 'sensitivity_list_elements' is an array of expressions, typically created
472500
// using 'xls_vast_verilog_file_make_pos_edge' or

0 commit comments

Comments
 (0)