#how to show the code after comptime?

1 messages · Page 1 of 1 (latest)

raven lion
#

sample code:
fn number(n: u32) u32{
if(n > 10) return 0;
return n;
}
fn main() {
const x = number(11);
const y = number(9);
}
after comptime the code is following?(I guess the fn number don't use anywhere, maybe remove it)
fn main() {
const x = 0;
const y = 9;
}

any way can show the code after comptime?

mint flicker
#

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.

raven lion
#

y, it is not macros.
but it is useful for learning zig language

mint flicker
#

i dont really see how

#

you can use @compileLog to print comptime known values if that helps

raven lion
#

o, i will try it

#

if the fn number don't use anywhere after comptime, will zig remove it?

mint flicker
#

it can be optimized away sure, non-exported symbols in zig are considered local

raven lion
#

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.

astral stream
#

therefor you cannot "output" the resulting code

#

since there is none

#

your comptime function is interpreted at compile time

#

its not a macro

raven lion
#

i got, its not a macro.
i mean: what exact code for "inline or generic" after build(not comptime)

astral stream
#

do you mean the resulting assembly? you can just disassemble the binary

raven lion
#

y, assemble is a way, its more difficult way

astral stream
#

more difficult than what?

#

your comptime/generic functions are compiled into an intermediate assembly and interpreted

#

there's no code to view

swift mica
#

-femit-llvm-ir is another option.

raven lion
#

its very difficult way to understand/learn zig lang. no "more" word (my en is not good)

swift mica
#

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.

mint flicker
#

or just learn zig, and read the source code...
have you looked at the language reference?

swift mica
#

what you're looking for just isn't present AFAIK. There's no "after generic" code to view because generic is just more comptime.

raven lion
astral stream
#

look at godbolt

#

it shows the resulting assembly

#

there is no "code after build", only the varying levels of assembly/IRs

raven lion
#

got, thanks

swift mica
#
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
raven lion
#

i will try. this is already very similar to "code after build" for learning.

#

@mint flicker y, I have quickly overview the zig reference