cgen: add support for `v -printfn main file.v`

pull/4587/head
Delyan Angelov 2020-04-25 11:15:02 +03:00
parent 900d96c638
commit e0ab318f56
4 changed files with 20 additions and 4 deletions

View File

@ -145,6 +145,10 @@ fn parse_args(args []string) (&pref.Preferences, string) {
'-freestanding' { '-freestanding' {
res.is_bare = true res.is_bare = true
} }
'-prof', '-profile' {
eprintln('TODO: -prof')
res.is_prof = true
}
'-prod' { '-prod' {
res.is_prod = true res.is_prod = true
} }
@ -182,8 +186,12 @@ fn parse_args(args []string) (&pref.Preferences, string) {
} }
res.os = target_os_kind res.os = target_os_kind
} }
'-printfn' {
res.printfn_list << cmdline.option(current_args, '-printfn', '')
i++
}
'-cflags' { '-cflags' {
res.cflags = cmdline.option(current_args, '-cflags', '') res.cflags += ' ' + cmdline.option(current_args, '-cflags', '')
i++ i++
} }
'-define', '-d' { '-define', '-d' {

View File

@ -60,6 +60,7 @@ struct Gen {
mut: mut:
file ast.File file ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string
tmp_count int tmp_count int
variadic_args map[string]int variadic_args map[string]int
is_c_call bool // e.g. `C.printf("v")` is_c_call bool // e.g. `C.printf("v")`
@ -479,8 +480,12 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} }
} }
ast.FnDecl { ast.FnDecl {
fn_start_pos := g.out.len
g.fn_decl = it // &it g.fn_decl = it // &it
g.gen_fn_decl(it) g.gen_fn_decl(it)
if g.last_fn_c_name in g.pref.printfn_list {
println(g.out.after(fn_start_pos))
}
g.writeln('') g.writeln('')
} }
ast.ForCStmt { ast.ForCStmt {

View File

@ -19,12 +19,15 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
if g.is_gui_app() { if g.is_gui_app() {
// GUI application // GUI application
g.writeln('int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, LPWSTR cmd_line, int show_cmd') g.writeln('int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, LPWSTR cmd_line, int show_cmd')
g.last_fn_c_name = 'wWinMain'
} else { } else {
// Console application // Console application
g.writeln('int wmain(int ___argc, wchar_t* ___argv[], wchar_t* ___envp[]') g.writeln('int wmain(int ___argc, wchar_t* ___argv[], wchar_t* ___envp[]')
g.last_fn_c_name = 'wmain'
} }
} else { } else {
g.write('int ${it.name}(int ___argc, char** ___argv') g.write('int ${it.name}(int ___argc, char** ___argv')
g.last_fn_c_name = it.name
} }
} else { } else {
mut name := it.name mut name := it.name
@ -46,6 +49,7 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
// type_name := g.table.Type_to_str(it.return_type) // type_name := g.table.Type_to_str(it.return_type)
type_name := g.typ(it.return_type) type_name := g.typ(it.return_type)
g.write('$type_name ${name}(') g.write('$type_name ${name}(')
g.last_fn_c_name = name
g.definitions.write('$type_name ${name}(') g.definitions.write('$type_name ${name}(')
} }
// Receiver is the first argument // Receiver is the first argument

View File

@ -77,8 +77,8 @@ pub mut:
compile_defines_all []string // contains both: ['vfmt','another'] compile_defines_all []string // contains both: ['vfmt','another']
mod string mod string
run_args []string // `v run x.v 1 2 3` => `1 2 3` run_args []string // `v run x.v 1 2 3` => `1 2 3`
printfn_list []string // a list of generated function names, whose source should be shown, for debugging
} }
pub fn backend_from_string(s string) ?Backend { pub fn backend_from_string(s string) ?Backend {
@ -97,4 +97,3 @@ pub fn backend_from_string(s string) ?Backend {
} }
} }
} }