parrotcode: Untitled | |
Contents | Compilers |
This file implements an API for generating bytecode.
All gory details are hidden.
In order to improve modularity,
all bytecode generating functions take a bytecode
struct parameter,
which keeps track of the state; this includes a Parrot interpreter.
Since this state is private, the bytecode struct is declared in this C file, not in the header file. It is, however, declared in the header file as a struct, so you can use it as a type, but not touch its private bits. Everything you need to know should be accessible through accessor functions.
static int new_const(bytecode *const bc)
int add_pmc_const(bytecode *const bc, PMC *pmc)
pmc
to the constant table.
This function returns the index in the constant table where pmc
is stored.int add_string_const(bytecode *const bc, char const *const str)
str
to the constant table.
This function returns the index in the constant table where str
is stored.f
is stored is returned.int add_key_const(bytecode *const bc, PMC *key)
bytecode *new_bytecode(Interp *interp, char const *const filename, int bytes, int codesize)
filename
void emit_opcode(bytecode *const bc, opcode_t op)
op
into the bytecode stream.void emit_int_arg(bytecode *const bc, int intval)
void emit_op_by_name(bytecode *const bc, char const *const opname)
opname
must be a valid,
signatured opname.
So,
print
is not valid,
whereas print_ic
is.
The opcode of the opname is looked up and written into the bytecode stream.
If opname
is not valid,
an error message is written.void add_sub_pmc(bytecode *const bc, char const *const subname, -- the name of this sub char const *const nsentry, -- the value of the :nsentry flag char const *const subid, -- the value of the :subid flag int vtable_index, -- vtable index, or -1 if no :vtable unsigned regs_used[], -- register usage of this sub int startoffset, -- start offset of this sub in bytecode int endoffset)
-- end offset of this sub in bytecodevoid write_pbc_file(bytecode *const bc, char const *const filename)
filename
. /* pack the packfile */
size = PackFile_pack_size(bc->interp, bc->interp->code->base.pf) * sizeof (opcode_t);
packed = (opcode_t*) mem_sys_allocate(size);
PackFile_pack(bc->interp, bc->interp->code->base.pf, packed);
/* write to file */
fp = fopen(filename, "wb");
if (fp == NULL)
fprintf(stderr, "Couldn't open %s\n", filename);
result = fwrite(packed, size, 1, fp);
if (result != 1)
fprintf(stderr, "Couldn't write %s\n", filename);
fclose(fp);
/* done! */
}
|
![]() |