2019-06-23 04:21:30 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
module main
|
|
|
|
|
2019-07-12 07:37:54 +02:00
|
|
|
import os
|
2019-08-28 16:35:44 +02:00
|
|
|
import strings
|
|
|
|
import time
|
2019-07-12 07:37:54 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
struct CGen {
|
|
|
|
out os.File
|
|
|
|
out_path string
|
2019-08-30 19:19:06 +02:00
|
|
|
//types []string
|
|
|
|
thread_fns []string
|
|
|
|
//buf strings.Builder
|
|
|
|
is_user bool
|
|
|
|
mut:
|
|
|
|
lines []string
|
2019-06-22 20:20:28 +02:00
|
|
|
typedefs []string
|
|
|
|
type_aliases []string
|
|
|
|
includes []string
|
|
|
|
thread_args []string
|
|
|
|
consts []string
|
|
|
|
fns []string
|
|
|
|
so_fns []string
|
|
|
|
consts_init []string
|
2019-07-29 18:21:36 +02:00
|
|
|
pass Pass
|
2019-07-26 16:45:16 +02:00
|
|
|
nogen bool
|
|
|
|
tmp_line string
|
|
|
|
cur_line string
|
|
|
|
prev_line string
|
|
|
|
is_tmp bool
|
|
|
|
fn_main string
|
|
|
|
stash string
|
|
|
|
file string
|
|
|
|
line int
|
|
|
|
line_directives bool
|
2019-08-28 16:35:44 +02:00
|
|
|
cut_pos int
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 21:51:16 +02:00
|
|
|
fn new_cgen(out_name_c string) &CGen {
|
2019-08-23 10:42:48 +02:00
|
|
|
path := out_name_c
|
2019-07-03 21:07:42 +02:00
|
|
|
out := os.create(path) or {
|
2019-08-28 16:35:44 +02:00
|
|
|
println('failed to create $path')
|
|
|
|
return &CGen{}
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
gen := &CGen {
|
2019-08-28 16:35:44 +02:00
|
|
|
out_path: path
|
|
|
|
out: out
|
|
|
|
//buf: strings.new_builder(10000)
|
2019-07-26 16:45:16 +02:00
|
|
|
lines: _make(0, 1000, sizeof(string))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return gen
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) genln(s string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if g.nogen || g.pass != .main {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if g.is_tmp {
|
|
|
|
g.tmp_line = '$g.tmp_line $s\n'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.cur_line = '$g.cur_line $s'
|
|
|
|
if g.cur_line != '' {
|
2019-07-26 16:45:16 +02:00
|
|
|
if g.line_directives && g.cur_line.trim_space() != '' {
|
|
|
|
g.lines << '#line $g.line "$g.file"'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
g.lines << g.cur_line
|
|
|
|
g.prev_line = g.cur_line
|
|
|
|
g.cur_line = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) gen(s string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if g.nogen || g.pass != .main {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if g.is_tmp {
|
|
|
|
g.tmp_line = '$g.tmp_line $s'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.cur_line = '$g.cur_line $s'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 20:21:23 +02:00
|
|
|
fn (g mut CGen) resetln(s string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if g.nogen || g.pass != .main {
|
2019-07-18 20:21:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if g.is_tmp {
|
|
|
|
g.tmp_line = s
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.cur_line = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (g mut CGen) save() {
|
|
|
|
s := g.lines.join('\n')
|
2019-06-30 16:11:55 +02:00
|
|
|
g.out.writeln(s)
|
2019-06-22 20:20:28 +02:00
|
|
|
g.out.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) start_tmp() {
|
|
|
|
if g.is_tmp {
|
|
|
|
println(g.tmp_line)
|
2019-06-23 10:12:09 +02:00
|
|
|
println('start_tmp() already started. cur_line="$g.cur_line"')
|
|
|
|
exit(1)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// kg.tmp_lines_pos++
|
|
|
|
g.tmp_line = ''
|
|
|
|
g.is_tmp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) end_tmp() string {
|
|
|
|
g.is_tmp = false
|
|
|
|
res := g.tmp_line
|
|
|
|
g.tmp_line = ''
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) add_placeholder() int {
|
|
|
|
if g.is_tmp {
|
|
|
|
return g.tmp_line.len
|
|
|
|
}
|
|
|
|
return g.cur_line.len
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:50:19 +02:00
|
|
|
fn (g mut CGen) start_cut() {
|
2019-08-28 16:35:44 +02:00
|
|
|
g.cut_pos = g.add_placeholder()
|
|
|
|
}
|
2019-08-13 13:50:19 +02:00
|
|
|
|
|
|
|
fn (g mut CGen) cut() string {
|
2019-08-28 16:35:44 +02:00
|
|
|
pos := g.cut_pos
|
|
|
|
g.cut_pos = 0
|
2019-08-13 13:50:19 +02:00
|
|
|
if g.is_tmp {
|
2019-08-28 16:35:44 +02:00
|
|
|
res := g.tmp_line.right(pos)
|
|
|
|
g.tmp_line = g.tmp_line.left(pos)
|
|
|
|
return res
|
2019-08-13 13:50:19 +02:00
|
|
|
}
|
2019-08-28 16:35:44 +02:00
|
|
|
res := g.cur_line.right(pos)
|
|
|
|
g.cur_line = g.cur_line.left(pos)
|
|
|
|
return res
|
|
|
|
}
|
2019-08-13 13:50:19 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (g mut CGen) set_placeholder(pos int, val string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if g.nogen || g.pass != .main {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// g.lines.set(pos, val)
|
|
|
|
if g.is_tmp {
|
|
|
|
left := g.tmp_line.left(pos)
|
|
|
|
right := g.tmp_line.right(pos)
|
|
|
|
g.tmp_line = '${left}${val}${right}'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
left := g.cur_line.left(pos)
|
|
|
|
right := g.cur_line.right(pos)
|
|
|
|
g.cur_line = '${left}${val}${right}'
|
|
|
|
// g.genln('')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) insert_before(val string) {
|
2019-08-28 16:35:44 +02:00
|
|
|
prev := g.lines[g.lines.len - 1]
|
|
|
|
g.lines[g.lines.len - 1] = '$prev \n $val \n'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text string) {
|
|
|
|
for arg in g.thread_args {
|
|
|
|
if arg.contains(wrapper_name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.thread_args << struct_text
|
|
|
|
g.thread_args << wrapper_text
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c mut V) prof_counters() string {
|
|
|
|
mut res := []string
|
|
|
|
// Global fns
|
2019-06-30 22:44:15 +02:00
|
|
|
//for f in c.table.fns {
|
|
|
|
//res << 'double ${c.table.cgen_name(f)}_time;'
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Methods
|
2019-09-01 13:31:43 +02:00
|
|
|
/*
|
2019-06-22 20:20:28 +02:00
|
|
|
for typ in c.table.types {
|
|
|
|
// println('')
|
|
|
|
for f in typ.methods {
|
|
|
|
// res << f.cgen_name()
|
|
|
|
res << 'double ${c.table.cgen_name(f)}_time;'
|
|
|
|
// println(f.cgen_name())
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 13:31:43 +02:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
return res.join(';\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) print_prof_counters() string {
|
|
|
|
mut res := []string
|
|
|
|
// Global fns
|
2019-06-30 22:44:15 +02:00
|
|
|
//for f in p.table.fns {
|
|
|
|
//counter := '${p.table.cgen_name(f)}_time'
|
|
|
|
//res << 'if ($counter) printf("%%f : $f.name \\n", $counter);'
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Methods
|
2019-09-01 13:31:43 +02:00
|
|
|
/*
|
2019-06-22 20:20:28 +02:00
|
|
|
for typ in p.table.types {
|
|
|
|
// println('')
|
|
|
|
for f in typ.methods {
|
|
|
|
counter := '${p.table.cgen_name(f)}_time'
|
|
|
|
res << 'if ($counter) printf("%%f : ${p.table.cgen_name(f)} \\n", $counter);'
|
|
|
|
// res << 'if ($counter) printf("$f.name : %%f\\n", $counter);'
|
|
|
|
// res << f.cgen_name()
|
|
|
|
// res << 'double ${f.cgen_name()}_time;'
|
|
|
|
// println(f.cgen_name())
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 13:31:43 +02:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
return res.join(';\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_typedef(s string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if !p.first_pass() {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.cgen.typedefs << s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_type_alias(s string) {
|
2019-07-29 18:21:36 +02:00
|
|
|
if !p.first_pass() {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.cgen.type_aliases << s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut CGen) add_to_main(s string) {
|
|
|
|
g.fn_main = g.fn_main + s
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:27:35 +02:00
|
|
|
|
2019-08-28 16:35:44 +02:00
|
|
|
fn build_thirdparty_obj_file(flag string) {
|
|
|
|
obj_path := flag.all_after(' ')
|
2019-07-10 13:27:35 +02:00
|
|
|
if os.file_exists(obj_path) {
|
2019-08-28 16:35:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println('$obj_path not found, building it...')
|
2019-09-03 15:09:43 +02:00
|
|
|
parent := os.dir( obj_path )
|
2019-08-28 16:35:44 +02:00
|
|
|
files := os.ls(parent)
|
|
|
|
mut cfiles := ''
|
2019-07-10 13:27:35 +02:00
|
|
|
for file in files {
|
2019-08-28 16:35:44 +02:00
|
|
|
if file.ends_with('.c') {
|
2019-09-03 15:09:43 +02:00
|
|
|
cfiles += '"' + os.realpath( parent + os.PathSeparator + file ) + '" '
|
2019-08-28 16:35:44 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 10:42:48 +02:00
|
|
|
cc := find_c_compiler()
|
|
|
|
cc_thirdparty_options := find_c_compiler_thirdparty_options()
|
2019-09-03 15:09:43 +02:00
|
|
|
cmd := '$cc $cc_thirdparty_options -c -o "$obj_path" $cfiles'
|
|
|
|
res := os.exec(cmd) or {
|
|
|
|
println('failed thirdparty object build cmd: $cmd')
|
2019-08-29 02:30:17 +02:00
|
|
|
cerror(err)
|
|
|
|
return
|
2019-08-08 07:30:05 +02:00
|
|
|
}
|
2019-08-28 16:35:44 +02:00
|
|
|
println(res.output)
|
|
|
|
}
|
2019-07-10 13:27:35 +02:00
|
|
|
|
2019-08-28 16:35:44 +02:00
|
|
|
fn os_name_to_ifdef(name string) string {
|
2019-08-13 13:50:19 +02:00
|
|
|
switch name {
|
|
|
|
case 'windows': return '_WIN32'
|
|
|
|
case 'mac': return '__APPLE__'
|
2019-08-28 16:35:44 +02:00
|
|
|
case 'linux': return '__linux__'
|
|
|
|
case 'freebsd': return '__FreeBSD__'
|
|
|
|
case 'openbsd': return '__OpenBSD__'
|
|
|
|
case 'netbsd': return '__NetBSD__'
|
|
|
|
case 'dragonfly': return '__DragonFly__'
|
|
|
|
case 'msvc': return '_MSC_VER'
|
|
|
|
}
|
2019-08-29 02:30:17 +02:00
|
|
|
cerror('bad os ifdef name "$name"')
|
|
|
|
return ''
|
2019-08-28 16:35:44 +02:00
|
|
|
}
|
2019-08-13 13:50:19 +02:00
|
|
|
|
|
|
|
fn platform_postfix_to_ifdefguard(name string) string {
|
|
|
|
switch name {
|
|
|
|
case '.v': return '' // no guard needed
|
|
|
|
case '_win.v': return '#ifdef _WIN32'
|
|
|
|
case '_nix.v': return '#ifndef _WIN32'
|
|
|
|
case '_lin.v': return '#ifdef __linux__'
|
|
|
|
case '_mac.v': return '#ifdef __APPLE__'
|
|
|
|
}
|
2019-08-29 02:30:17 +02:00
|
|
|
cerror('bad platform_postfix "$name"')
|
|
|
|
return ''
|
2019-08-13 13:50:19 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 16:35:44 +02:00
|
|
|
// C struct definitions, ordered
|
2019-08-29 00:52:32 +02:00
|
|
|
// Sort the types, make sure types that are referenced by other types
|
|
|
|
// are added before them.
|
2019-08-28 16:35:44 +02:00
|
|
|
fn (v mut V) c_type_definitions() string {
|
2019-09-04 18:49:53 +02:00
|
|
|
mut types := []Type // structs that need to be sorted
|
|
|
|
mut builtin_types := []Type // builtin types
|
2019-09-01 13:31:43 +02:00
|
|
|
// builtin types need to be on top
|
|
|
|
builtins := ['string', 'array', 'map', 'Option']
|
|
|
|
for builtin in builtins {
|
|
|
|
typ := v.table.typesmap[builtin]
|
|
|
|
builtin_types << typ
|
|
|
|
}
|
2019-09-04 18:49:53 +02:00
|
|
|
// everything except builtin will get sorted
|
|
|
|
for t_name, t in v.table.typesmap {
|
|
|
|
if t_name in builtins {
|
2019-08-29 00:52:32 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
types << t
|
2019-08-28 16:35:44 +02:00
|
|
|
}
|
2019-09-04 18:49:53 +02:00
|
|
|
// sort structs
|
|
|
|
types_sorted := sort_structs(types)
|
2019-08-28 16:35:44 +02:00
|
|
|
// Generate C code
|
2019-09-01 13:31:43 +02:00
|
|
|
return types_to_c(builtin_types,v.table) + '\n//----\n' +
|
2019-09-04 18:49:53 +02:00
|
|
|
types_to_c(types_sorted, v.table)
|
2019-08-29 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn types_to_c(types []Type, table &Table) string {
|
2019-08-28 16:35:44 +02:00
|
|
|
mut sb := strings.new_builder(10)
|
2019-08-29 00:52:32 +02:00
|
|
|
for t in types {
|
2019-08-28 16:35:44 +02:00
|
|
|
if t.cat != .union_ && t.cat != .struct_ {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//if is_objc {
|
|
|
|
//sb.writeln('@interface $name : $objc_parent { @public')
|
|
|
|
//}
|
|
|
|
//if is_atomic {
|
|
|
|
//sb.write('_Atomic ')
|
|
|
|
//}
|
|
|
|
kind := if t.cat == .union_ {'union'} else {'struct'}
|
|
|
|
sb.writeln('$kind $t.name {')
|
|
|
|
for field in t.fields {
|
2019-08-29 00:52:32 +02:00
|
|
|
sb.writeln(table.cgen_name_type_pair(field.name,
|
2019-08-28 16:35:44 +02:00
|
|
|
field.typ) + ';')
|
|
|
|
}
|
|
|
|
sb.writeln('};\n')
|
|
|
|
//if is_objc {
|
2019-08-29 00:52:32 +02:00
|
|
|
//sb.writeln('@end')
|
2019-08-28 16:35:44 +02:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:11:21 +02:00
|
|
|
// sort structs by dependant fields
|
2019-09-04 09:35:08 +02:00
|
|
|
fn sort_structs(types []Type) []Type {
|
|
|
|
mut dep_graph := new_dep_graph()
|
|
|
|
// types name list
|
2019-09-03 18:11:21 +02:00
|
|
|
mut type_names := []string
|
2019-09-04 09:35:08 +02:00
|
|
|
for t in types {
|
|
|
|
type_names << t.name
|
2019-09-03 18:11:21 +02:00
|
|
|
}
|
|
|
|
// loop over types
|
2019-09-04 09:35:08 +02:00
|
|
|
for t in types {
|
2019-09-03 18:11:21 +02:00
|
|
|
// create list of deps
|
2019-09-04 09:35:08 +02:00
|
|
|
mut field_deps := []string
|
2019-09-03 18:11:21 +02:00
|
|
|
for field in t.fields {
|
2019-09-04 09:35:08 +02:00
|
|
|
// skip if not in types list or already in deps
|
|
|
|
if !(field.typ in type_names) || field.typ in field_deps {
|
2019-09-03 18:11:21 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-09-04 09:35:08 +02:00
|
|
|
field_deps << field.typ
|
2019-09-03 18:11:21 +02:00
|
|
|
}
|
|
|
|
// add type and dependant types to graph
|
2019-09-04 09:35:08 +02:00
|
|
|
dep_graph.add(t.name, field_deps)
|
|
|
|
}
|
|
|
|
// sort graph
|
|
|
|
dep_graph_sorted := dep_graph.resolve()
|
|
|
|
if !dep_graph_sorted.acyclic {
|
2019-09-04 18:49:53 +02:00
|
|
|
cerror('error: cgen.sort_structs() DGNAC.\nplease create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
2019-09-04 09:35:08 +02:00
|
|
|
}
|
|
|
|
// sort types
|
|
|
|
mut types_sorted := []Type
|
|
|
|
for node in dep_graph_sorted.nodes {
|
|
|
|
for t in types {
|
2019-09-03 18:11:21 +02:00
|
|
|
if t.name == node.name {
|
2019-09-04 09:35:08 +02:00
|
|
|
types_sorted << t
|
2019-08-29 00:52:32 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 09:35:08 +02:00
|
|
|
return types_sorted
|
2019-08-29 00:52:32 +02:00
|
|
|
}
|
|
|
|
|