11# Bytecode file writer - handles sections and overall structure
22
33# Bytecode version
4- const BYTECODE_VERSION = ( 13 , 1 , 0 )
4+ const DEFAULT_BYTECODE_VERSION = v " 13.1 "
55
66# Magic number
77const MAGIC = UInt8[0x7f , 0x54 , 0x69 , 0x6c , 0x65 , 0x49 , 0x52 , 0x00 ] # "\x7fTileIR\x00"
@@ -97,9 +97,11 @@ mutable struct CodeBuilder
9797 next_value_id:: Int
9898 cur_debug_attr:: DebugAttrId
9999 num_ops:: Int
100+ version:: VersionNumber
100101end
101102
102- function CodeBuilder (string_table:: StringTable , constant_table:: ConstantTable , type_table:: TypeTable )
103+ function CodeBuilder (string_table:: StringTable , constant_table:: ConstantTable , type_table:: TypeTable ;
104+ version:: VersionNumber = DEFAULT_BYTECODE_VERSION)
103105 CodeBuilder (
104106 UInt8[],
105107 string_table,
@@ -108,7 +110,8 @@ function CodeBuilder(string_table::StringTable, constant_table::ConstantTable, t
108110 DebugAttrId[],
109111 0 ,
110112 DebugAttrId (0 ), # No debug info
111- 0
113+ 0 ,
114+ version
112115 )
113116end
114117
@@ -374,9 +377,10 @@ mutable struct BytecodeWriter
374377 debug_attr_table:: DebugAttrTable
375378 debug_info:: Vector{Vector{DebugAttrId}}
376379 num_functions:: Int
380+ version:: VersionNumber
377381end
378382
379- function BytecodeWriter ()
383+ function BytecodeWriter (; version :: VersionNumber = DEFAULT_BYTECODE_VERSION )
380384 string_table = StringTable ()
381385 BytecodeWriter (
382386 UInt8[],
@@ -385,21 +389,21 @@ function BytecodeWriter()
385389 TypeTable (),
386390 DebugAttrTable (string_table),
387391 Vector{Vector{DebugAttrId}}[],
388- 0
392+ 0 ,
393+ version
389394 )
390395end
391396
392397"""
393398Write the bytecode header.
394399"""
395- function write_header! (buf:: Vector{UInt8} )
400+ function write_header! (buf:: Vector{UInt8} , version :: VersionNumber )
396401 append! (buf, MAGIC)
397- major, minor, tag = BYTECODE_VERSION
398- push! (buf, UInt8 (major))
399- push! (buf, UInt8 (minor))
400- # Tag as 2-byte little-endian
401- push! (buf, UInt8 (tag & 0xff ))
402- push! (buf, UInt8 ((tag >> 8 ) & 0xff ))
402+ push! (buf, UInt8 (version. major))
403+ push! (buf, UInt8 (version. minor))
404+ # Patch as 2-byte little-endian
405+ push! (buf, UInt8 (version. patch & 0xff ))
406+ push! (buf, UInt8 ((version. patch >> 8 ) & 0xff ))
403407end
404408
405409"""
486490Write complete bytecode to a buffer.
487491Returns the buffer with all sections.
488492"""
489- function write_bytecode! (f:: Function , num_functions:: Int )
490- writer = BytecodeWriter ()
493+ function write_bytecode! (f:: Function , num_functions:: Int ;
494+ version:: VersionNumber = DEFAULT_BYTECODE_VERSION)
495+ writer = BytecodeWriter (; version)
491496
492497 # Function section content
493498 func_buf = UInt8[]
@@ -502,7 +507,7 @@ function write_bytecode!(f::Function, num_functions::Int)
502507
503508 # Build final output
504509 buf = UInt8[]
505- write_header! (buf)
510+ write_header! (buf, version )
506511
507512 # Sections in order: Func, Global (if any), Constant, Debug, Type, String, End
508513 write_section! (buf, Section. Func, func_buf, 8 )
@@ -574,7 +579,8 @@ function add_function!(writer::BytecodeWriter, func_buf::Vector{UInt8},
574579 end
575580
576581 # Create code builder for function body
577- cb = CodeBuilder (writer. string_table, writer. constant_table, writer. type_table)
582+ cb = CodeBuilder (writer. string_table, writer. constant_table, writer. type_table;
583+ version= writer. version)
578584
579585 return cb
580586end
0 commit comments