#how to show the code after comptime?
1 messages · Page 1 of 1 (latest)
there's no way to do this, this isn't how comptime works.
unlike macros, comptime isn't a preprocess, but tied into the analysis of zig itself.
y, it is not macros.
but it is useful for learning zig language
i dont really see how
you can use @compileLog to print comptime known values if that helps
o, i will try it
if the fn number don't use anywhere after comptime, will zig remove it?
it can be optimized away sure, non-exported symbols in zig are considered local
the @compileLog is good at learning zig.
but if we want to know that what exact code for "inline or generic". it don't work
thanks your answer.
comptime does not generate code
therefor you cannot "output" the resulting code
since there is none
your comptime function is interpreted at compile time
its not a macro
i got, its not a macro.
i mean: what exact code for "inline or generic" after build(not comptime)
do you mean the resulting assembly? you can just disassemble the binary
easiest way to see the resulting assembly is godbolt https://godbolt.org/
y, assemble is a way, its more difficult way
more difficult than what?
your comptime/generic functions are compiled into an intermediate assembly and interpreted
there's no code to view
-femit-llvm-ir is another option.
its very difficult way to understand/learn zig lang. no "more" word (my en is not good)
yes. You can get an idea of what's happening but it's certainly not an easy solution. On second thought I'd prefer objdump/godbolt to the IR in most cases.
or just learn zig, and read the source code...
have you looked at the language reference?
what you're looking for just isn't present AFAIK. There's no "after generic" code to view because generic is just more comptime.
got.
in blog : https://kristoff.it/blog/what-is-zig-comptime/
I see the follow :
var acc: i64 = 0;
acc +%= num;
acc *%= num;
acc -%= num;
acc -%= num;
return acc;
then I want to know , can show the code after build.
look at godbolt
it shows the resulting assembly
there is no "code after build", only the varying levels of assembly/IRs
got, thanks
export fn thing(num: i64) i64 {
var acc: i64 = 0;
acc +%= num;
acc *%= num;
acc -%= num;
acc -%= num;
return acc;
}
you can do it locally:
$ zig build-obj z98.zig -OReleaseFast
$ objdump -S z98.o
z98.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <thing>:
export fn thing(num: i64) i64 {
var acc: i64 = 0;
acc +%= num;
acc *%= num;
acc -%= num;
acc -%= num;
0: 48 8d 47 fe lea -0x2(%rdi),%rax
4: 48 0f af c7 imul %rdi,%rax
return acc;
8: c3 ret