├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── asmer ├── README.md ├── asm │ ├── asmer.tu │ ├── define.tu │ └── instructs.tu ├── ast │ ├── common.tu │ ├── sym.tu │ ├── symtables.tu │ └── token.tu ├── bin │ └── amd64_linux_tua ├── cases │ ├── add.i │ ├── add.s │ ├── and.i │ ├── and.s │ ├── call.i │ ├── call.s │ ├── cdq.i │ ├── cdq.s │ ├── cmp.i │ ├── cmp.s │ ├── cmpxchgl.i │ ├── cmpxchgl.s │ ├── cmpxchgq.i │ ├── cmpxchgq.s │ ├── div_idiv.i │ ├── div_idiv.s │ ├── float.i │ ├── float.s │ ├── imul.i │ ├── imul.s │ ├── jbe.i │ ├── jbe.s │ ├── je.i │ ├── je.s │ ├── jg.i │ ├── jg.s │ ├── jle.i │ ├── jle.s │ ├── jmp.i │ ├── jmp.s │ ├── jne.i │ ├── jne.s │ ├── lea.i │ ├── lea.s │ ├── mov.i │ ├── mov.s │ ├── mov_bwlq.i │ ├── mov_bwlq.s │ ├── movq.i │ ├── movq.s │ ├── movsbl.i │ ├── movsbl.s │ ├── movswl.i │ ├── movswl.s │ ├── movsxd.i │ ├── movsxd.s │ ├── movzb.i │ ├── movzb.s │ ├── movzbl.i │ ├── movzbl.s │ ├── movzwl.i │ ├── movzwl.s │ ├── movzx.i │ ├── movzx.s │ ├── not.i │ ├── not.s │ ├── or.i │ ├── or.s │ ├── push_pop.i │ ├── push_pop.s │ ├── reg.i │ ├── reg.s │ ├── rel.i │ ├── rel.s │ ├── sar.i │ ├── sar.s │ ├── set.i │ ├── set.s │ ├── shl.i │ ├── shl.s │ ├── shr.i │ ├── shr.s │ ├── sub.i │ ├── sub.s │ ├── xadd.i │ ├── xadd.s │ ├── xchg.i │ ├── xchg.s │ ├── xor.i │ ├── xor.s │ ├── xorl.i │ ├── xorl.s │ ├── zero.i │ └── zero.s ├── elf │ ├── build.tu │ ├── define.tu │ ├── elf.tu │ ├── header.tu │ └── header_64.tu ├── instruct │ ├── build.tu │ ├── build_1.tu │ ├── build_2.tu │ ├── common.tu │ └── instruct.tu ├── main.tu ├── parser │ ├── func.tu │ ├── instruct.tu │ ├── keyword.tu │ ├── parser.tu │ └── scanner.tu ├── test.sh ├── test.tu └── utils │ ├── file.tu │ ├── hexbytes.tu │ └── msg.tu ├── assets ├── compiler_compiler.png ├── compiler_helloworld.png ├── linker_helloworld.png ├── logo.svg └── tulang.gif ├── compiler ├── README.md ├── ast │ ├── ast.tu │ ├── class.tu │ ├── context.tu │ ├── contextparser.tu │ ├── define.tu │ ├── func.tu │ ├── struct.tu │ └── token.tu ├── bin │ └── amd64_linux_tuc ├── compile │ ├── cast.tu │ ├── comon.tu │ ├── compiler.tu │ ├── define.tu │ ├── func.tu │ ├── register.tu │ ├── store.tu │ ├── struct.tu │ └── variable.tu ├── gen │ ├── block.tu │ ├── builtin.tu │ ├── chain.tu │ ├── chain_static.tu │ ├── class.tu │ ├── common.tu │ ├── datastruct.tu │ ├── expr_compile.tu │ ├── func.tu │ ├── func_call.tu │ ├── func_dyncall.tu │ ├── future.tu │ ├── kv.tu │ ├── kv_array.tu │ ├── kv_map.tu │ ├── kv_static.tu │ ├── new.tu │ ├── new_static.tu │ ├── operator.tu │ ├── operator_helper.tu │ ├── package.tu │ ├── parser.tu │ ├── rettype.tu │ ├── statement.tu │ ├── statement_cond.tu │ ├── statement_match.tu │ ├── struct.tu │ ├── type.tu │ └── variable.tu ├── internal │ └── Internal.tu ├── main.tu ├── parser │ ├── block.tu │ ├── data.tu │ ├── expression.tu │ ├── future │ │ ├── async.tu │ │ ├── block.tu │ │ ├── parser.tu │ │ └── statement.tu │ ├── global.tu │ ├── keyword.tu │ ├── package │ │ ├── common.tu │ │ ├── geninits.tu │ │ ├── genvars.tu │ │ ├── package.tu │ │ ├── package2.tu │ │ └── pub.tu │ ├── parser.tu │ ├── scanner │ │ ├── define.tu │ │ ├── scan.tu │ │ └── scan_static.tu │ ├── statement.tu │ ├── static.tu │ └── struct.tu ├── test.sh ├── test │ ├── case │ └── token.tu ├── test_scaner2.tu ├── test_scanner.tu ├── test_static_token.tu └── utils │ ├── common.tu │ ├── fmt.tu │ ├── init.tu │ ├── msg.tu │ ├── sort.tu │ └── str.tu ├── devfile ├── library ├── fmt │ ├── assert.tu │ ├── fmt.tu │ └── vfprint.tu ├── os │ ├── args.tu │ ├── exit.tu │ ├── gc.tu │ ├── shell.tu │ ├── signal.tu │ └── sys.tu ├── std │ ├── array.tu │ ├── atomic │ │ └── atomic.tu │ ├── common.tu │ ├── define.tu │ ├── dir.tu │ ├── file.tu │ ├── header.tu │ ├── malloc.tu │ ├── map │ │ ├── common.tu │ │ ├── map.tu │ │ ├── node.tu │ │ └── static.tu │ ├── mem.tu │ ├── regex │ │ └── replace.tu │ ├── str.tu │ └── sys.tu ├── string │ ├── common.tu │ ├── dyn.tu │ ├── float.tu │ ├── header.tu │ ├── str.tu │ ├── string.tu │ └── wrap.tu └── time │ └── sleep.tu ├── linker ├── README.md ├── asserts │ ├── linker-compile.png │ └── linker-demo.png ├── bin │ ├── amd64_linux_tl1 │ ├── amd64_linux_tl2 │ └── demo ├── demo │ ├── init.s │ └── main.s ├── link │ ├── block.tu │ ├── define.tu │ ├── linker.tu │ ├── linker_build.tu │ ├── segment.tu │ └── sym_link.tu ├── linux │ ├── define.tu │ ├── file.tu │ ├── file_read.tu │ ├── file_write.tu │ ├── header_64.tu │ └── rel.tu ├── mac │ └── README.md ├── main.tu ├── test.sh ├── utils │ ├── file.tu │ └── msg.tu └── windows │ └── README.md ├── release ├── tu └── tulang.a ├── runtime ├── ast.tu ├── async.tu ├── common.tu ├── core.tu ├── debug │ ├── common.tu │ ├── elf.tu │ ├── header.tu │ ├── lines.tu │ ├── reader.tu │ └── stack.tu ├── gcbuflist.tu ├── gcmarksw.tu ├── gcproc.tu ├── kv.tu ├── linux.tu ├── malloc.tu ├── marena.tu ├── mheap.tu ├── mutex.tu ├── object.tu ├── object2.tu ├── pools.tu ├── runtimeinit.tu ├── utils.tu └── value.tu ├── syscall ├── asm_amd64.s ├── std_atomic.s ├── sys_os_amd64.s ├── sys_runtime_amd64.s └── sys_std_amd64.s ├── tests ├── README.md ├── async │ ├── basic.tu │ ├── dyn_mem.tu │ ├── passarg.tu │ └── static_mem.tu ├── class │ ├── chain_object.tu │ ├── file │ │ ├── define.tu │ │ ├── file2 │ │ │ ├── define.tu │ │ │ └── func.tu │ │ └── func.tu │ ├── object.tu │ ├── object_func.tu │ ├── object_inherit.tu │ ├── object_mem.tu │ ├── object_member_binary_operator.tu │ ├── object_member_unary_operator.tu │ ├── object_split.tu │ ├── object_super_synatx.tu │ └── pkg │ │ └── a.tu ├── common │ ├── class_member_initialization.tu │ ├── global_mem_var.tu │ ├── globalvar_initialization.tu │ ├── gmv │ │ ├── a.tu │ │ └── b.tu │ ├── init │ │ ├── init.tu │ │ ├── init1 │ │ │ └── init1.tu │ │ └── init_1.tu │ ├── init_automatic_call.tu │ ├── logor_logand_priority.tu │ ├── objmember │ │ ├── a.tu │ │ └── b.tu │ ├── package_call.tu │ ├── var │ │ ├── dyn │ │ │ ├── check.tu │ │ │ └── var.tu │ │ ├── native │ │ │ └── var.tu │ │ └── var.tu │ └── var_scope.tu ├── datastruct │ ├── array.tu │ ├── bool.tu │ ├── char.tu │ ├── closure.tu │ ├── ds_map.tu │ ├── float.tu │ ├── func.tu │ ├── global_int_string.tu │ ├── global_map_array.tu │ ├── int.tu │ ├── pkg1 │ │ ├── global_test.tu │ │ ├── pkg2 │ │ │ └── test.tu │ │ ├── test.tu │ │ └── test2.tu │ ├── pkg2 │ │ ├── array_map.tu │ │ └── test.tu │ └── string.tu ├── internalpkg │ ├── atomic_test.tu │ ├── atomic_test2.tu │ ├── common.tu │ ├── dir │ │ ├── a │ │ └── b │ ├── file │ │ └── file.txt │ ├── fmt.tu │ ├── os_exit.tu │ ├── os_gc.tu │ ├── os_shell.tu │ ├── os_signal_test.tu │ ├── runtime_common.tu │ ├── std_dir.tu │ ├── std_file.tu │ ├── std_map.tu │ ├── std_map_bench.tu │ ├── std_regex.tu │ ├── string.tu │ └── time_sleep.tu ├── memory │ ├── assign.tu │ ├── byte.tu │ ├── delref.tu │ ├── i8u64_dynamic.tu │ ├── i8u64_static.tu │ ├── net │ │ ├── ip.tu │ │ └── struct.tu │ ├── op_bitop.tu │ ├── op_compare1.tu │ ├── op_compare2.tu │ ├── op_div.tu │ ├── op_minus.tu │ ├── op_mod.tu │ ├── op_mul.tu │ ├── op_plus.tu │ ├── pointer.tu │ ├── statement.tu │ ├── struct.tu │ ├── struct_chain1.tu │ ├── struct_chain2.tu │ └── struct_inner.tu ├── mixed │ ├── chain.tu │ ├── logor_and.tu │ ├── passargs.tu │ ├── typeassert_base.tu │ ├── typeassert_basetype.tu │ ├── typeassert_chain.tu │ └── typeassert_staticmember.tu ├── native │ ├── arrop.tu │ ├── base_op.tu │ ├── float.tu │ ├── mem_chain_func.tu │ ├── memarr.tu │ ├── memclass.tu │ ├── memclass_field.tu │ ├── memclass_index_field.tu │ ├── pkg │ │ └── a.tu │ ├── pkg2 │ │ ├── a.tu │ │ └── b.tu │ ├── pointer.tu │ ├── stack_global_arr.tu │ ├── stack_global_memvar.tu │ ├── stack_global_memvar_define.tu │ ├── stack_local_arr.tu │ ├── static_arr_index.tu │ ├── std_str.tu │ ├── string.catfmt.tu │ ├── string.putc.tu │ ├── string_base.tu │ ├── struct.tu │ └── t │ │ ├── a.tu │ │ └── define.tu ├── operator │ ├── add.tu │ ├── add_agn.tu │ ├── bitand.tu │ ├── bitand_agn.tu │ ├── bitor.tu │ ├── bitor_agn.tu │ ├── bitxor.tu │ ├── bitxor_agn.tu │ ├── div.tu │ ├── div_agn.tu │ ├── equal.tu │ ├── equal_not.tu │ ├── greater_equal_than.tu │ ├── greater_than.tu │ ├── logand.tu │ ├── logor.tu │ ├── lower-equal-than.tu │ ├── lowerthan.tu │ ├── minus.tu │ ├── minus_agn.tu │ ├── minus_or_negative.tu │ ├── mul.tu │ ├── mul_agn.tu │ ├── precedence.tu │ ├── shift_left.tu │ ├── shift_left_agn.tu │ ├── shift_right.tu │ ├── shift_right_agn.tu │ └── unary.tu ├── runtime │ ├── kv_common.tu │ ├── len_cap.tu │ ├── malloc.tu │ ├── pools.tu │ ├── temp │ │ └── class.tu │ └── type.tu └── statement │ ├── args_variadic.tu │ ├── break.tu │ ├── chain.tu │ ├── continue.tu │ ├── for.tu │ ├── func.tu │ ├── func_arg.tu │ ├── func_arg_dyn.tu │ ├── if.tu │ ├── import.tu │ ├── match.tu │ ├── multi_assign.tu │ ├── multi_assign1.tu │ ├── multi_assign2.tu │ ├── nest │ ├── a.tu │ └── nest1 │ │ ├── a.tu │ │ └── nest2 │ │ └── a.tu │ ├── package.tu │ ├── pkg1 │ ├── global_test.tu │ ├── pkg2 │ │ └── test.tu │ ├── test.tu │ └── test2.tu │ ├── pkg2 │ ├── array_map.tu │ └── test.tu │ ├── return.tu │ ├── while.tu │ └── while2.tu ├── tests_all.sh ├── tools └── README.md └── tulang.tu /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.pyc 3 | *.si4project 4 | *CMakeCache* 5 | .idea 6 | .vscode 7 | *.out 8 | *.o 9 | *.swp 10 | tmp.s 11 | cmake-build-debug 12 | tests/*/*.s 13 | tests/*/*.o 14 | tests/*/a.out 15 | tests/a.out 16 | tests/*.cmake 17 | tests/Makefil1e 18 | tests/tmp.s 19 | tests/do 20 | tests/to 21 | tests/tulang 22 | linker/*.s 23 | linker/*.o 24 | linker/a.out 25 | compiler/*.s 26 | compiler/*.o 27 | compiler/a.out 28 | dev 29 | docs 30 | core 31 | asmer/*.s 32 | asmer/dev 33 | _tmp 34 | ./*.s 35 | core.[0-9]* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 版权 @2019-2023 tu-lang语言 作者。保留所有权利 2 | -------------------------------------------------------------------------------- /asmer/README.md: -------------------------------------------------------------------------------- 1 | # asmer 2 | 基于tu-lang自举 asmer汇编器 3 | -------------------------------------------------------------------------------- /asmer/asm/define.tu: -------------------------------------------------------------------------------- 1 | use asmer.elf 2 | use asmer.parser 3 | 4 | mem Asmer 5 | { 6 | parser.Parser* parser 7 | u64* out 8 | elf.ElfFile* elf 9 | i32 data ,text 10 | i32 bytes 11 | } 12 | trace = 0 13 | Pad1 = 1 14 | -------------------------------------------------------------------------------- /asmer/asm/instructs.tu: -------------------------------------------------------------------------------- 1 | use asmer.ast 2 | use asmer.utils 3 | use asmer.instruct 4 | 5 | Asmer::InstWrite() { 6 | utils.debug("Asmer::InstWrite".(i8)) 7 | for(i = 0 ; i < this.parser.funcs.len() ; i += 1){ 8 | 9 | f = this.parser.funcs.addr[i] 10 | for(j = 0 ; j < f.instructs.len() ; j += 1){ 11 | 12 | inst = f.instructs.addr[j] 13 | this.writeBytes(&inst.bytes,inst.size) 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /asmer/ast/symtables.tu: -------------------------------------------------------------------------------- 1 | use asmer.utils 2 | use std 3 | use std.map 4 | use string 5 | 6 | mem SymTable { 7 | map.Map* symbols //string => Sym* 8 | std.Array* data_symbol // Sym* 9 | } 10 | func mapstringhashkey(k){ 11 | return k.hash64() 12 | } 13 | SymTable::init(){ 14 | this.symbols = map.map_new(mapstringhashkey.(u64),0.(i8)) 15 | this.data_symbol = std.NewArray() 16 | } 17 | 18 | SymTable::hasName(name) { 19 | return this.symbols.find(name) != map.Null 20 | } 21 | SymTable::addSym(sym) { 22 | utils.debug("SymTable::addSym name:%S".(i8),sym.name.str()) 23 | if this.symbols.find(sym.name) != map.Null { 24 | pre_sym = this.symbols.find(sym.name) 25 | if pre_sym.global 26 | sym.global = true 27 | } 28 | this.symbols.insert(sym.name ,sym) 29 | if sym.segName.cmpstr(".data".(i8)) == string.Equal { 30 | this.data_symbol.push(sym) 31 | } 32 | 33 | } 34 | SymTable::getSym(name) { 35 | utils.debug("SymTable::getSym name:%S".(i8),name.str()) 36 | if this.hasName(name) == True { 37 | return this.symbols.find(name) 38 | }else{ 39 | sym = newSym(name,True) 40 | this.symbols.insert(name,sym) 41 | return sym 42 | } 43 | } -------------------------------------------------------------------------------- /asmer/bin/amd64_linux_tua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/asmer/bin/amd64_linux_tua -------------------------------------------------------------------------------- /asmer/cases/add.s: -------------------------------------------------------------------------------- 1 | imr: 2 | add $0 ,%rsp 3 | add $100 , %rbp 4 | add $100 , %rdi 5 | add $100 , %edi 6 | add $100 , %r9 7 | r4: 8 | add %edi , %eax 9 | add %ebx , %esi 10 | add %ecx , %edx 11 | add %edi , %esp 12 | add %ecx , %ebp 13 | r8: 14 | add %rdi, %rax 15 | add %rdi , %r8 16 | add %r9 , %rdi 17 | add %rdi , %rsp 18 | add %rdi , %rbp 19 | mem: 20 | add %rdi , (%rsp) 21 | add %rdi , (%rbp) 22 | add %rcx , 100(%rsp) 23 | add %rcx , 100(%rbp) 24 | add %rcx , 130(%rbp) 25 | add %rcx , -130(%rbp) 26 | add %rax , (%rdi) 27 | bigi: 28 | add $100, %rax 29 | add $192, %rax 30 | add $192, %eax 31 | add $192, %rcx 32 | add $192, %rdx 33 | trbp: 34 | add $0 , (%rbp) 35 | add $0x4 , (%rbp) 36 | add $0x4 , 10(%rbp) 37 | add $0x48 , (%rbp) 38 | add $-128 , (%rbp) 39 | addq $128 , (%rbp) 40 | addq $0xaaaaaa , (%rbp) 41 | addq $0xaaaaaa , 10(%rbp) 42 | trsp: 43 | add $0 , (%rsp) 44 | add $0x4 , (%rsp) 45 | add $0x4 , 10(%rsp) 46 | add $0x48 , (%rsp) 47 | add $-128 , (%rsp) 48 | addq $128 , (%rsp) 49 | addq $128 , 10(%rsp) 50 | addq $0xaaaaaa , (%rsp) 51 | addq $0xaaaaaa , 130(%rsp) 52 | addq $0xaaaaaa , -130(%rsp) 53 | -------------------------------------------------------------------------------- /asmer/cases/and.s: -------------------------------------------------------------------------------- 1 | imr: 2 | and $0 ,%rsp 3 | and $100 , %rbp 4 | and $100 , %rdi 5 | and $100 , %edi 6 | and $100 , %r9 7 | r4: 8 | and %edi , %eax 9 | and %ebx , %esi 10 | and %ecx , %edx 11 | and %edi , %esp 12 | and %ecx , %ebp 13 | r8: 14 | and %rdi, %rax 15 | and %rdi , %r8 16 | and %r9 , %rdi 17 | and %rdi , %rsp 18 | and %rdi , %rbp 19 | mem: 20 | and %rdi , (%rsp) 21 | and %rdi , (%rbp) 22 | and %rcx , 100(%rsp) 23 | and %rcx , 100(%rbp) 24 | and %rcx , 130(%rbp) 25 | and %rcx , -130(%rbp) 26 | and %rax , (%rdi) 27 | bigi: 28 | and $100, %rax 29 | and $192, %rax 30 | and $192, %eax 31 | and $192, %rcx 32 | and $192, %rdx 33 | 34 | i8i: 35 | and $1 , %rax 36 | and $1 , %eax 37 | and $1 , %al 38 | and $1 , %cl 39 | and $1 , %dl 40 | and $1 , %bl 41 | and %dl, %al 42 | and %edx, %eax 43 | and %rdx, %rax 44 | -------------------------------------------------------------------------------- /asmer/cases/call.i: -------------------------------------------------------------------------------- 1 | 2 | call.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: e8 00 00 00 00 callq 5 9 | 5: e8 0a 00 00 00 callq 14 10 | a: ff d0 callq *%rax 11 | c: 41 ff d2 callq *%r10 12 | f: e8 ec ff ff ff callq 0 13 | 14 | 0000000000000014 : 15 | 14: 48 c7 c0 01 00 00 00 mov $0x1,%rax 16 | -------------------------------------------------------------------------------- /asmer/cases/call.s: -------------------------------------------------------------------------------- 1 | l.call: 2 | call extern 3 | call l.1 4 | call *%rax 5 | call *%r10 6 | call l.call 7 | l.1: 8 | mov $1, %rax 9 | -------------------------------------------------------------------------------- /asmer/cases/cdq.i: -------------------------------------------------------------------------------- 1 | 2 | cdq.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 99 cltd 9 | 1: 99 cltd 10 | 2: 48 99 cqto 11 | -------------------------------------------------------------------------------- /asmer/cases/cdq.s: -------------------------------------------------------------------------------- 1 | main: 2 | cdq 3 | cltd 4 | cqo 5 | -------------------------------------------------------------------------------- /asmer/cases/cmp.s: -------------------------------------------------------------------------------- 1 | imm: 2 | cmp $1,%rax 3 | cmp $0,%eax 4 | rr: 5 | cmp %rdi,%rax 6 | cmp %edi,%eax 7 | cmp %rsi,%rdi 8 | 9 | common: 10 | cmp $-129,%rax 11 | cmp $-129,%eax 12 | cmp $-129,%rcx 13 | cmp $-129,%ecx 14 | cmp $127,%rax 15 | cmp $127,%eax 16 | cmp $127,%rcx 17 | cmp $127,%ecx 18 | cmp $128,%rax 19 | cmp $128,%eax 20 | cmp $128,%rcx 21 | cmp $128,%ecx 22 | cmp $0xff,%rax 23 | cmp $0xff,%eax 24 | cmp $0xff,%rcx 25 | cmp $0xff,%ecx 26 | cmp $0xffffffff,%eax 27 | cmp $0xffffffff,%ecx 28 | cmp $0xfffffffffffff001,%rax 29 | cmp $0xfffffffffffff001,%eax 30 | cmp $0xfffffffffffff001,%rcx 31 | cmp $0xfffffffffffff001,%ecx 32 | 33 | r2m: 34 | cmp $1 , %rax 35 | cmp $192 , %rax 36 | cmp $1 , 16(%rax) 37 | cmpq $192 , 16(%rax) 38 | cmp $1 , 32(%rdi) 39 | cmp $1 , 32(%rsp) -------------------------------------------------------------------------------- /asmer/cases/cmpxchgl.i: -------------------------------------------------------------------------------- 1 | 2 | cmpxchgl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 0f b1 11 cmpxchg %edx,(%rcx) 9 | 3: 41 0f b1 11 cmpxchg %edx,(%r9) 10 | 7: 0f b1 c1 cmpxchg %eax,%ecx 11 | a: 0f b1 c8 cmpxchg %ecx,%eax 12 | 0000000000000000 : 13 | 0: f0 0f b0 1a lock cmpxchg %bl,(%rdx) 14 | 4: f0 0f b0 02 lock cmpxchg %al,(%rdx) 15 | 8: f0 0f b0 0a lock cmpxchg %cl,(%rdx) -------------------------------------------------------------------------------- /asmer/cases/cmpxchgl.s: -------------------------------------------------------------------------------- 1 | r4: 2 | cmpxchgl %edx , (%rcx) 3 | cmpxchgl %edx , (%r9) 4 | cmpxchgl %eax , %ecx 5 | cmpxchgl %ecx , %eax 6 | rmr1: 7 | lock cmpxchgb %bl, (%rdx) 8 | lock cmpxchgb %al, (%rdx) 9 | lock cmpxchgb %cl, (%rdx) 10 | -------------------------------------------------------------------------------- /asmer/cases/cmpxchgq.i: -------------------------------------------------------------------------------- 1 | 2 | cmpxchgq.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 0f b1 11 cmpxchg %rdx,(%rcx) 9 | 4: 48 0f b1 c1 cmpxchg %rax,%rcx 10 | 8: 49 0f b1 c1 cmpxchg %rax,%r9 11 | c: 4c 0f b1 c7 cmpxchg %r8,%rdi 12 | 10: 4d 0f b1 c1 cmpxchg %r8,%r9 13 | -------------------------------------------------------------------------------- /asmer/cases/cmpxchgq.s: -------------------------------------------------------------------------------- 1 | r8: 2 | cmpxchgq %rdx , (%rcx) 3 | cmpxchgq %rax , %rcx 4 | cmpxchgq %rax , %r9 5 | cmpxchgq %r8 , %rdi 6 | cmpxchgq %r8 , %r9 7 | 8 | -------------------------------------------------------------------------------- /asmer/cases/div_idiv.i: -------------------------------------------------------------------------------- 1 | 2 | div_idiv.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: f6 f0 div %al 9 | 2: f6 f7 div %bh 10 | 4: f7 f0 div %eax 11 | 6: 48 f7 f0 div %rax 12 | 9: f7 f7 div %edi 13 | b: 48 f7 f7 div %rdi 14 | e: 49 f7 f0 div %r8 15 | 11: 48 f7 f0 div %rax 16 | 17 | 0000000000000014 : 18 | 14: f6 f8 idiv %al 19 | 16: f6 ff idiv %bh 20 | 18: f7 f8 idiv %eax 21 | 1a: 48 f7 f8 idiv %rax 22 | 1d: f7 ff idiv %edi 23 | 1f: 48 f7 ff idiv %rdi 24 | 22: 49 f7 f8 idiv %r8 25 | 25: 48 f7 f0 div %rax 26 | 27 | 0000000000000000 : 28 | 0: 48 f7 f8 idiv %rax 29 | 3: 48 f7 ff idiv %rdi 30 | 6: 49 f7 f8 idiv %r8 31 | 9: 49 f7 f9 idiv %r9 32 | c: 48 f7 7c 24 08 idivq 0x8(%rsp) 33 | 11: 48 f7 7c 24 f0 idivq -0x10(%rsp) 34 | 16: 48 f7 7d 08 idivq 0x8(%rbp) 35 | 1a: 48 f7 7d f0 idivq -0x10(%rbp) 36 | 1e: 48 f7 f0 div %rax 37 | 21: 49 f7 f1 div %r9 38 | 24: 48 f7 38 idivq (%rax) 39 | 27: 48 f7 78 08 idivq 0x8(%rax) 40 | 2b: 49 f7 39 idivq (%r9) 41 | 2e: 49 f7 79 f0 idivq -0x10(%r9) 42 | -------------------------------------------------------------------------------- /asmer/cases/div_idiv.s: -------------------------------------------------------------------------------- 1 | ldiv: 2 | div %al 3 | div %bh 4 | div %eax 5 | div %rax 6 | div %edi 7 | div %rdi 8 | div %r8 9 | div %rax 10 | lidiv: 11 | idiv %al 12 | idiv %bh 13 | idiv %eax 14 | idiv %rax 15 | idiv %edi 16 | idiv %rdi 17 | idiv %r8 18 | div %rax 19 | 20 | lidivq: 21 | idivq %rax 22 | idivq %rdi 23 | idivq %r8 24 | idivq %r9 25 | idivq 8(%rsp) 26 | idivq -16(%rsp) 27 | idivq 8(%rbp) 28 | idivq -16(%rbp) 29 | div %rax 30 | div %r9 31 | idivq (%rax) 32 | idivq 8(%rax) 33 | idivq (%r9) 34 | idivq -16(%r9) -------------------------------------------------------------------------------- /asmer/cases/imul.i: -------------------------------------------------------------------------------- 1 | 2 | imul.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 6b c0 08 imul $0x8,%rax,%rax 9 | 4: 4d 6b c9 08 imul $0x8,%r9,%r9 10 | 8: 6b c0 08 imul $0x8,%eax,%eax 11 | 12 | 000000000000000b : 13 | b: 48 0f af c7 imul %rdi,%rax 14 | f: 4c 0f af c7 imul %rdi,%r8 15 | 13: 49 0f af c0 imul %r8,%rax 16 | 17: 4c 89 c0 mov %r8,%rax 17 | 1a: 0f af c7 imul %edi,%eax 18 | 19 | 000000000000001d : 20 | 1d: 48 6b c0 64 imul $0x64,%rax,%rax 21 | 21: 48 69 c0 c0 00 00 00 imul $0xc0,%rax,%rax 22 | 28: 69 c0 c0 00 00 00 imul $0xc0,%eax,%eax 23 | 2e: 48 69 c9 c0 00 00 00 imul $0xc0,%rcx,%rcx 24 | 35: 48 69 d2 c0 00 00 00 imul $0xc0,%rdx,%rdx 25 | -------------------------------------------------------------------------------- /asmer/cases/imul.s: -------------------------------------------------------------------------------- 1 | main1: 2 | imul $8 ,%rax 3 | imul $8 ,%r9 4 | imul $8 ,%eax 5 | main2: 6 | imul %rdi , %rax 7 | imul %rdi , %r8 8 | imul %r8 , %rax 9 | mov %r8 , %rax 10 | imul %edi , %eax 11 | bigi: 12 | imul $100, %rax 13 | imul $192, %rax 14 | imul $192, %eax 15 | imul $192, %rcx 16 | imul $192, %rdx 17 | -------------------------------------------------------------------------------- /asmer/cases/jbe.i: -------------------------------------------------------------------------------- 1 | 2 | jbe.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 83 f8 00 cmp $0x0,%eax 9 | 3: 0f 86 09 00 00 00 jbe e 10 | 5: 48 c7 c0 01 00 00 00 mov $0x1,%rax 11 | c: 89 c3 mov %eax,%ebx 12 | e: 83 f8 01 cmp $0x1,%eax 13 | 11: 0f 86 f5 ff ff ff jbe c -------------------------------------------------------------------------------- /asmer/cases/jbe.s: -------------------------------------------------------------------------------- 1 | main: 2 | cmp $0,%eax 3 | jbe .L.TRUE.007 4 | mov $1, %rax 5 | .L.TRUE.006: 6 | mov %eax,%ebx 7 | .L.TRUE.007: 8 | cmp $1,%eax 9 | jbe .L.TRUE.006 -------------------------------------------------------------------------------- /asmer/cases/je.i: -------------------------------------------------------------------------------- 1 | 2 | je.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 83 f8 00 cmp $0x0,%eax 9 | 3: 0f 84 09 00 00 00 je e 10 | 5: 48 c7 c0 01 00 00 00 mov $0x1,%rax 11 | c: 89 c3 mov %eax,%ebx 12 | e: 83 f8 01 cmp $0x1,%eax 13 | 11: 0f 84 f5 ff ff ff je c 14 | 13: 0f 84 8b 00 00 00 je a0 15 | 19: 0f 84 00 00 00 00 je 1b 16 | 17 | 000000000000001b : 18 | 1b: 48 c7 c0 01 00 00 00 mov $0x1,%rax 19 | 22: 48 c7 c0 02 00 00 00 mov $0x2,%rax 20 | 29: 48 c7 c0 03 00 00 00 mov $0x3,%rax 21 | 30: 48 c7 c0 04 00 00 00 mov $0x4,%rax 22 | 37: 48 c7 c0 05 00 00 00 mov $0x5,%rax 23 | 3e: 48 c7 c0 06 00 00 00 mov $0x6,%rax 24 | 45: 48 c7 c0 07 00 00 00 mov $0x7,%rax 25 | 4c: 48 c7 c0 08 00 00 00 mov $0x8,%rax 26 | 53: 48 c7 c0 09 00 00 00 mov $0x9,%rax 27 | 5a: 48 c7 c0 0a 00 00 00 mov $0xa,%rax 28 | 61: 48 c7 c0 0b 00 00 00 mov $0xb,%rax 29 | 68: 48 c7 c0 0c 00 00 00 mov $0xc,%rax 30 | 6f: 48 c7 c0 0d 00 00 00 mov $0xd,%rax 31 | 76: 48 c7 c0 0e 00 00 00 mov $0xe,%rax 32 | 7d: 48 c7 c0 0f 00 00 00 mov $0xf,%rax 33 | 84: 48 c7 c0 10 00 00 00 mov $0x10,%rax 34 | 8b: 48 c7 c0 11 00 00 00 mov $0x11,%rax 35 | 92: 48 c7 c0 12 00 00 00 mov $0x12,%rax 36 | 99: 48 c7 c0 13 00 00 00 mov $0x13,%rax 37 | 38 | 00000000000000a0 : 39 | a0: 48 c7 c0 14 00 00 00 mov $0x14,%rax 40 | -------------------------------------------------------------------------------- /asmer/cases/je.s: -------------------------------------------------------------------------------- 1 | main: 2 | cmp $0,%eax 3 | je .L.TRUE.007 4 | mov $1, %rax 5 | .L.TRUE.006: 6 | mov %eax,%ebx 7 | .L.TRUE.007: 8 | cmp $1,%eax 9 | je .L.TRUE.006 10 | je L2 11 | je L1 12 | L1: 13 | mov $1 , %rax # 7字节 14 | mov $2 , %rax 15 | mov $3 , %rax 16 | mov $4 , %rax 17 | mov $5 , %rax 18 | mov $6 , %rax 19 | mov $7 , %rax 20 | mov $8 , %rax 21 | mov $9 , %rax 22 | mov $10 , %rax 23 | mov $11 , %rax 24 | mov $12 , %rax 25 | mov $13 , %rax 26 | mov $14 , %rax 27 | mov $15 , %rax 28 | mov $16 , %rax 29 | mov $17 , %rax 30 | mov $18 , %rax 31 | mov $19 , %rax 32 | L2: 33 | mov $20 , %rax 34 | -------------------------------------------------------------------------------- /asmer/cases/jg.i: -------------------------------------------------------------------------------- 1 | 2 | jg.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 83 f8 00 cmp $0x0,%eax 9 | 3: 0f 8f 09 00 00 00 jg e 10 | 5: 48 c7 c0 01 00 00 00 mov $0x1,%rax 11 | c: 89 c3 mov %eax,%ebx 12 | e: 83 f8 01 cmp $0x1,%eax 13 | 11: 0f 8f f5 ff ff ff jg c 14 | 15 | 000000000000001b : 16 | 1b: 0f 8d df ff ff ff jge 0
17 | 21: 0f 8d 02 00 00 00 jge 29 18 | 27: c3 retq 19 | 28: c3 retq 20 | 21 | 0000000000000029 : 22 | 29: c3 retq -------------------------------------------------------------------------------- /asmer/cases/jg.s: -------------------------------------------------------------------------------- 1 | main: 2 | cmp $0,%eax 3 | jg .L.TRUE.007 4 | mov $1, %rax 5 | .L.TRUE.006: 6 | mov %eax,%ebx 7 | .L.TRUE.007: 8 | cmp $1,%eax 9 | jg .L.TRUE.006 10 | 11 | main2: 12 | jge main 13 | jge main3 14 | ret 15 | ret 16 | main3: 17 | ret 18 | 19 | 20 | -------------------------------------------------------------------------------- /asmer/cases/jle.i: -------------------------------------------------------------------------------- 1 | 2 | jle.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 83 f8 00 cmp $0x0,%eax 9 | 3: 0f 8e 09 00 00 00 jle e 10 | 5: 48 c7 c0 01 00 00 00 mov $0x1,%rax 11 | c: 89 c3 mov %eax,%ebx 12 | e: 83 f8 01 cmp $0x1,%eax 13 | 11: 0f 8e f5 ff ff ff jle c 14 | -------------------------------------------------------------------------------- /asmer/cases/jle.s: -------------------------------------------------------------------------------- 1 | main: 2 | cmp $0,%eax 3 | jle .L.TRUE.007 4 | mov $1, %rax 5 | .L.TRUE.006: 6 | mov %eax,%ebx 7 | .L.TRUE.007: 8 | cmp $1,%eax 9 | jle .L.TRUE.006 10 | -------------------------------------------------------------------------------- /asmer/cases/jmp.i: -------------------------------------------------------------------------------- 1 | 2 | jmp.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: e9 05 00 00 00 jmp 4 9 | 2: e9 00 00 00 00 jmp 4 10 | 11 | 0000000000000004 : 12 | 4: e9 f1 ff ff ff jmp 0
13 | 6: 48 c7 c0 01 00 00 00 mov $0x1,%rax 14 | d: e9 00 00 00 00 jmpq 12 15 | 12: e9 85 00 00 00 jmpq 9c 16 | 17 | 0000000000000017 : 18 | 17: 48 c7 c0 01 00 00 00 mov $0x1,%rax 19 | 1e: 48 c7 c0 02 00 00 00 mov $0x2,%rax 20 | 25: 48 c7 c0 03 00 00 00 mov $0x3,%rax 21 | 2c: 48 c7 c0 04 00 00 00 mov $0x4,%rax 22 | 33: 48 c7 c0 05 00 00 00 mov $0x5,%rax 23 | 3a: 48 c7 c0 06 00 00 00 mov $0x6,%rax 24 | 41: 48 c7 c0 07 00 00 00 mov $0x7,%rax 25 | 48: 48 c7 c0 08 00 00 00 mov $0x8,%rax 26 | 4f: 48 c7 c0 09 00 00 00 mov $0x9,%rax 27 | 56: 48 c7 c0 0a 00 00 00 mov $0xa,%rax 28 | 5d: 48 c7 c0 0b 00 00 00 mov $0xb,%rax 29 | 64: 48 c7 c0 0c 00 00 00 mov $0xc,%rax 30 | 6b: 48 c7 c0 0d 00 00 00 mov $0xd,%rax 31 | 72: 48 c7 c0 0e 00 00 00 mov $0xe,%rax 32 | 79: 48 c7 c0 0f 00 00 00 mov $0xf,%rax 33 | 80: 48 c7 c0 10 00 00 00 mov $0x10,%rax 34 | 87: 48 c7 c0 11 00 00 00 mov $0x11,%rax 35 | 8e: 48 c7 c0 12 00 00 00 mov $0x12,%rax 36 | 95: 48 c7 c0 13 00 00 00 mov $0x13,%rax 37 | 38 | 000000000000009c : 39 | 9c: 48 c7 c0 14 00 00 00 mov $0x14,%rax 40 | a3: e9 6f ff ff ff jmpq 17 41 | -------------------------------------------------------------------------------- /asmer/cases/jmp.s: -------------------------------------------------------------------------------- 1 | main: 2 | jmp test 3 | jmp test 4 | test: 5 | jmp main 6 | mov $1 , %rax 7 | jmp extern 8 | jmp long_end 9 | long_jmp: 10 | mov $1 , %rax # 7字节 11 | mov $2 , %rax 12 | mov $3 , %rax 13 | mov $4 , %rax 14 | mov $5 , %rax 15 | mov $6 , %rax 16 | mov $7 , %rax 17 | mov $8 , %rax 18 | mov $9 , %rax 19 | mov $10 , %rax 20 | mov $11 , %rax 21 | mov $12 , %rax 22 | mov $13 , %rax 23 | mov $14 , %rax 24 | mov $15 , %rax 25 | mov $16 , %rax 26 | mov $17 , %rax 27 | mov $18 , %rax 28 | mov $19 , %rax 29 | long_end: 30 | mov $20 , %rax # 140字节 31 | jmp long_jmp 32 | 33 | -------------------------------------------------------------------------------- /asmer/cases/jne.i: -------------------------------------------------------------------------------- 1 | 2 | jne.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 83 f8 00 cmp $0x0,%eax 9 | 3: 0f 85 09 00 00 00 jne e 10 | 5: 48 c7 c0 01 00 00 00 mov $0x1,%rax 11 | c: 89 c3 mov %eax,%ebx 12 | e: 83 f8 01 cmp $0x1,%eax 13 | 11: 0f 85 f5 ff ff ff jne c 14 | -------------------------------------------------------------------------------- /asmer/cases/jne.s: -------------------------------------------------------------------------------- 1 | main: 2 | cmp $0,%eax 3 | jne .L.TRUE.007 4 | mov $1, %rax 5 | .L.TRUE.006: 6 | mov %eax,%ebx 7 | .L.TRUE.007: 8 | cmp $1,%eax 9 | jne .L.TRUE.006 10 | -------------------------------------------------------------------------------- /asmer/cases/lea.i: -------------------------------------------------------------------------------- 1 | 2 | lea.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 8d 35 10 00 00 00 lea 0x10(%rip),%rsi # 17 9 | 7: 48 8d 35 00 00 00 00 lea 0x0(%rip),%rsi # e 10 | e: 48 8d 74 24 10 lea 0x10(%rsp),%rsi 11 | 13: 48 8d 45 20 lea 0x20(%rbp),%rax 12 | 13 | 0000000000000017 : 14 | 17: 48 8d 40 10 lea 0x10(%rax),%rax 15 | 1b: 4c 8d 48 f0 lea -0x10(%rax),%r9 16 | 1f: 49 8d 51 10 lea 0x10(%r9),%rdx 17 | 23: 4d 8d 41 f0 lea -0x10(%r9),%r8 18 | 27: 4d 8d 81 82 00 00 00 lea 0x82(%r9),%r8 19 | 2e: 4d 8d 81 74 ff ff ff lea -0x8c(%r9),%r8 20 | 21 | 0000000000000035 : 22 | 35: 48 8d 15 c4 ff ff ff lea -0x3c(%rip),%rdx # 0 23 | 3c: 48 8d 15 d4 ff ff ff lea -0x2c(%rip),%rdx # 17 24 | -------------------------------------------------------------------------------- /asmer/cases/lea.s: -------------------------------------------------------------------------------- 1 | mem: 2 | lea test(%rip), %rsi 3 | lea extern1(%rip), %rsi 4 | lea 16(%rsp), %rsi 5 | lea 32(%rbp), %rax 6 | test: 7 | lea 16(%rax) , %rax 8 | lea -16(%rax) , %r9 9 | lea 16(%r9) , %rdx 10 | lea -16(%r9) , %r8 11 | lea 130(%r9) , %r8 12 | lea -140(%r9) , %r8 13 | test1: 14 | lea mem(%rip) ,%rdx 15 | lea test(%rip) ,%rdx 16 | 17 | -------------------------------------------------------------------------------- /asmer/cases/mov.s: -------------------------------------------------------------------------------- 1 | main: 2 | mov %al,%bl 3 | mov %eax,%ecx 4 | mov %rax ,%rcx 5 | mov %r8 ,%r9 6 | mov %r8 ,%rax 7 | mov %rax ,%r8 8 | mov $1 ,%rax 9 | mov $300 ,%rax 10 | mov $100 , %r9 11 | r1: 12 | mov %al, (%rdi) 13 | mov %ax, (%rdi) 14 | sub %al, (%rdi) 15 | add %al, (%rdi) 16 | mov %rax, (%rdi) 17 | mov %rax, 10(%rdi) 18 | mov %rax, 130(%rdi) 19 | mov (%rdi), %rax 20 | mov 10(%rdi), %rax 21 | mov 130(%rdi), %rax 22 | bigim: 23 | mov $11887393157837578923,%rdx 24 | mov $-1 , %rax 25 | mov $-4250523714016506264,%rsi 26 | mov $2147483647 , %rax # int max 27 | mov $1010521725724951834,%rdx 28 | mov $-2147483648,%rax 29 | 30 | r4: 31 | mov $0 , %eax 32 | mov $0 , %ebx 33 | 34 | fsrrm: 35 | movq %fs:0xfffffffffffffff0,%rax 36 | movq %fs:0xfffffffffffffff0,%rcx 37 | movq %fs:0xfffffffffffffff0,%rdx 38 | movq (%rsp) , %rax 39 | movq (%rdi) , %rax 40 | fsrmr: 41 | movq %rax,%fs:0xfffffffffffffff0 42 | movq %rcx,%fs:0xfffffffffffffff0 43 | movq %rdx,%fs:0xfffffffffffffff0 44 | movq %rax,(%rsp) 45 | movq %rax,(%rdi) 46 | -------------------------------------------------------------------------------- /asmer/cases/mov_bwlq.s: -------------------------------------------------------------------------------- 1 | rr: 2 | movb $58 , (%rax) 3 | movw $58 , (%rax) 4 | movw $128 , (%rax) 5 | movl $58 , (%rax) 6 | movq $58 , (%rax) 7 | b1: 8 | movb $58 , (%rax) 9 | movb $58 , (%rdi) 10 | movb $58 , 63(%rax) 11 | movb $58 , 63(%rdi) 12 | movb $127 , 63(%rax) 13 | movb $128 , 63(%rax) 14 | movb $128 , 68(%rax) 15 | movb $129 , 63(%rax) 16 | movb $129 , 127(%rax) 17 | movb $129 , 128(%rax) 18 | movb $129 , 128(%rdi) 19 | w1: 20 | movw $58 , (%rax) 21 | movw $58 , (%rdi) 22 | movw $58 , 63(%rax) 23 | movw $58 , 63(%rdi) 24 | movw $127 , 63(%rax) 25 | movw $128 , 63(%rax) 26 | movw $128 , 68(%rax) 27 | movw $129 , 63(%rax) 28 | movw $129 , 127(%rax) 29 | movw $129 , 128(%rax) 30 | movw $129 , 128(%rdi) 31 | l1: 32 | movl $58 , (%rax) 33 | movl $58 , (%rdi) 34 | movl $58 , 63(%rax) 35 | movl $58 , 63(%rdi) 36 | movl $127 , 63(%rax) 37 | movl $128 , 63(%rax) 38 | movl $128 , 68(%rax) 39 | movl $129 , 63(%rax) 40 | movl $129 , 127(%rax) 41 | movl $129 , 128(%rax) 42 | movl $129 , 128(%rdi) 43 | q1: 44 | movq $58 , (%rax) 45 | movq $58 , (%rdi) 46 | movq $58 , 63(%rax) 47 | movq $58 , 63(%rdi) 48 | movq $127 , 63(%rax) 49 | movq $128 , 63(%rax) 50 | movq $128 , 68(%rax) 51 | movq $129 , 63(%rax) 52 | movq $129 , 127(%rax) 53 | movq $129 , 128(%rax) 54 | movq $129 , 128(%rdi) 55 | 56 | 57 | -------------------------------------------------------------------------------- /asmer/cases/movq.i: -------------------------------------------------------------------------------- 1 | 2 | movq.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 89 e0 mov %rsp,%rax 9 | 3: 48 89 e8 mov %rbp,%rax 10 | 6: 48 89 f8 mov %rdi,%rax 11 | 9: 48 89 f0 mov %rsi,%rax 12 | c: 48 89 d0 mov %rdx,%rax 13 | f: 48 89 c8 mov %rcx,%rax 14 | 12: 4c 89 c0 mov %r8,%rax 15 | 15: 4c 89 c8 mov %r9,%rax 16 | 18: 48 89 c0 mov %rax,%rax 17 | 1b: 48 89 d8 mov %rbx,%rax 18 | 1e: 48 b8 00 00 00 00 84 movabs $0x41a7d78400000000,%rax 19 | 25: d7 a7 41 20 | 28: 48 b8 00 00 00 00 84 movabs $0x41a7d78400000000,%rax 21 | 2f: d7 a7 41 22 | 23 | 24 | 0000000000000000 : 25 | 0: f3 0f 7e 45 f8 movq -0x8(%rbp),%xmm0 26 | 5: f3 44 0f 7e 4d 10 movq 0x10(%rbp),%xmm9 27 | b: 66 0f d6 45 f8 movq %xmm0,-0x8(%rbp) 28 | 10: 66 44 0f d6 4d 10 movq %xmm9,0x10(%rbp) 29 | 16: 66 48 0f 6e c0 movq %rax,%xmm0 30 | 1b: 66 4d 0f 6e e0 movq %r8,%xmm12 31 | 20: 66 48 0f 7e c0 movq %xmm0,%rax 32 | 25: 66 4d 0f 7e e0 movq %xmm12,%r8 -------------------------------------------------------------------------------- /asmer/cases/movq.s: -------------------------------------------------------------------------------- 1 | r8: 2 | movq %rsp , %rax 3 | movq %rbp , %rax 4 | movq %rdi , %rax 5 | movq %rsi , %rax 6 | movq %rdx , %rax 7 | movq %rcx , %rax 8 | movq %r8 , %rax 9 | movq %r9 , %rax 10 | movq %rax , %rax 11 | movq %rbx , %rax 12 | movq $4730986895511650304, %rax 13 | movabsq $4730986895511650304, %rax 14 | 15 | float: 16 | movq -8(%rbp), %xmm0 17 | movq 16(%rbp), %xmm9 18 | movq %xmm0 , -8(%rbp) 19 | movq %xmm9 , 16(%rbp) 20 | movq %rax, %xmm0 21 | movq %r8, %xmm12 22 | movq %xmm0 , %rax 23 | movq %xmm12, %r8 24 | -------------------------------------------------------------------------------- /asmer/cases/movsbl.i: -------------------------------------------------------------------------------- 1 | 2 | movsbl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 0f be c0 movsbl %al,%eax 9 | 3: 0f be c3 movsbl %bl,%eax 10 | 6: 0f be 05 07 00 00 00 movsbl 0x7(%rip),%eax # 14 11 | d: 0f be 05 00 00 00 00 movsbl 0x0(%rip),%eax # 14 12 | 13 | 0000000000000014 : 14 | 14: 48 c7 c0 01 00 00 00 mov $0x1,%rax 15 | -------------------------------------------------------------------------------- /asmer/cases/movsbl.s: -------------------------------------------------------------------------------- 1 | main: 2 | movsbl %al , %eax 3 | movsbl %bl , %eax 4 | movsbl lable(%rip) , %eax 5 | movsbl e1(%rip) , %eax 6 | lable: 7 | mov $1 , %rax 8 | -------------------------------------------------------------------------------- /asmer/cases/movswl.i: -------------------------------------------------------------------------------- 1 | 2 | movswl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 0f bf 00 movswl (%rax),%eax 9 | 3: 0f bf 18 movswl (%rax),%ebx 10 | 6: 41 0f bf 00 movswl (%r8),%eax 11 | a: 41 0f bf 20 movswl (%r8),%esp 12 | e: 41 0f bf 60 0a movswl 0xa(%r8),%esp 13 | 13: 41 0f bf a0 8c 00 00 movswl 0x8c(%r8),%esp 14 | 1a: 00 15 | 16 | 000000000000001b : 17 | 1b: 0f bf c0 movswl %ax,%eax 18 | -------------------------------------------------------------------------------- /asmer/cases/movswl.s: -------------------------------------------------------------------------------- 1 | rm2r: 2 | movswl (%rax), %eax 3 | movswl (%rax), %ebx 4 | movswl (%r8), %eax 5 | movswl (%r8), %esp 6 | movswl 10(%r8), %esp 7 | movswl 140(%r8), %esp 8 | r2r: 9 | movswl %ax, %eax 10 | # movswl %ax, %ebp 11 | -------------------------------------------------------------------------------- /asmer/cases/movsxd.i: -------------------------------------------------------------------------------- 1 | 2 | movsxd.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 48 89 c3 mov %rax,%rbx 9 | 3: 48 63 c0 movslq %eax,%rax 10 | 6: 48 63 d8 movslq %eax,%rbx 11 | 9: 4c 63 c1 movslq %ecx,%r8 12 | -------------------------------------------------------------------------------- /asmer/cases/movsxd.s: -------------------------------------------------------------------------------- 1 | main: 2 | mov %rax, %rbx 3 | movsxd %eax, %rax 4 | movsxd %eax, %rbx 5 | movsxd %ecx ,%r8 6 | -------------------------------------------------------------------------------- /asmer/cases/movzb.i: -------------------------------------------------------------------------------- 1 | 2 | movzb.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 48 0f b6 c0 movzbq %al,%rax 9 | 4: 0f b6 c0 movzbl %al,%eax 10 | 7: 4c 0f b6 c0 movzbq %al,%r8 11 | b: 4c 0f b6 08 movzbq (%rax),%r9 12 | f: 4d 0f b6 08 movzbq (%r8),%r9 13 | 13: 49 0f b6 00 movzbq (%r8),%rax 14 | 17: 49 0f b6 40 0a movzbq 0xa(%r8),%rax 15 | 1c: 49 0f b6 80 8c 00 00 movzbq 0x8c(%r8),%rax 16 | 23: 00 17 | 24: 48 0f b6 c3 movzbq %bl,%rax 18 | 28: 0f b6 c3 movzbl %bl,%eax 19 | 2b: 4c 0f b6 c3 movzbq %bl,%r8 20 | 2f: 48 0f b6 05 26 00 00 movzbq 0x26(%rip),%rax # 5d 21 | 36: 00 22 | 37: 0f b6 05 1f 00 00 00 movzbl 0x1f(%rip),%eax # 5d 23 | 3e: 4c 0f b6 05 17 00 00 movzbq 0x17(%rip),%r8 # 5d 24 | 45: 00 25 | 46: 48 0f b6 05 00 00 00 movzbq 0x0(%rip),%rax # 4e 26 | 4d: 00 27 | 4e: 0f b6 05 00 00 00 00 movzbl 0x0(%rip),%eax # 55 28 | 55: 4c 0f b6 05 00 00 00 movzbq 0x0(%rip),%r8 # 5d 29 | 5c: 00 30 | 31 | 000000000000005d : 32 | 5d: 4c 0f b6 05 00 00 00 movzbq 0x0(%rip),%r8 # 65 33 | 64: 00 34 | -------------------------------------------------------------------------------- /asmer/cases/movzb.s: -------------------------------------------------------------------------------- 1 | main: 2 | movzb %al , %rax 3 | movzb %al , %eax 4 | movzb %al , %r8 5 | movzb (%rax), %r9 6 | movzb (%r8), %r9 7 | movzb (%r8), %rax 8 | movzb 10(%r8), %rax 9 | movzb 140(%r8), %rax 10 | 11 | movzb %bl , %rax 12 | movzb %bl , %eax 13 | movzb %bl , %r8 14 | #内部 15 | movzb lable(%rip) , %rax 16 | movzb lable(%rip) , %eax 17 | movzb lable(%rip) , %r8 18 | #外部 19 | movzb e1(%rip) , %rax 20 | movzb e1(%rip) , %eax 21 | movzb e1(%rip) , %r8 22 | lable: 23 | movzb e1(%rip) , %r8 24 | -------------------------------------------------------------------------------- /asmer/cases/movzbl.i: -------------------------------------------------------------------------------- 1 | 2 | movzbl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 48 0f b6 c0 movzbq %al,%rax 9 | 4: 0f b6 c0 movzbl %al,%eax 10 | 7: 4c 0f b6 c0 movzbq %al,%r8 11 | b: 4c 0f b6 08 movzbq (%rax),%r9 12 | f: 0f b6 00 movzbl (%rax),%eax 13 | 12: 4d 0f b6 08 movzbq (%r8),%r9 14 | 16: 49 0f b6 00 movzbq (%r8),%rax 15 | 1a: 49 0f b6 40 0a movzbq 0xa(%r8),%rax 16 | 1f: 49 0f b6 80 8c 00 00 movzbq 0x8c(%r8),%rax 17 | 26: 00 18 | 27: 48 0f b6 c3 movzbq %bl,%rax 19 | 2b: 0f b6 c3 movzbl %bl,%eax 20 | 2e: 4c 0f b6 c3 movzbq %bl,%r8 21 | 32: 48 0f b6 05 26 00 00 movzbq 0x26(%rip),%rax # 60 22 | 39: 00 23 | 3a: 0f b6 05 1f 00 00 00 movzbl 0x1f(%rip),%eax # 60 24 | 41: 4c 0f b6 05 17 00 00 movzbq 0x17(%rip),%r8 # 60 25 | 48: 00 26 | 49: 48 0f b6 05 00 00 00 movzbq 0x0(%rip),%rax # 51 27 | 50: 00 28 | 51: 0f b6 05 00 00 00 00 movzbl 0x0(%rip),%eax # 58 29 | 58: 4c 0f b6 05 00 00 00 movzbq 0x0(%rip),%r8 # 60 30 | 5f: 00 31 | 32 | 0000000000000060 : 33 | 60: 48 c7 c0 01 00 00 00 mov $0x1,%rax 34 | -------------------------------------------------------------------------------- /asmer/cases/movzbl.s: -------------------------------------------------------------------------------- 1 | main: 2 | movzbl %al , %rax 3 | movzbl %al , %eax 4 | movzbl %al , %r8 5 | movzbl (%rax), %r9 6 | movzbl (%rax), %eax 7 | movzbl (%r8), %r9 8 | movzbl (%r8), %rax 9 | movzbl 10(%r8), %rax 10 | movzbl 140(%r8), %rax 11 | movzbl %bl , %rax 12 | movzbl %bl , %eax 13 | movzbl %bl , %r8 14 | movzbl lable(%rip) , %rax 15 | movzbl lable(%rip) , %eax 16 | movzbl lable(%rip) , %r8 17 | movzbl e1(%rip) , %rax 18 | movzbl e1(%rip) , %eax 19 | movzbl e1(%rip) , %r8 20 | lable: 21 | mov $1 , %rax 22 | 23 | -------------------------------------------------------------------------------- /asmer/cases/movzwl.i: -------------------------------------------------------------------------------- 1 | 2 | movzwl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 0f b7 00 movzwl (%rax),%eax 9 | 3: 48 0f b7 38 movzwq (%rax),%rdi 10 | 7: 41 0f b7 00 movzwl (%r8),%eax 11 | b: 41 0f b7 40 0a movzwl 0xa(%r8),%eax 12 | 10: 4d 0f b7 08 movzwq (%r8),%r9 13 | 14: 4d 0f b7 88 8c 00 00 movzwq 0x8c(%r8),%r9 14 | 1b: 00 15 | 16 | 000000000000001c : 17 | 1c: 0f b7 c0 movzwl %ax,%eax 18 | 1f: 48 0f b7 c0 movzwq %ax,%rax 19 | -------------------------------------------------------------------------------- /asmer/cases/movzwl.s: -------------------------------------------------------------------------------- 1 | rm2r: 2 | movzwl (%rax), %eax 3 | movzwl (%rax), %rdi 4 | movzwl (%r8), %eax 5 | movzwl 10(%r8), %eax 6 | movzwl (%r8), %r9 7 | movzwl 140(%r8), %r9 8 | r2r: 9 | movzwl %ax, %eax 10 | movzwl %ax, %rax 11 | -------------------------------------------------------------------------------- /asmer/cases/movzx.i: -------------------------------------------------------------------------------- 1 | 2 | movzx.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: 48 0f b6 c0 movzbq %al,%rax 9 | 4: 0f b6 c0 movzbl %al,%eax 10 | 7: 4c 0f b6 c0 movzbq %al,%r8 11 | b: 48 0f b6 c3 movzbq %bl,%rax 12 | f: 0f b6 c3 movzbl %bl,%eax 13 | 12: 4c 0f b6 c3 movzbq %bl,%r8 14 | 16: 48 0f b6 05 26 00 00 movzbq 0x26(%rip),%rax # 44 15 | 1d: 00 16 | 1e: 0f b6 05 1f 00 00 00 movzbl 0x1f(%rip),%eax # 44 17 | 25: 4c 0f b6 05 17 00 00 movzbq 0x17(%rip),%r8 # 44 18 | 2c: 00 19 | 2d: 48 0f b6 05 00 00 00 movzbq 0x0(%rip),%rax # 35 20 | 34: 00 21 | 35: 0f b6 05 00 00 00 00 movzbl 0x0(%rip),%eax # 3c 22 | 3c: 4c 0f b6 05 00 00 00 movzbq 0x0(%rip),%r8 # 44 23 | 43: 00 24 | 25 | 0000000000000044 : 26 | 44: 48 c7 c0 01 00 00 00 mov $0x1,%rax 27 | -------------------------------------------------------------------------------- /asmer/cases/movzx.s: -------------------------------------------------------------------------------- 1 | main: 2 | movzx %al , %rax 3 | movzx %al , %eax 4 | movzx %al , %r8 5 | 6 | movzx %bl , %rax 7 | movzx %bl , %eax 8 | movzx %bl , %r8 9 | #内部 10 | movzx lable(%rip) , %rax 11 | movzx lable(%rip) , %eax 12 | movzx lable(%rip) , %r8 13 | #外部 14 | movzx e1(%rip) , %rax 15 | movzx e1(%rip) , %eax 16 | movzx e1(%rip) , %r8 17 | lable: 18 | mov $1 , %rax 19 | 20 | -------------------------------------------------------------------------------- /asmer/cases/not.i: -------------------------------------------------------------------------------- 1 | 2 | not.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 f7 d0 not %rax 9 | 3: 48 f7 d1 not %rcx 10 | 6: 49 f7 d1 not %r9 11 | 12 | 0000000000000009 : 13 | 9: f7 d0 not %eax 14 | b: f7 d2 not %edx 15 | -------------------------------------------------------------------------------- /asmer/cases/not.s: -------------------------------------------------------------------------------- 1 | r8: 2 | not %rax 3 | not %rcx 4 | not %r9 5 | r4: 6 | not %eax 7 | not %edx 8 | 9 | -------------------------------------------------------------------------------- /asmer/cases/or.i: -------------------------------------------------------------------------------- 1 | 2 | or.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 09 f8 or %rdi,%rax 9 | 3: 49 09 f8 or %rdi,%r8 10 | 6: 49 09 c9 or %rcx,%r9 11 | 9: 4c 09 c8 or %r9,%rax 12 | 13 | 000000000000000c : 14 | c: 09 f8 or %edi,%eax 15 | e: 09 fa or %edi,%edx 16 | 17 | 0000000000000000 : 18 | 0: 08 d0 or %dl,%al 19 | 2: 08 c1 or %al,%cl -------------------------------------------------------------------------------- /asmer/cases/or.s: -------------------------------------------------------------------------------- 1 | r8r8: 2 | or %rdi,%rax 3 | or %rdi,%r8 4 | or %rcx,%r9 5 | or %r9,%rax 6 | r4r4: 7 | or %edi,%eax 8 | or %edi,%edx 9 | r1r1: 10 | or %dl , %al 11 | or %al , %cl 12 | -------------------------------------------------------------------------------- /asmer/cases/push_pop.i: -------------------------------------------------------------------------------- 1 | 2 | push_pop.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 6a 64 pushq $0x64 9 | 2: 68 90 00 00 00 pushq $0x90 10 | 7: 68 01 01 00 00 pushq $0x101 11 | c: 68 aa aa aa 00 pushq $0xaaaaaa 12 | 13 | 0000000000000011 : 14 | 11: 50 push %rax 15 | 12: 57 push %rdi 16 | 13: 41 51 push %r9 17 | 15: 55 push %rbp 18 | 16: 54 push %rsp 19 | 20 | 0000000000000000 : 21 | 0: ff 34 24 pushq (%rsp) 22 | 3: ff 74 24 10 pushq 0x10(%rsp) 23 | 7: ff 30 pushq (%rax) 24 | 9: ff 70 10 pushq 0x10(%rax) 25 | c: ff 37 pushq (%rdi) 26 | e: ff 77 10 pushq 0x10(%rdi) 27 | 11: 41 ff 31 pushq (%r9) 28 | 14: 41 ff 71 10 pushq 0x10(%r9) 29 | 30 | 0000000000000017 : 31 | 17: 58 pop %rax 32 | 18: 5f pop %rdi 33 | 19: 5d pop %rbp 34 | 1a: 41 58 pop %r8 35 | 1c: 41 59 pop %r9 36 | 1e: 5c pop %rsp 37 | 1f: 5d pop %rbp 38 | 20: 8f 00 popq (%rax) 39 | 22: 41 8f 01 popq (%r9) 40 | 25: 8f 44 24 0a popq 0xa(%rsp) 41 | 29: 8f 40 0a popq 0xa(%rax) 42 | 2c: 41 8f 41 0a popq 0xa(%r9) 43 | 30: 41 8f 81 7e ff ff ff popq -0x82(%r9) 44 | 37: 41 8f 81 82 00 00 00 popq 0x82(%r9) 45 | -------------------------------------------------------------------------------- /asmer/cases/push_pop.s: -------------------------------------------------------------------------------- 1 | pushri: 2 | push $100 #1字节 3 | push $144 4 | push $257 #2字节 5 | push $0xaaaaaa #4字节 6 | pushr8: 7 | push %rax 8 | push %rdi 9 | push %r9 10 | push %rbp 11 | push %rsp 12 | 13 | pushm: 14 | push (%rsp) 15 | push 16(%rsp) 16 | push (%rax) 17 | push 16(%rax) 18 | push (%rdi) 19 | push 16(%rdi) 20 | push (%r9) 21 | push 16(%r9) 22 | 23 | popr8: 24 | pop %rax 25 | pop %rdi 26 | pop %rbp 27 | pop %r8 28 | pop %r9 29 | pop %rsp 30 | pop %rbp 31 | pop (%rax) 32 | pop (%r9) 33 | pop 10(%rsp) 34 | pop 10(%rax) 35 | pop 10(%r9) 36 | pop -130(%r9) 37 | pop 130(%r9) 38 | 39 | -------------------------------------------------------------------------------- /asmer/cases/reg.i: -------------------------------------------------------------------------------- 1 | 2 | reg.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 49 89 d4 mov %rdx,%r12 9 | 3: 49 89 cd mov %rcx,%r13 10 | 6: 4d 89 c6 mov %r8,%r14 11 | 9: 48 89 f4 mov %rsi,%rsp 12 | c: 4c 89 e7 mov %r12,%rdi 13 | f: 4c 89 ef mov %r13,%rdi 14 | 12: 4c 89 f1 mov %r14,%rcx 15 | 15: 4c 89 fa mov %r15,%rdx 16 | 17 | 0000000000000018 : 18 | 18: 48 c7 c2 00 00 00 00 mov $0x0,%rdx 19 | 1f: 49 c7 c2 00 00 00 00 mov $0x0,%r10 20 | 26: 49 c7 c6 64 00 00 00 mov $0x64,%r14 21 | 2d: 49 c7 c0 00 00 00 00 mov $0x0,%r8 22 | 34: 48 c7 c0 38 00 00 00 mov $0x38,%rax 23 | 3b: 41 ff d5 callq *%r13 -------------------------------------------------------------------------------- /asmer/cases/reg.s: -------------------------------------------------------------------------------- 1 | rr: 2 | mov %rdx , %r12 3 | mov %rcx , %r13 4 | mov %r8 , %r14 5 | mov %rsi , %rsp 6 | mov %r12 , %rdi 7 | mov %r13 , %rdi 8 | mov %r14 , %rcx 9 | mov %r15 , %rdx 10 | imr: 11 | mov $0 , %rdx 12 | mov $0 , %r10 13 | mov $100 , %r14 14 | mov $0 , %r8 15 | mov $56, %rax 16 | callq *%r13 -------------------------------------------------------------------------------- /asmer/cases/rel.i: -------------------------------------------------------------------------------- 1 | 2 | rel.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # 7 9 | 7: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # e 10 | -------------------------------------------------------------------------------- /asmer/cases/rel.s: -------------------------------------------------------------------------------- 1 | .global func 2 | func: 3 | lea out1(%rip), %rax 4 | mov out2(%rip), %rax 5 | -------------------------------------------------------------------------------- /asmer/cases/sar.i: -------------------------------------------------------------------------------- 1 | 2 | sar.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 c1 f8 64 sar $0x64,%rax 9 | 4: 49 c1 f9 64 sar $0x64,%r9 10 | 8: c1 f8 64 sar $0x64,%eax 11 | b: c1 fd 64 sar $0x64,%ebp 12 | e: c1 fc 64 sar $0x64,%esp 13 | 14 | 0000000000000011 : 15 | 11: d3 f8 sar %cl,%eax 16 | 13: d3 fd sar %cl,%ebp 17 | 15: d3 fc sar %cl,%esp 18 | 17: d3 ff sar %cl,%edi 19 | 20 | 0000000000000019 : 21 | 19: 48 d3 f8 sar %cl,%rax 22 | 1c: 49 d3 f8 sar %cl,%r8 23 | 1f: 48 d3 fd sar %cl,%rbp 24 | 22: 48 d3 fc sar %cl,%rsp 25 | -------------------------------------------------------------------------------- /asmer/cases/sar.s: -------------------------------------------------------------------------------- 1 | rim: 2 | sar $100 , %rax 3 | sar $100 , %r9 4 | sar $100 , %eax 5 | sar $100 , %ebp 6 | sar $100 , %esp 7 | r4: 8 | sar %cl , %eax 9 | sar %cl , %ebp 10 | sar %cl , %esp 11 | sar %cl , %edi 12 | r8: 13 | sar %cl , %rax 14 | sar %cl , %r8 15 | sar %cl , %rbp 16 | sar %cl , %rsp 17 | -------------------------------------------------------------------------------- /asmer/cases/set.s: -------------------------------------------------------------------------------- 1 | l.sete: 2 | sete %al 3 | sete %bh 4 | l.setne: 5 | setne %al 6 | setne %bh 7 | l.setb: 8 | setb %al 9 | setb %bl 10 | setb %bh 11 | l.setbe: 12 | setbe %al 13 | setbe %bl 14 | setbe %bh 15 | l.setl: 16 | setl %al 17 | setl %bl 18 | setl %bh 19 | l.setle: 20 | setle %al 21 | setle %bl 22 | setle %bh 23 | l.setg: 24 | setg %al 25 | setg %bl 26 | setg %bh 27 | l.setge: 28 | setge %al 29 | setge %bl 30 | setge %bh 31 | l.setz: 32 | setz %al 33 | setz %bl 34 | setz %bh 35 | l.setnz: 36 | setnz %al 37 | setnz %bl 38 | setnz %bh 39 | l.seta: 40 | seta %al 41 | seta %bl 42 | seta %bh 43 | l.setae: 44 | setae %al 45 | setae %bl 46 | setae %bh 47 | 48 | l.setp_n: 49 | setnp %dl 50 | setnp %al 51 | setp %dl 52 | setp %al 53 | sete %dl 54 | sete %al 55 | -------------------------------------------------------------------------------- /asmer/cases/shl.i: -------------------------------------------------------------------------------- 1 | 2 | shl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: d3 e0 shl %cl,%eax 9 | 2: d3 e7 shl %cl,%edi 10 | 4: d2 e5 shl %cl,%ch 11 | 6: d2 e7 shl %cl,%bh 12 | 8: 48 d3 e0 shl %cl,%rax 13 | b: 48 d3 e1 shl %cl,%rcx 14 | e: 48 d3 e4 shl %cl,%rsp 15 | 11: 48 d3 e7 shl %cl,%rdi 16 | 14: 49 d3 e0 shl %cl,%r8 17 | 18 | 0000000000000017 : 19 | 17: 48 c1 e0 00 shl $0x0,%rax 20 | 1b: 48 c1 e7 00 shl $0x0,%rdi 21 | 1f: 49 c1 e0 64 shl $0x64,%r8 22 | 23: c1 e0 64 shl $0x64,%eax 23 | 26: 48 c1 e0 c0 shl $0xc0,%rax 24 | -------------------------------------------------------------------------------- /asmer/cases/shl.s: -------------------------------------------------------------------------------- 1 | main: 2 | shl %cl,%eax 3 | shl %cl,%edi 4 | shl %cl,%ch 5 | shl %cl,%bh 6 | shl %cl,%rax 7 | shl %cl,%rcx 8 | shl %cl,%rsp 9 | shl %cl,%rdi 10 | shl %cl,%r8 11 | im: 12 | shl $0,%rax 13 | shl $0,%rdi 14 | shl $100, %r8 15 | shl $100, %eax 16 | shl $192, %rax 17 | 18 | -------------------------------------------------------------------------------- /asmer/cases/shr.i: -------------------------------------------------------------------------------- 1 | 2 | shr.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: d3 e8 shr %cl,%eax 9 | 2: d3 ea shr %cl,%edx 10 | 11 | 0000000000000004 : 12 | 4: 48 d3 e8 shr %cl,%rax 13 | 7: 49 d3 e9 shr %cl,%r9 14 | 15 | 000000000000000a : 16 | a: 48 c1 e8 00 shr $0x0,%rax 17 | e: 48 c1 ef 00 shr $0x0,%rdi 18 | 12: 49 c1 e8 64 shr $0x64,%r8 19 | 16: c1 e8 64 shr $0x64,%eax 20 | 19: 48 c1 e8 c0 shr $0xc0,%rax 21 | -------------------------------------------------------------------------------- /asmer/cases/shr.s: -------------------------------------------------------------------------------- 1 | r1r2: 2 | shr %cl, %eax 3 | shr %cl, %edx 4 | r1r8: 5 | shr %cl, %rax 6 | shr %cl, %r9 7 | im: 8 | shr $0,%rax 9 | shr $0,%rdi 10 | shr $100, %r8 11 | shr $100, %eax 12 | shr $192, %rax 13 | -------------------------------------------------------------------------------- /asmer/cases/sub.i: -------------------------------------------------------------------------------- 1 | 2 | sub.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 83 ec 00 sub $0x0,%rsp 9 | 4: 48 83 ed 64 sub $0x64,%rbp 10 | 8: 48 83 ef 64 sub $0x64,%rdi 11 | c: 83 ef 64 sub $0x64,%edi 12 | f: 49 83 e9 64 sub $0x64,%r9 13 | 14 | 0000000000000013 : 15 | 13: 29 f8 sub %edi,%eax 16 | 15: 29 de sub %ebx,%esi 17 | 17: 29 ca sub %ecx,%edx 18 | 19: 29 fc sub %edi,%esp 19 | 1b: 29 cd sub %ecx,%ebp 20 | 21 | 000000000000001d : 22 | 1d: 48 29 f8 sub %rdi,%rax 23 | 20: 49 29 f8 sub %rdi,%r8 24 | 23: 4c 29 cf sub %r9,%rdi 25 | 26: 48 29 fc sub %rdi,%rsp 26 | 29: 48 29 fd sub %rdi,%rbp 27 | 28 | 000000000000002c : 29 | 2c: 48 29 3c 24 sub %rdi,(%rsp) 30 | 30: 48 29 7d 00 sub %rdi,0x0(%rbp) 31 | 34: 48 29 4c 24 64 sub %rcx,0x64(%rsp) 32 | 39: 48 29 8c 24 82 00 00 sub %rcx,0x82(%rsp) 33 | 40: 00 34 | 41: 48 29 8c 24 7e ff ff sub %rcx,-0x82(%rsp) 35 | 48: ff 36 | 49: 48 29 4d 64 sub %rcx,0x64(%rbp) 37 | 4d: 48 29 07 sub %rax,(%rdi) 38 | 39 | 0000000000000050 : 40 | 50: 48 83 e8 64 sub $0x64,%rax 41 | 54: 48 2d c0 00 00 00 sub $0xc0,%rax 42 | 5a: 2d c0 00 00 00 sub $0xc0,%eax 43 | 5f: 48 81 e9 c0 00 00 00 sub $0xc0,%rcx 44 | 66: 48 81 ea c0 00 00 00 sub $0xc0,%rdx 45 | -------------------------------------------------------------------------------- /asmer/cases/sub.s: -------------------------------------------------------------------------------- 1 | imr: 2 | sub $0 ,%rsp 3 | sub $100 , %rbp 4 | sub $100 , %rdi 5 | sub $100 , %edi 6 | sub $100 , %r9 7 | r4: 8 | sub %edi , %eax 9 | sub %ebx , %esi 10 | sub %ecx , %edx 11 | sub %edi , %esp 12 | sub %ecx , %ebp 13 | r8: 14 | sub %rdi, %rax 15 | sub %rdi , %r8 16 | sub %r9 , %rdi 17 | sub %rdi , %rsp 18 | sub %rdi , %rbp 19 | mem: 20 | sub %rdi , (%rsp) 21 | sub %rdi , (%rbp) 22 | sub %rcx , 100(%rsp) 23 | sub %rcx , 130(%rsp) 24 | sub %rcx , -130(%rsp) 25 | sub %rcx , 100(%rbp) 26 | sub %rax , (%rdi) 27 | bigi: 28 | sub $100, %rax 29 | sub $192, %rax 30 | sub $192, %eax 31 | sub $192, %rcx 32 | sub $192, %rdx 33 | -------------------------------------------------------------------------------- /asmer/cases/xadd.i: -------------------------------------------------------------------------------- 1 | 2 | xadd.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 0f c1 08 xadd %rcx,(%rax) 9 | 4: 49 0f c1 09 xadd %rcx,(%r9) 10 | 8: 4c 0f c1 c9 xadd %r9,%rcx 11 | c: 4d 0f c1 c8 xadd %r9,%r8 12 | 13 | 0000000000000010 : 14 | 10: 0f c1 08 xadd %ecx,(%rax) 15 | 13: 41 0f c1 09 xadd %ecx,(%r9) 16 | 17: 0f c1 ca xadd %ecx,%edx 17 | -------------------------------------------------------------------------------- /asmer/cases/xadd.s: -------------------------------------------------------------------------------- 1 | r8: 2 | xadd %rcx , (%rax) 3 | xadd %rcx , (%r9) 4 | xadd %r9 , %rcx 5 | xadd %r9 , %r8 6 | r4: 7 | xadd %ecx , (%rax) 8 | xadd %ecx , (%r9) 9 | xadd %ecx , %edx 10 | -------------------------------------------------------------------------------- /asmer/cases/xchg.i: -------------------------------------------------------------------------------- 1 | 2 | xchg.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 97 xchg %rax,%rdi 9 | 2: 48 87 cf xchg %rcx,%rdi 10 | 5: 49 91 xchg %rax,%r9 11 | 7: 4c 87 ca xchg %r9,%rdx 12 | a: 4d 87 c8 xchg %r9,%r8 13 | d: 48 87 0a xchg %rcx,(%rdx) 14 | 15 | 0000000000000010 : 16 | 10: 87 08 xchg %ecx,(%rax) 17 | 12: 41 87 09 xchg %ecx,(%r9) 18 | 15: 87 ca xchg %ecx,%edx 19 | -------------------------------------------------------------------------------- /asmer/cases/xchg.s: -------------------------------------------------------------------------------- 1 | r8: 2 | xchg %rax , %rdi 3 | xchg %rcx , %rdi 4 | xchg %rax , %r9 5 | xchg %r9 , %rdx 6 | xchg %r9 , %r8 7 | xchg %rcx , (%rdx) 8 | r4: 9 | xchg %ecx , (%rax) 10 | xchg %ecx , (%r9) 11 | xchg %ecx , %edx 12 | -------------------------------------------------------------------------------- /asmer/cases/xor.i: -------------------------------------------------------------------------------- 1 | 2 | xor.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 48 31 f8 xor %rdi,%rax 9 | 3: 49 31 f9 xor %rdi,%r9 10 | 6: 4c 31 c0 xor %r8,%rax 11 | 9: 4d 31 c1 xor %r8,%r9 12 | 13 | 000000000000000c : 14 | c: 31 f8 xor %edi,%eax 15 | e: 31 cf xor %ecx,%edi 16 | -------------------------------------------------------------------------------- /asmer/cases/xor.s: -------------------------------------------------------------------------------- 1 | r8r8: 2 | xor %rdi,%rax 3 | xor %rdi,%r9 4 | xor %r8, %rax 5 | xor %r8, %r9 6 | r4: 7 | xor %edi,%eax 8 | xor %ecx,%edi 9 | -------------------------------------------------------------------------------- /asmer/cases/xorl.i: -------------------------------------------------------------------------------- 1 | 2 | xorl.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 : 8 | 0: 31 f8 xor %edi,%eax 9 | 2: 31 cf xor %ecx,%edi 10 | -------------------------------------------------------------------------------- /asmer/cases/xorl.s: -------------------------------------------------------------------------------- 1 | r4: 2 | xorl %edi,%eax 3 | xorl %ecx,%edi 4 | -------------------------------------------------------------------------------- /asmer/cases/zero.i: -------------------------------------------------------------------------------- 1 | 2 | zero.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000
: 8 | 0: f0 c3 lock retq 9 | 2: c3 retq 10 | 3: c9 leaveq 11 | 4: c9 leaveq 12 | 5: 0f 05 syscall 13 | 6: 0f 01 f9 rdtscp 14 | 7: f3 90 pause 15 | -------------------------------------------------------------------------------- /asmer/cases/zero.s: -------------------------------------------------------------------------------- 1 | main: 2 | lock 3 | retq 4 | ret 5 | leaveq 6 | leave 7 | syscall 8 | rdtscp 9 | pause 10 | -------------------------------------------------------------------------------- /asmer/elf/header_64.tu: -------------------------------------------------------------------------------- 1 | # u16 Elf32_Half; 2 | # u16 Elf64_Half; 3 | # u32 Elf32_Word; 4 | # int32 Elf32_Sword; 5 | # u32 Elf64_Word; 6 | # int32 Elf64_Sword; 7 | 8 | # u64 Elf32_Xword; 9 | # int64 Elf32_Sxword; 10 | # u64 Elf64_Xword; 11 | # int64 Elf64_Sxword; 12 | 13 | # u32 Elf32_Addr 14 | # u64 Elf64_Addr 15 | # u32 Elf32_Off; 16 | # u64 Elf64_Off 17 | # u16 Elf32_Section; 18 | # u16 Elf64_Section; 19 | # u16 Elf32_Versym; 20 | # u16 Elf64_Versym; 21 | 22 | mem Elf64_Ehdr 23 | { 24 | u8 e_ident[16] 25 | u16 e_type 26 | u16 e_machine 27 | u32 e_version 28 | u64 e_entry 29 | u64 e_phoff 30 | u64 e_shoff 31 | u32 e_flags 32 | 33 | u16 e_ehsize 34 | u16 e_phentsize 35 | u16 e_phnum 36 | u16 e_shentsize 37 | u16 e_shnum 38 | u16 e_shstrndx 39 | } 40 | 41 | mem Elf64_Phdr 42 | { 43 | u32 p_type 44 | u32 p_flags 45 | u64 p_offset 46 | u64 p_vaddr 47 | u64 p_paddr 48 | u64 p_filesz 49 | u64 p_memsz 50 | u64 p_align 51 | } 52 | 53 | mem Elf64_Shdr 54 | { 55 | u32 sh_name 56 | u32 sh_type 57 | u64 sh_flags 58 | u64 sh_addr 59 | u64 sh_offset 60 | u64 sh_size 61 | u32 sh_link 62 | u32 sh_info 63 | u64 sh_addralign 64 | u64 sh_entsize 65 | } 66 | 67 | mem Elf64_Sym 68 | { 69 | u32 st_name 70 | u8 st_info 71 | u8 st_other 72 | u16 st_shndx 73 | u64 st_value 74 | u64 st_size 75 | } 76 | 77 | mem Elf64_Rela 78 | { 79 | u64 r_offset 80 | u64 r_info 81 | i64 r_addend 82 | } -------------------------------------------------------------------------------- /asmer/instruct/build.tu: -------------------------------------------------------------------------------- 1 | use asmer.ast 2 | use asmer.utils 3 | use asmer.instruct 4 | 5 | Instruct::genZeroInst() { 6 | utils.debug("Instruct::genZeroInst()".(i8)) 7 | i = this.type - ast.KW_RET 8 | opcode = opcode0[i] 9 | if(this.type == ast.KW_CQO) 10 | this.append1(0x48.(i8)) 11 | if this.type == ast.KW_RDTSCP 12 | this.append1(0x0f.(i8)) 13 | if(this.need2byte_op2()) 14 | this.append2(opcode) 15 | else 16 | this.append1(opcode) 17 | } 18 | Instruct::gen(){ 19 | utils.debug("Instruct::gen %d %d".(i8),this,&this.type) 20 | if this == null 21 | utils.errorf("Instruct::gen this is null %d",int(this)) 22 | token = this.type 23 | if( token >= ast.KW_MOV && token <= ast.KW_LEA ) 24 | this.genTwoInst() 25 | else if( token >= ast.KW_CALL && token <= ast.KW_POP ) 26 | this.genOneInst() 27 | else if(token >= ast.KW_RET && token <= ast.KW_CDQ) 28 | this.genZeroInst() 29 | else 30 | utils.error("[instruct gen] unknow instuct\n") 31 | utils.debug("Instruct::gen done %S".(i8),this.str.str()) 32 | } 33 | -------------------------------------------------------------------------------- /asmer/parser/func.tu: -------------------------------------------------------------------------------- 1 | use asmer.ast 2 | use asmer.utils 3 | use asmer.instruct 4 | use string 5 | 6 | Parser::parseFunction(labelname) { 7 | utils.debug("Parser::parseFunction(%S)".(i8),labelname.str()) 8 | this.check(this.scanner.curtoken >= ast.KW_MOV && this.scanner.curtoken <= ast.KW_CDQ,"should be any valid inst [mov-ret]") 9 | 10 | fc = new ast.Function(labelname) 11 | 12 | inst = null 13 | loop { 14 | 15 | token = this.scanner.curtoken 16 | 17 | if( token >= ast.KW_MOV && token <= ast.KW_LEA ) 18 | inst = this.parseTwoInstruct() 19 | else if( token >= ast.KW_CALL && token <= ast.KW_POP ) 20 | inst = this.parseOneInstruct() 21 | else if(token >= ast.KW_RET && token <= ast.KW_CDQ) 22 | inst = this.parseZeroInstruct() 23 | else 24 | utils.errorf("[Parser] unknow instruct:%s\n",this.scanner.curlex.dyn()) 25 | if inst == null 26 | utils.errorf("[Parser] something wrong:%s\n",this.scanner.curlex.dyn()) 27 | 28 | fc.instructs.push(inst) 29 | //.loc 30 | LPARSE_LOC: 31 | if this.scanner.curtoken == ast.KW_DEBUG_LOC { 32 | this.next_expect(ast.TK_NUMBER,".loc {?}") 33 | this.next_expect(ast.TK_NUMBER,".loc {?} {?}") 34 | this.scanner.scan() 35 | goto LPARSE_LOC 36 | } 37 | 38 | if(this.scanner.curtoken >= ast.KW_MOV && this.scanner.curtoken <= ast.KW_CDQ){} else{ 39 | break 40 | } 41 | } 42 | 43 | return fc 44 | } 45 | -------------------------------------------------------------------------------- /asmer/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | log(){ 3 | str="$1" 4 | echo -e "\033[32m$str \033[0m " 5 | } 6 | failed(){ 7 | str="$1" 8 | echo -e "\033[31m$str \033[0m" 9 | ps aux|grep test.sh|awk '{print $2}' |xargs kill -9 10 | exit 1 11 | } 12 | clean() { 13 | if ls $1 > /dev/null 2>&1; then 14 | rm -rf $1 15 | fi 16 | } 17 | check(){ 18 | if [ "$?" != 0 ]; then 19 | # actual=`./a.out` 20 | # if [ "$?" != 0 ]; then 21 | failed "exec failed" 22 | # fi 23 | # rm ./a.out 24 | fi 25 | 26 | } 27 | 28 | test_asmer_compile(){ 29 | log "[compile] tu -s asmer/main.tu " 30 | tu -s asmer/main.tu 31 | check 32 | tu -c . 33 | tu -o . -o /usr/local/lib/colib/ 34 | check 35 | chmod 777 a.out 36 | log "./a.out -p asmer/cases" 37 | ./a.out -p asmer/cases 38 | check 39 | clean "a.out" 40 | clean "*.s" 41 | clean "*.o" 42 | echo "test asmer_compile done..." 43 | return 44 | } 45 | test_all(){ 46 | log "[compile] tu -s asmer/test.tu " 47 | tu -s asmer/test.tu 48 | check 49 | tu -c . 50 | tu -o . -o /usr/local/lib/colib 51 | check 52 | chmod 777 a.out 53 | ./a.out 54 | check 55 | clean "a.out" 56 | clean "*.s" 57 | clean "*.o" 58 | echo "test asmer_compile done..." 59 | return 60 | } 61 | clean "./*.s" 62 | clean "./*.o" 63 | test_asmer_compile 64 | clean "./*.s" 65 | clean "./*.o" 66 | test_all 67 | log "all passing...." 68 | -------------------------------------------------------------------------------- /asmer/utils/msg.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | use std 4 | use string 5 | use asmer.asm 6 | 7 | func debug(args...){ 8 | if asm.trace == 1 { 9 | s = string.stringfmt(args) 10 | s = s.putc('\n'.(i8)) 11 | fmt.fputs(s,std.STDOUT) 12 | } 13 | } 14 | func printf(args...){ 15 | // if debug_mode == 1 { 16 | s = string.stringfmt(args) 17 | fmt.fputs(s,std.STDOUT) 18 | // } 19 | } 20 | func print_green(red){ 21 | return " \033[32m" + red + "\033[0m" 22 | } 23 | func print_red(red){ 24 | return " \033[31m" + red + "\033[0m" 25 | } 26 | func msg(progress,str) { 27 | if progress == 0 { 28 | fmt.println(print_green(str)) 29 | }else { 30 | prefix = "[ " + progress + "%]" 31 | fmt.println(prefix + print_green(str)) 32 | } 33 | } 34 | func smsg(pre,str) { 35 | fmt.println(pre + print_green(str)) 36 | } 37 | func error(str) { 38 | fmt.println(print_red(str)) 39 | os.exit(-1) 40 | } 41 | func errorf(args...) { 42 | f = fmt.sprintf(args) 43 | fmt.println(print_red(f)) 44 | os.exit(-1) 45 | } -------------------------------------------------------------------------------- /assets/compiler_compiler.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/assets/compiler_compiler.png -------------------------------------------------------------------------------- /assets/compiler_helloworld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/assets/compiler_helloworld.png -------------------------------------------------------------------------------- /assets/linker_helloworld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/assets/linker_helloworld.png -------------------------------------------------------------------------------- /assets/tulang.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/assets/tulang.gif -------------------------------------------------------------------------------- /compiler/README.md: -------------------------------------------------------------------------------- 1 | 基于tu-lang自举 compiler 编译器 2 | 3 | # 环境准备 4 | ``` 5 | > cd tu-lang 6 | > make install-bin 7 | ``` 8 | # 编译测试 9 | ``` 10 | > vim hello.tu 11 | use fmt 12 | func main(){ 13 | fmt.println("hello tulang") 14 | } 15 | > tu run hello.tu 16 | ``` 17 | ![image](../assets/compiler_helloworld.png#w50) 18 | 19 | # 自举测试 20 | ``` 21 | > cd tu-lang 22 | > tu build tulang.tu 23 | ``` 24 | ![image](../assets/compiler_compiler.png#w50) -------------------------------------------------------------------------------- /compiler/ast/context.tu: -------------------------------------------------------------------------------- 1 | use compiler.utils 2 | use std 3 | 4 | class Ctx { 5 | level = 0 6 | vars = {} // map{string:VarExpr} 7 | isFuncArg = {} // map{string,bool} 8 | 9 | cur_funcname = "" 10 | end_str = "" 11 | start_str = "" 12 | continue_str = "" 13 | point = 0 14 | 15 | //for future async 16 | breakto continueto 17 | } 18 | 19 | Ctx::createVar(varname,ident) 20 | { 21 | this.vars[varname] = ident 22 | } 23 | Ctx::getVar(varname) 24 | { 25 | if this.vars[varname] != null { 26 | return this.vars[varname] 27 | } 28 | return null 29 | } 30 | 31 | //compile phase 32 | Context::getLocalVar(varname) 33 | { 34 | var = GF().FindLocalVar(varname) 35 | if var != null return var 36 | 37 | var = GP().getGlobalVar("",varname) 38 | if var != null return var 39 | 40 | if GF().fntype == ClosureFunc && GF().parent != null { 41 | var = GF().parent.FindLocalVar(varname) 42 | if var != null 43 | return var 44 | } 45 | 46 | utils.debug( 47 | "variable:%s not define in local or params or global filename:%s" 48 | ,varname,GF().parser.filename 49 | ) 50 | return null 51 | } -------------------------------------------------------------------------------- /compiler/ast/define.tu: -------------------------------------------------------------------------------- 1 | use compiler.compile 2 | 3 | class ConfigOpts { 4 | base_static = false 5 | } 6 | 7 | # auto increment closure id 8 | closureidx = 0 9 | # auto increment count 10 | labelidx = 0 11 | 12 | enum { 13 | Var_Obj_Member , 14 | Var_Extern_Global, Var_Local_Global,Var_Local_Mem_Global, 15 | Var_Global_Extern, Var_Global_Local, 16 | Var_Local, 17 | Var_Func, 18 | Var_Global_Local_Static_Field, 19 | Var_Local_Static, 20 | Var_Local_Static_Field, 21 | Var_Global_Extern_Static, 22 | } 23 | 24 | func GP(){ 25 | return compile.currentParser 26 | } 27 | func GF(){ 28 | return compile.currentFunc 29 | } 30 | fn isfloattk(tk){ 31 | if(tk == F32 || tk == F64) return true 32 | return false 33 | } 34 | 35 | fn cfg_static(){ 36 | if GP().cfgs.base_static return true 37 | 38 | if GP().pkg.cfgs.base_static return true 39 | 40 | return false 41 | } 42 | 43 | -------------------------------------------------------------------------------- /compiler/ast/token.tu: -------------------------------------------------------------------------------- 1 | 2 | Null = 0 3 | Int = 1 4 | Double = 2 5 | String = 3 6 | Bool = 4 7 | Char = 5 8 | Array = 6 9 | Map = 7 10 | Object = 8 11 | Func = 9 12 | 13 | 14 | TRUE = 1 15 | FALSE = 0 16 | OK = 0 17 | ERROR = -1 18 | 19 | 20 | enum { 21 | ILLEGAL, END, 22 | INT, STRING, FLOAT, CHAR, 23 | I8,U8,I16,U16,I32,U32,I64,U64,F32,F64, 24 | BITAND, BITOR,BITXOR,BITNOT , 25 | SHL, SHR,LOGAND, LOGOR, LOGNOT, 26 | EQ,NE,GT,GE,LT,LE, 27 | ADD, SUB, MUL, DIV, MOD, 28 | ASSIGN,ADD_ASSIGN,SUB_ASSIGN,MUL_ASSIGN,BITXOR_ASSIGN,DIV_ASSIGN,MOD_ASSIGN,SHL_ASSIGN,SHR_ASSIGN,BITAND_ASSIGN,BITOR_ASSIGN, 29 | COMMA,LPAREN,RPAREN,LBRACE,RBRACE,LBRACKET,RBRACKET,DOT,COLON,SEMICOLON, 30 | VAR,IF,ELSE,BOOL,WHILE,LOOP,FOR,EMPTY,FUNC,RETURN,BREAK,CONTINUE,NEW, 31 | EXTERN,USE,CO,CLASS,DELREF,EXTRA,MEM,MATCH,ENUM,BUILTIN,GOTO,CFG,ASYNC,AWAIT, 32 | } 33 | 34 | type_id = 9 35 | 36 | fn getTypeId(){ 37 | ret = type_id 38 | type_id += 1 39 | return ret 40 | } 41 | 42 | fn resetTypeId(){ 43 | type_id = 9 44 | } -------------------------------------------------------------------------------- /compiler/bin/amd64_linux_tuc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/compiler/bin/amd64_linux_tuc -------------------------------------------------------------------------------- /compiler/compile/define.tu: -------------------------------------------------------------------------------- 1 | use compiler.utils 2 | 3 | // dynamic: args registers 4 | args8 = ["%dil" , "%sil" , "%dl" , "%cl" , "%r8b" , "%r9b"] # 8bit 5 | args16 = ["%di" , "%si" , "%dx" , "%cx" , "%r8w" , "%r9w"] # 16bit 6 | args32 = ["%edi" , "%esi" , "%edx" , "%ecx" , "%r8d" , "%r9d"] # 32bit 7 | args64 = ["%rdi" , "%rsi" , "%rdx" , "%rcx" , "%r8" , "%r9" ] # 64bit 8 | argm64 = ["%rax" , "%rbx" , "%rcx" , "%rdi" , "%rsi" , "%r8" , "%r9"] # 64bit 9 | 10 | GP_MAX = 6 11 | FP_MAX = 8 12 | -------------------------------------------------------------------------------- /compiler/gen/common.tu: -------------------------------------------------------------------------------- 1 | use compiler.ast 2 | use compiler.compile 3 | 4 | Null = null 5 | EmptyStr = "" 6 | 7 | typeids = [] 8 | // I8 = int(ast.I8) U8 = int(ast.U8) 9 | // I16 = int(ast.I16) U16 = int(ast.U16) 10 | // I32 = int(ast.I32) U32 = int(ast.U32) 11 | // I64 = int(ast.I64) U64 = int(ast.U64) 12 | // typesize = { 13 | // I8 : 1 , I16 : 2 , I32 : 4 , I64 : 8, 14 | // U8 : 1 , U16 : 2 , U32 : 4 , U64 : 8 15 | // } 16 | func init(){ 17 | typeids = { 18 | "null":ast.Null , "int" : ast.Int , "double" : ast.Double, "string" : ast.String, 19 | "bool":ast.Bool , "char": ast.Char, "array" : ast.Array , "map" : ast.Map, 20 | "function": ast.Func 21 | } 22 | } 23 | func exprIsMtype(cond,ctx){ 24 | ismtype = false 25 | match type(cond) { 26 | type(StructMemberExpr) | type(DelRefExpr) | type(AddrExpr): { 27 | ismtype = true 28 | } 29 | type(VarExpr) | type(BinaryExpr): { 30 | ismtype = cond.isMemtype(ctx) 31 | } 32 | type(ChainExpr): ismtype = cond.ismem(ctx) 33 | type(BuiltinFuncExpr) : ismtype = cond.isMem(ctx) 34 | type(IndexExpr): { 35 | i = cond 36 | if i.tyassert != null { 37 | ismtype = true 38 | }else{ 39 | var = new VarExpr(i.varname,i.line,i.column) 40 | var.package = i.package 41 | ismtype = var.isMemtype(ctx) 42 | } 43 | } 44 | type(MemberExpr) : ismtype = cond.ismem(ctx) 45 | } 46 | return ismtype 47 | } 48 | func GP(){ 49 | return compile.currentParser 50 | } 51 | func GF(){ 52 | return compile.currentFunc 53 | } -------------------------------------------------------------------------------- /compiler/gen/expr_compile.tu: -------------------------------------------------------------------------------- 1 | use compiler.ast 2 | use string 3 | 4 | BinaryExpr::expr_compile(){ 5 | utils.debugf("gen.BinaryExpr::expr_compile()") 6 | left = 0 7 | right = 0 8 | match type(this.lhs) { 9 | type(BinaryExpr) : left = this.lhs.expr_compile() 10 | type(VarExpr) : left = this.lhs.expr_compile() 11 | type(IntExpr) : left = string.tonumber(this.lhs.lit) 12 | _ : this.check(false,"unsupport type in Binaryy::expr_compile ") 13 | } 14 | match type(this.rhs) { 15 | type(BinaryExpr) : right = this.rhs.expr_compile() 16 | type(VarExpr) : right = this.rhs.expr_compile() 17 | type(IntExpr) : right = string.tonumber(this.rhs.lit) 18 | _ : this.check(false,"unsupport type in Binaryy::expr_compile ") 19 | } 20 | match this.opt { 21 | ast.ADD : return left + right 22 | ast.SUB: return left - right 23 | ast.MUL: return left * right 24 | ast.DIV: return left / right 25 | ast.SHL: return left << right 26 | _: this.check(false,"only support +-*/<< in expr_compile") 27 | } 28 | } 29 | VarExpr::expr_compile(){ 30 | realvar = GP().getGlobalVar(this.package,this.varname) 31 | if realvar == null { 32 | this.check(false,"macro var not exist in mem member define") 33 | } 34 | return string.tonumber(realvar.ivalue) 35 | } 36 | 37 | fn toBinExpr(expr){ 38 | be = new BinaryExpr(expr.line,expr.column) 39 | be.lhs = expr 40 | be.opt = ast.GT 41 | i = new IntExpr(expr.line,expr.column) 42 | i.lit = "0" 43 | be.rhs = i 44 | return be 45 | } -------------------------------------------------------------------------------- /compiler/gen/future.tu: -------------------------------------------------------------------------------- 1 | IfStmt::checkawait(){ 2 | for it : this.cases { 3 | if it.cond.hasawait { 4 | this.hasawait = true 5 | break 6 | } 7 | if it.block.hasawait { 8 | this.hasawait = true 9 | break 10 | } 11 | } 12 | if this.elseCase != null { 13 | if this.elseCase.block.hasawait { 14 | this.hasawait = true 15 | } 16 | } 17 | } 18 | 19 | ChainExpr::checkawait(){ 20 | if this.first != null && this.first.hasawait { 21 | this.hasawait = true 22 | return true 23 | } 24 | if this.last != null && this.last.hasawait { 25 | this.hasawait = true 26 | return true 27 | } 28 | for it : this.fields { 29 | if it.hasawait { 30 | this.hasawait = true 31 | return true 32 | } 33 | } 34 | } 35 | 36 | BinaryExpr::checkawait(){ 37 | if this.lhs.hasawait { 38 | this.hasawait = true 39 | } 40 | if this.rhs != null && this.rhs.hasawait { 41 | this.hasawait = true 42 | } 43 | } 44 | 45 | AssignExpr::checkawait(){ 46 | if this.lhs.hasawait { 47 | this.hasawait = true 48 | } 49 | if this.rhs != null && this.rhs.hasawait { 50 | this.hasawait = true 51 | } 52 | } -------------------------------------------------------------------------------- /compiler/gen/kv_array.tu: -------------------------------------------------------------------------------- 1 | use compiler.utils 2 | use compiler.ast 3 | use compiler.internal 4 | use compiler.compile 5 | use compiler.parser 6 | use compiler.parser.package 7 | 8 | class ArrayExpr : ast.Ast { 9 | lit = [] 10 | func init(line,column){ 11 | super.init(line,column) 12 | } 13 | } 14 | 15 | // @param ctx [Context] 16 | // @return Expression 17 | ArrayExpr::compile(ctx,load){ 18 | utils.debug("gen.ArrayExpr::compile()") 19 | this.record() 20 | //new Array & push array 21 | internal.newobject(ast.Array, 0) 22 | 23 | compile.Push() 24 | 25 | for(element: this.lit){ 26 | compile.writeln(" push (%%rsp)") 27 | //new element & push element 28 | element.compile(ctx,true) 29 | compile.Push() 30 | 31 | internal.arr_pushone() 32 | } 33 | 34 | //pop array 35 | compile.Pop("%rax") 36 | 37 | return null 38 | } 39 | ArrayExpr::toString() { 40 | str = "ArrayExpr(elements=[" 41 | for e : this.lit { 42 | str += e.toString() 43 | } 44 | str += "])" 45 | return str 46 | } 47 | 48 | -------------------------------------------------------------------------------- /compiler/gen/kv_map.tu: -------------------------------------------------------------------------------- 1 | use compiler.ast 2 | use compiler.compile 3 | use compiler.utils 4 | 5 | 6 | class MapExpr : ast.Ast { 7 | lit = [] 8 | func init(line,column){ 9 | super.init(line,column) 10 | } 11 | } 12 | MapExpr::toString() { 13 | str = "MapExpr(elements={" 14 | if std.len(this.lit) != 0 { 15 | for (e : this.lit) { 16 | str += e.toString() 17 | } 18 | } 19 | str += "})" 20 | return str 21 | } 22 | MapExpr::compile(ctx,load){ 23 | this.record() 24 | utils.debug("gen.MapExpr::compile() ") 25 | 26 | internal.newobject(ast.Map, 0) 27 | compile.Push() 28 | 29 | for(element: this.lit){ 30 | compile.writeln(" push (%%rsp)") 31 | element.compile(ctx,true) 32 | internal.kv_update() 33 | } 34 | 35 | compile.Pop("%rax") 36 | return null 37 | } -------------------------------------------------------------------------------- /compiler/gen/type.tu: -------------------------------------------------------------------------------- 1 | use compiler.ast 2 | use compiler.compile 3 | use std 4 | use fmt 5 | use compiler.parser.package 6 | 7 | class TypeAssertExpr : ast.Ast { 8 | func init(line,column){ 9 | super.init(line,column) 10 | } 11 | pkgname = "" 12 | name = "" 13 | } 14 | 15 | TypeAssertExpr::toString() { 16 | return fmt.sprintf("TypeAssertExpr(%s.%s)" 17 | this.pkgname,this.name 18 | ) 19 | } 20 | 21 | TypeAssertExpr::compile(ctx,load){ 22 | return null 23 | 24 | } 25 | 26 | TypeAssertExpr::getStruct(){ 27 | name = this.name 28 | utils.debugf("gen.TypeAssertExpr::getStruct() pkgname:%s name:%s\n",this.pkgname,name) 29 | s = null 30 | pkg = GP().pkg.getPackage(this.pkgname) 31 | if pkg == null { 32 | this.check(false,"type assert: mem package not exist:" + this.pkgname) 33 | } 34 | s = pkg.getStruct(name) 35 | if s == null { 36 | this.check(false,"mem type not exist :" + name) 37 | } 38 | return s 39 | } -------------------------------------------------------------------------------- /compiler/parser/data.tu: -------------------------------------------------------------------------------- 1 | use std 2 | use fmt 3 | use compiler.parser.package 4 | use compiler.compile 5 | 6 | Parser::addFunc(name, f) 7 | { 8 | if name == "init" { 9 | if compile.phase == compile.GlobalPhase { 10 | return true 11 | } 12 | f.name = f.name + "." + this.pkg.geninitid() 13 | name = f.name 14 | this.pkg.inits[] = f 15 | } 16 | if f.fntype == ast.ExternFunc this.extern_funcs[name] = f 17 | else this.funcs[name] = f 18 | } 19 | 20 | Parser::hasFunc(name, is_extern) 21 | { 22 | if name == "init" { return false} 23 | 24 | if is_extern return std.exist(name,this.extern_funcs) 25 | else return std.exist(name,this.funcs) 26 | } 27 | 28 | Parser::getFunc(name, is_extern) 29 | { 30 | if is_extern { 31 | if std.exist(name,this.extern_funcs) 32 | return this.extern_funcs[name] 33 | }else { 34 | if std.exist(name,this.funcs) 35 | return this.funcs[name] 36 | } 37 | return null 38 | } 39 | 40 | Parser::getGvar(name){ 41 | if std.exist(name,this.gvars) 42 | return this.gvars[name] 43 | return null 44 | } 45 | Parser::getGlobalVar(pkgname ,varname){ 46 | pkg = this.pkg.getPackage(pkgname) 47 | if (pkg == null){ 48 | return null 49 | } 50 | return pkg.getGlobalVar(varname) 51 | } 52 | Parser::getGlobalFunc(pkgname ,varname,is_extern){ 53 | p = this.pkg.getPackage(pkgname) 54 | if p == null return null 55 | 56 | return p.getFunc(varname,is_extern) 57 | } -------------------------------------------------------------------------------- /compiler/parser/package/genvars.tu: -------------------------------------------------------------------------------- 1 | use compiler.parser 2 | use compiler.internal 3 | use compiler.ast 4 | use std 5 | 6 | Package::InsertInitVarExpression(expr){ 7 | funcname = this.getInitVarsFuncName() 8 | f = this.getFunc(funcname,false) 9 | if f == null { 10 | p = std.head(this.parsers) 11 | f = new ast.Function() 12 | //set parser 13 | f.name = funcname 14 | f.block = new gen.BlockStmt() 15 | f.parser = p 16 | f.package = this 17 | p.addFunc(f.name,f) 18 | } 19 | f.InsertExpression(expr) 20 | } 21 | 22 | Package::genvarsinit(){ 23 | mf = this.getFunc("init.0",false) 24 | if mf == null { 25 | p = std.head(this.parsers) 26 | mf = this.InsertInitFunc(p) 27 | } 28 | inits = [] 29 | for(pkg : packages){ 30 | if(pkg.getFunc(this.getInitVarsFuncName(),false)){ 31 | // mf.InsertFuncall(pkg.getFullName(),this.getInitVarsFuncName()) 32 | inits[] = [pkg.getFullName(),this.getInitVarsFuncName()] 33 | } 34 | } 35 | mf.InsertFuncallHead(inits) 36 | } 37 | -------------------------------------------------------------------------------- /compiler/parser/package/package2.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use compiler.parser.scanner 3 | use compiler.compile 4 | 5 | Package::parse2() 6 | { 7 | for(cls : this.classes){ 8 | cls.type_id = 0 9 | cls.funcs = [] 10 | } 11 | 12 | for st : this.structs { 13 | st.funcs = {} 14 | } 15 | 16 | this.initid = 0 17 | this.inits = [] 18 | } 19 | 20 | Package::parse3() 21 | { 22 | for(p : this.parsers){ 23 | reader = p.scanner 24 | reader.reset() 25 | 26 | compile.currentParser = p 27 | p.parse() 28 | compile.currentParser = null 29 | } 30 | } -------------------------------------------------------------------------------- /compiler/parser/package/pub.tu: -------------------------------------------------------------------------------- 1 | use compiler.compile 2 | use std 3 | 4 | packages = {} // map{name: Package} 5 | 6 | Package::add_string(str){ 7 | if this.gstrs[str.lit] != null 8 | return true 9 | 10 | this.gstrs[str.lit] = str 11 | } 12 | 13 | Package::get_string(str){ 14 | return this.gstrs[str.lit] 15 | } 16 | 17 | func getStruct(packagename,name) { 18 | pkgname = packagename 19 | if GP().pkg.imports[packagename] != null { 20 | pkgname = GP().pkg.imports[packagename] 21 | } 22 | if pkgname == "" || pkgname == null 23 | pkgname = GP().getpkgname() 24 | 25 | if packages[pkgname] == null { 26 | return null 27 | } 28 | pkg = packages[pkgname] 29 | return pkg.getStruct(name) 30 | } 31 | 32 | fn getClass(package,name) 33 | { 34 | pkgname = package 35 | if GP().pkg.imports[package] != null { 36 | pkgname = GP().pkg.imports[package] 37 | } 38 | if pkgname == "" 39 | pkgname = GP().getpkgname() 40 | 41 | if packages[pkgname] == null { 42 | return null 43 | } 44 | pkg = packages[pkgname] 45 | return pkg.getClass(name) 46 | } -------------------------------------------------------------------------------- /compiler/parser/scanner/define.tu: -------------------------------------------------------------------------------- 1 | 2 | use compiler.ast 3 | 4 | EOF = -1 5 | keywords = { 6 | "i8" : ast.I8, "i16" : ast.I16, "i32" : ast.I32, "i64" : ast.I64, 7 | "u8" : ast.U8, "u16" : ast.U16, "u32" : ast.U32, "u64" : ast.U64, 8 | "f32" : ast.F32, "f64" : ast.F64, 9 | "if" : ast.IF, "else" : ast.ELSE, 10 | "while" : ast.WHILE, "loop" : ast.LOOP, "for" : ast.FOR, "false" : ast.BOOL, 11 | "true" : ast.BOOL, "null" : ast.EMPTY, "func" : ast.FUNC, "fn" : ast.FUNC, 12 | "return": ast.RETURN, "break" : ast.BREAK, "continue": ast.CONTINUE, 13 | "use" : ast.USE, "extern": ast.EXTERN, "class" : ast.CLASS, 14 | "new" : ast.NEW, "co" : ast.CO, "mem" : ast.MEM, "await": ast.AWAIT, "async": ast.ASYNC, 15 | "match" : ast.MATCH , "enum" : ast.ENUM, "goto" : ast.GOTO, "cfg" : ast.CFG, 16 | } 17 | builtins = { 18 | "int" : true , "sizeof" : true , "type" : true, "float" : true, 19 | "inter_get_bp" : true, 20 | } 21 | specs = { 22 | "\\n" : 10.(i8), 23 | "\\\\" : 92.(i8), 24 | "\\t" : 9.(i8), 25 | "\\\'" : 39.(i8), 26 | "\\\"" : 34.(i8), 27 | "\\b" : 8.(i8), 28 | "\\r" : 13.(i8), 29 | "\\f" : 12.(i8), 30 | "\\0" : 0.(i8), 31 | "\\r" : 13.(i), 32 | "\\v" : 11.(i8) 33 | } -------------------------------------------------------------------------------- /compiler/parser/static.tu: -------------------------------------------------------------------------------- 1 | use string 2 | use std 3 | use compiler.ast 4 | use compiler.parser.scanner 5 | use compiler.utils 6 | use compiler.gen 7 | 8 | Parser::parseTypeAssert(lastdot){ 9 | utils.debugf("Parser::parseTypeAssert() lastdot:%d",lastdot) 10 | reader = this.scanner 11 | ta = null 12 | if reader.curToken == ast.LPAREN { 13 | ta = new gen.TypeAssertExpr(this.line,this.column) 14 | reader.scan()//eat ( 15 | this.check(reader.curToken == ast.VAR || this.isbase(),"a.(?) must be var") 16 | ta.name = reader.curLex.dyn() 17 | reader.scan()//eat first var 18 | if reader.curToken == ast.DOT { 19 | reader.scan()//eat . 20 | this.check(reader.curToken == ast.VAR || this.isbase(),"a.(,?) must be var") 21 | ta.pkgname = ta.name 22 | ta.name = reader.curLex.dyn() 23 | reader.scan()//eat last 24 | } 25 | this.check(reader.curToken == ast.RPAREN,"a.(,) wrong") 26 | reader.scan()//eat ) 27 | if lastdot { 28 | this.check(reader.curToken == ast.DOT,"next typeassertexpr can't be nothing")//must be dot 29 | reader.scan()//eat. 30 | } 31 | } 32 | return ta 33 | } 34 | 35 | Parser::parseVarStack(expr){ 36 | this.expect(ast.COLON,"sould be : in stack var declare") 37 | reader = this.scanner 38 | reader.scan() 39 | expr.stack = true 40 | expr.stacksize = 1 41 | if reader.curToken != ast.GT { 42 | this.expect(ast.INT,"must be int (var /dev/null 2>&1; then 14 | rm -rf $1 15 | fi 16 | } 17 | check(){ 18 | if [ "$?" != 0 ]; then 19 | # actual=`./a.out` 20 | # if [ "$?" != 0 ]; then 21 | failed "exec failed" 22 | # fi 23 | # rm ./a.out 24 | fi 25 | 26 | } 27 | 28 | assert(){ 29 | log "[compile] tu -s compiler/$1 " 30 | tu -s "$1" 31 | check 32 | tu -c . 33 | tu -o . -o /usr/local/lib/colib/ 34 | check 35 | chmod 777 a.out 36 | ./a.out 37 | check 38 | clean "a.out" 39 | clean "*.s" 40 | clean "*.o" 41 | echo "exec done..." 42 | 43 | return 44 | # failed "[compile] $input failed" 45 | } 46 | clean "./*.s" 47 | clean "./*.o" 48 | assert compiler/test_scanner.tu 49 | clean "./*.s" 50 | clean "./*.o" 51 | assert compiler/main.tu 52 | clean "./*.s" 53 | clean "./*.o" 54 | assert compiler/test_scaner2.tu 55 | clean "./*.s" 56 | clean "./*.o" 57 | assert compiler/test_static_token.tu 58 | clean "./*.s" 59 | clean "./*.o" 60 | log "all passing...." 61 | -------------------------------------------------------------------------------- /compiler/test/case: -------------------------------------------------------------------------------- 1 | 0x777777 100 # INT INT 2 | "string" # STRING 3 | #fix me 100.20 FLOAT 4 | 101 # INT 5 | 'a' '\n' # CHAR 6 | & | ~ # BITAND BITOR BITNOT 7 | << >> && || ! # SHL SHR LOGAND LOGOR LOGNOT 8 | == != > >= < <= # EQ NE GT GE LT LE 9 | + - * / % # ADD SUB MUL DIV MOD 10 | = += -= *= /= # ASSIGN ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN 11 | %= <<= >>= # MOD_ASSIGN SHL_ASSIGN SHR_ASSIGN 12 | &= |= # BITAND_ASSIGN BITOR_ASSIGN 13 | , ( ) { } # COMMA LPAREN RPAREN LBRACE RBRACE 14 | [] . : ; # LBRACKET RBRACKET DOT COLON SEMICOLON 15 | obj if else # VAR IF ELSE 16 | true false # bool 17 | while for # WHILE FOR 18 | null func # EMPTY FUNC 19 | return break # RETURN BREAK 20 | continue new # CONTINUE NEW 21 | extern use co # EXTERN USE CO 22 | class *obj # CLSS DELREF VAR 23 | //: # EXTRA 24 | mem match enum # MEM MATCH ENUM 25 | i8 i16 i32 i64 u8 u16 u32 u64 # i8 - u64 26 | goto # GOTO 27 | int( # BUILTIN LPAREN 28 | sizeof( # BUILTIN LPAREN 29 | type( # BUILTIN LPAREN 30 | 31 | -------------------------------------------------------------------------------- /compiler/test/token.tu: -------------------------------------------------------------------------------- 1 | use compiler.ast 2 | 3 | token = [ 4 | //FIXME: int(ast.INT), int(ast.INT), int(ast.STRING) ,int(ast.FLOAT), int(ast.CHAR),int(ast.CHAR), 5 | int(ast.INT), int(ast.INT), int(ast.STRING) ,int(ast.INT), int(ast.CHAR),int(ast.CHAR), 6 | int(ast.BITAND),int(ast.BITOR),int(ast.BITNOT), 7 | int(ast.SHL), int(ast.SHR), int(ast.LOGAND), int(ast.LOGOR), int(ast.LOGNOT), 8 | int(ast.EQ), int(ast.NE), int(ast.GT), int(ast.GE), int(ast.LT), int(ast.LE), 9 | int(ast.ADD), int(ast.SUB), int(ast.MUL), int(ast.DIV), int(ast.MOD), 10 | int(ast.ASSIGN), int(ast.ADD_ASSIGN), int(ast.SUB_ASSIGN), int(ast.MUL_ASSIGN), 11 | int(ast.DIV_ASSIGN), int(ast.MOD_ASSIGN), int(ast.SHL_ASSIGN), int(ast.SHR_ASSIGN), 12 | int(ast.BITAND_ASSIGN), int(ast.BITOR_ASSIGN), 13 | int(ast.COMMA), int(ast.LPAREN), int(ast.RPAREN), int(ast.LBRACE), int(ast.RBRACE), 14 | int(ast.LBRACKET), int(ast.RBRACKET), int(ast.DOT), int(ast.COLON), int(ast.SEMICOLON), 15 | int(ast.VAR), int(ast.IF), int(ast.ELSE), 16 | int(ast.BOOL), int(ast.BOOL), 17 | int(ast.WHILE), int(ast.FOR), 18 | int(ast.EMPTY), int(ast.FUNC), 19 | int(ast.RETURN), int(ast.BREAK), 20 | int(ast.CONTINUE), int(ast.NEW), 21 | int(ast.EXTERN), int(ast.USE), int(ast.CO), 22 | int(ast.CLASS), int(ast.DELREF),int(ast.VAR), 23 | int(ast.EXTRA), 24 | int(ast.MEM), int(ast.MATCH), int(ast.ENUM), 25 | int(ast.I8), int(ast.I16), int(ast.I32), int(ast.I64), 26 | int(ast.U8), int(ast.U16), int(ast.U32), int(ast.U64), 27 | int(ast.GOTO), 28 | int(ast.BUILTIN), int(ast.LPAREN), 29 | int(ast.BUILTIN), int(ast.LPAREN), 30 | int(ast.BUILTIN), int(ast.LPAREN), int(ast.END) 31 | ] 32 | 33 | -------------------------------------------------------------------------------- /compiler/test_scaner2.tu: -------------------------------------------------------------------------------- 1 | use compiler.parser.scanner 2 | use compiler.parser.package 3 | use compiler.parser 4 | use compiler.ast 5 | 6 | use string 7 | use fmt 8 | use std 9 | use os 10 | class Empty1{} 11 | func scan(abpath){ 12 | fd = std.opendir(abpath) 13 | if !fd { 14 | fmt.printf("file|dir not exist %s",abpath) 15 | } 16 | while true { 17 | file = fd.readdir() 18 | if !file { 19 | break 20 | } 21 | if file.isDir() && file.name != "." && file.name != ".." scan(file.path) 22 | if !file.isFile() continue 23 | filepath = file.path 24 | if string.sub(filepath,std.len(filepath) - 3) == ".tu" { 25 | s = new scanner.ScannerStatic(filepath,new Empty1()) 26 | fmt.println(filepath) 27 | loop { 28 | if s.scan() == ast.END { 29 | break 30 | } 31 | // fmt.println(s.curLex.dyn()) 32 | } 33 | } 34 | } 35 | 36 | } 37 | 38 | func main(){ 39 | scan("./compiler") 40 | } -------------------------------------------------------------------------------- /compiler/test_scanner.tu: -------------------------------------------------------------------------------- 1 | use compiler.parser 2 | use compiler.test 3 | use os 4 | use compiler.ast 5 | use compiler.parser.package 6 | use compiler.parser.scanner 7 | 8 | 9 | func test(){ 10 | mpkg = new package.Package("main","main",false) 11 | p = new parser.Parser("./compiler/test/case",mpkg) 12 | reader = p.scanner 13 | 14 | for v : test.token { 15 | if *v != reader.scan(){ 16 | os.dief( 17 | "token err: expect %s cur:%s lex:%s", 18 | ast.getTokenString(*v) , 19 | ast.getTokenString(reader.curToken), 20 | reader.curLex.dyn() 21 | ) 22 | } 23 | fmt.printf("pass token:%s type:%s\n",reader.curLex.dyn(),ast.getTokenString(reader.curToken)) 24 | } 25 | 26 | } 27 | func main(){ 28 | test() 29 | } -------------------------------------------------------------------------------- /compiler/test_static_token.tu: -------------------------------------------------------------------------------- 1 | use compiler.parser 2 | use compiler.test 3 | use os 4 | use compiler.ast 5 | use compiler.parser.package 6 | use compiler.parser.scanner 7 | 8 | class Empty{} 9 | func test(){ 10 | filecase = "./compiler/test/case" 11 | reader = new scanner.ScannerStatic(filecase,new Empty()) 12 | 13 | for v : test.token { 14 | if *v != reader.scan(){ 15 | os.dief( 16 | "token err: expect %s cur:%s lex:%s", 17 | ast.getTokenString(*v) , 18 | ast.getTokenString(reader.curToken), 19 | reader.curLex.dyn() 20 | ) 21 | } 22 | fmt.printf("pass token:%s type:%s\n",reader.curLex.dyn(),ast.getTokenString(reader.curToken)) 23 | } 24 | 25 | } 26 | func main(){ 27 | test() 28 | } -------------------------------------------------------------------------------- /compiler/utils/fmt.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | use fmt 3 | use std 4 | 5 | func println_green(count,args...){ 6 | total = count.data 7 | var = null 8 | 9 | p = &args 10 | stack = 5 11 | for (i = 0 ; i < total ; i += 1){ 12 | var = *p 13 | if stack < 1 p += 8 14 | else p -= 8 15 | if stack == 1 { 16 | p = &args 17 | p += 32 18 | } 19 | stack -= 1 20 | 21 | if var == null { 22 | fmt.vfprintf(std.STDOUT,*"\033[32mnull\033[0m\n") 23 | continue 24 | } 25 | match var.type { 26 | runtime.Null: fmt.vfprintf(std.STDOUT,*"\033[32mnull\033[0m") 27 | runtime.Int: fmt.vfprintf(std.STDOUT,*"\033[32m%d\033[0m",var.data) 28 | runtime.Bool: { 29 | if var.data == 0 fmt.vfprintf(std.STDOUT,*"\033[32mfalse\033[0m") 30 | else fmt.vfprintf(std.STDOUT,*"\033[32mtrue\033[0m") 31 | } 32 | runtime.String: fmt.vfprintf(std.STDOUT,*"\033[32m%s\033[0m",var.data) 33 | runtime.Char: fmt.vfprintf(std.STDOUT,*"\033[32m%d\033[0m",var.data) 34 | runtime.Array: fmt.vfprintf(std.STDOUT,*"\033[32m%s\033[0m",runtime.arr_tostring(var)) 35 | runtime.Map: fmt.vfprintf(std.STDOUT,*"\033[32mmap:%p\033[0m",var) 36 | runtime.Object: fmt.vfprintf(std.STDOUT,*"\033[32mobject:%p\033[0m",var) 37 | _: fmt.vfprintf(std.STDOUT,*"\033[32mpointer:%p\033[0m",var) 38 | } 39 | fmt.vfprintf(std.STDOUT,*"\t") 40 | } 41 | fmt.vfprintf(std.STDOUT,*"\n") 42 | } -------------------------------------------------------------------------------- /compiler/utils/init.tu: -------------------------------------------------------------------------------- 1 | use os 2 | use std 3 | 4 | envs # map{string:string,} 5 | 6 | func init(){ 7 | m = {} 8 | for (k,v : os.envs() ){ 9 | arr = string.split(v,"=") 10 | m[arr[0]] = arr[1] 11 | } 12 | envs = m 13 | } 14 | 15 | -------------------------------------------------------------------------------- /compiler/utils/sort.tu: -------------------------------------------------------------------------------- 1 | use std 2 | fn quick_sort(arr,cmper){ 3 | if std.len(arr) < 1 return [] 4 | first = arr[0] 5 | left = [] 6 | right = [] 7 | for i = 1 ; i < std.len(arr) ; i += 1 { 8 | if cmper(arr[i] , first) 9 | left[] = arr[i] 10 | else 11 | right[] = arr[i] 12 | } 13 | 14 | left = quick_sort(left,cmper) 15 | right = quick_sort(right,cmper) 16 | 17 | left[] = first 18 | return _qsort_merge(left,right) 19 | } 20 | 21 | fn _qsort_merge(arr1,arr2){ 22 | arr = [] 23 | std.merge(arr,arr1) 24 | std.merge(arr,arr2) 25 | return arr 26 | } 27 | -------------------------------------------------------------------------------- /devfile: -------------------------------------------------------------------------------- 1 | prefix = /usr/local 2 | 3 | .PHONY: clean 4 | clean: 5 | @$(CLEAN_ALL); clean_all 6 | @echo "clean all fininshed" 7 | 8 | test_memory: 9 | sh tests_compiler.sh memory 10 | sh tests_asmer.sh memory 11 | sh tests_linker.sh memory 12 | 13 | check: install test 14 | 15 | cases = mixed class common datastruct internalpkg memory native operator runtime statement 16 | 17 | test_compiler: 18 | tuc run tulang.tu 19 | 20 | test: test_compiler $(cases) 21 | @echo "all test passed" 22 | 23 | #make test -j9 24 | tests: $(cases) 25 | @echo "all test cases passed" 26 | 27 | %: ./tests/% 28 | @sh dev.sh $@ ; 29 | 30 | -------------------------------------------------------------------------------- /library/fmt/assert.tu: -------------------------------------------------------------------------------- 1 | 2 | use os 3 | 4 | func assert(a,b,str){ 5 | check = str 6 | if a != b { 7 | expr = "assert: " + a + " != " + b 8 | println(expr) 9 | if int(check) != null { 10 | msg = "warmsg: " + str 11 | println(msg) 12 | } 13 | os.exit(-1) 14 | } 15 | } -------------------------------------------------------------------------------- /library/os/args.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | 3 | # start at userspace init process 4 | func argc(){ 5 | return runtime.ori_argc 6 | } 7 | func argv(){ 8 | return runtime.ori_argv 9 | } 10 | func envs(){ 11 | return runtime.ori_envs 12 | } -------------------------------------------------------------------------------- /library/os/exit.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use std 4 | use runtime 5 | use string 6 | use runtime.debug 7 | 8 | debug_stack = 5 9 | 10 | func set_stack(size){ 11 | debug_stack = size 12 | } 13 | NewLine = '\n' 14 | 15 | func panic(args...){ 16 | ret = string.dynstringfmt(args) 17 | if ret != null { 18 | fmt.vfprintf(std.STDOUT,ret) 19 | } 20 | //REMOVE: todo 21 | if debug.enabled != 1 { 22 | p = null 23 | *p = 1 24 | } 25 | infos = debug.stack(debug_stack) 26 | fmt.println("\ndebug backtrace:") 27 | i = 1 28 | for v : infos { 29 | fmt.printf("%d: %s\n",i,v) 30 | i += 1 31 | } 32 | os.exit(-1) 33 | } 34 | func die(str){ 35 | fmt.println(str) 36 | code = -1 37 | if debug.enabled != 1 { 38 | p = null 39 | *p = 1 40 | } 41 | infos = debug.stack(debug_stack) 42 | fmt.println("\ndebug backtrace:") 43 | i = 1 44 | for v : infos { 45 | fmt.printf("%d: %s\n",i,v) 46 | i += 1 47 | } 48 | std.die(code) 49 | } 50 | func dief(args...){ 51 | ret = string.dynstringfmt(args) 52 | if ret != null { 53 | fmt.vfprintf(std.STDOUT,ret.putc(NewLine)) 54 | } 55 | if debug.enabled != 1 { 56 | p = null 57 | *p = 1 58 | } 59 | code = -1 60 | infos = debug.stack(debug_stack) 61 | fmt.println("\ndebug backtrace:") 62 | i = 1 63 | for v : infos { 64 | fmt.printf("%d: %s\n",i,v) 65 | i += 1 66 | } 67 | std.die(code) 68 | } 69 | func exit(code){ 70 | std.die(*code) 71 | } -------------------------------------------------------------------------------- /library/os/gc.tu: -------------------------------------------------------------------------------- 1 | 2 | use runtime 3 | 4 | fn gc() 5 | { 6 | runtime.GC() 7 | } 8 | 9 | fn enablegc(){ 10 | runtime.gc.enablegc = true 11 | } 12 | 13 | fn disbalegc(){ 14 | runtime.gc.enablegc = false 15 | } -------------------------------------------------------------------------------- /library/os/shell.tu: -------------------------------------------------------------------------------- 1 | use std 2 | use runtime 3 | 4 | EXIT_CODE = 127 5 | 6 | func system_argv(sh,c,cmd){ 7 | argv = new 32 8 | p = argv 9 | *p = sh p += 8 10 | *p = c p += 8 11 | *p = cmd p += 8 12 | *p = 0 13 | return argv 14 | } 15 | 16 | func shell(cmd) 17 | { 18 | argv = system_argv(*"sh",*"-c",*cmd) 19 | if fork() == 0 { 20 | std.execve(*"/bin/sh",argv,runtime.ori_envp) 21 | std.die(EXIT_CODE) 22 | } 23 | _wait4() 24 | } -------------------------------------------------------------------------------- /library/os/signal.tu: -------------------------------------------------------------------------------- 1 | use std 2 | 3 | EMPTY_MASK = 0xFFFFFFFFFFFFFFFF 4 | Null = 0 5 | _SA_RESTART = 0x10000000 6 | _SA_ONSTACK = 0x8000000 7 | _SA_RESTORER = 0x4000000 8 | _SA_SIGINFO = 0x4 9 | 10 | SIGHUP = 1 SIGINT = 2 SIGQUIT = 3 SIGILL = 4 SIGTRAP = 5 11 | SIGABRT = 6 SIGBUS = 7 SIGFPE = 8 SIGKILL = 9 SIGUSR1 = 10 12 | SIGSEGV = 11 SIGUSR2 = 12 SIGPIPE = 13 SIGALRM = 14 SIGTERM = 15 13 | SIGSTKFLT = 16 SIGCHLD = 17 SIGCONT = 18 SIGSTOP = 19 SIGTSTP = 20 14 | SIGTTIN = 21 SIGTTOU = 22 SIGURG = 23 SIGXCPU = 24 SIGXFSZ = 25 15 | SIGVTALRM = 26 SIGPROF = 27 SIGWINCH = 28 SIGIO = 29 SIGPWR = 30 16 | SIGSYS = 31 SIGRTMIN = 34 17 | 18 | mem Sigactiont { 19 | u64 sa_handler,sa_flags,sa_restorer,sa_mask 20 | } 21 | 22 | func setsignal(i , fc) { 23 | sa = new Sigactiont 24 | sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTORER | _SA_RESTART 25 | sa.sa_mask = EMPTY_MASK 26 | 27 | // sa.sa_restorer = std.sigreturn 28 | sa.sa_handler = fc 29 | mask_size = 8 30 | std.rt_sigaction(i, sa, Null,mask_size) 31 | } -------------------------------------------------------------------------------- /library/os/sys.tu: -------------------------------------------------------------------------------- 1 | //implement by asm 2 | func _getpid() 3 | func _fork() 4 | func _wait4() 5 | 6 | //user space 7 | func getpid() { 8 | return int(_getpid()) 9 | } 10 | func fork(){ 11 | return int(_fork()) 12 | } 13 | -------------------------------------------------------------------------------- /library/std/atomic/atomic.tu: -------------------------------------------------------------------------------- 1 | func cas(addr, old, newv) 2 | func cas64(addr, old, newv) 3 | func store(addr ,old,newv) 4 | func store32(addr ,old,newv){ 5 | return store(addr,old,newv) 6 | } 7 | func store64(addr ,old,newv) 8 | func xchg (addr , newv) 9 | func xadd(addr , newv) 10 | func xadd64(addr , newv) 11 | 12 | func swap_i32(addr , newv) { 13 | return xchg(addr,newv) 14 | } 15 | func load(ptr) { 16 | return *ptr 17 | } 18 | func load64(ptr){ 19 | return *ptr 20 | } 21 | fn or8(ptr , v) 22 | -------------------------------------------------------------------------------- /library/std/define.tu: -------------------------------------------------------------------------------- 1 | Null = 0 2 | # array 3 | ARRAY_SIZE = 8 4 | 5 | 6 | CLOCK_REALTIME = 0 7 | CLOCK_MONOTONIC = 1 8 | CLOCK_PROCESS_CPUTIME_ID = 2 9 | CLOCK_THREAD_CPUTIME_ID = 3 10 | CLOCK_MONOTONIC_RAW = 4 11 | CLOCK_REALTIME_COARSE = 5 12 | CLOCK_MONOTONIC_COARSE = 6 13 | CLOCK_BOOTTIME = 7 14 | CLOCK_REALTIME_ALARM = 8 15 | CLOCK_BOOTTIME_ALARM = 9 16 | CLOCK_SGI_CYCLE = 10 17 | CLOCK_TAI = 11 18 | 19 | mem TimeSpec { 20 | i64 sec,nsec 21 | } 22 | 23 | seed 24 | func stdinit(){ 25 | seed = time() 26 | initmalloc() 27 | } 28 | -------------------------------------------------------------------------------- /library/std/header.tu: -------------------------------------------------------------------------------- 1 | ZERO = 0 2 | Done = 1 3 | 4 | STDERR = 2 5 | STDOUT = 1 6 | STDIN = 0 7 | 8 | EOF = -1 9 | 10 | O_RDONLY = 0 11 | O_BINARY = 0 12 | O_WRONLY = 1 13 | O_RDWR = 2 14 | O_CREAT = 64 15 | O_TRUNC = 512 16 | O_APPEND = 1024 17 | O_DIRECTORY = 65536 18 | 19 | S_IFDIR = 16384 20 | S_IFREG = 32768 21 | 22 | 23 | SEEK_SET = 0 24 | SEEK_CUR = 1 25 | SEEK_END = 2 26 | 27 | //file stat 28 | mem Stat { 29 | u64 st_dev 30 | u64 st_ino 31 | u64 st_nlink 32 | u32 st_mode 33 | i32 st_uid 34 | i32 st_gid 35 | i32 __pad 36 | u64 std_rdev 37 | u64 st_size 38 | u64 st_blksize 39 | u64 st_blocks 40 | u64 st_atime[2] 41 | u64 st_mtime[2] 42 | u64 st_ctime[2] 43 | u64 reserved[3] 44 | } -------------------------------------------------------------------------------- /library/std/map/node.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | 3 | 4 | mem RbtreeNode { 5 | u64 key 6 | 7 | RbtreeNode* left 8 | RbtreeNode* right 9 | RbtreeNode* parent 10 | 11 | runtime.Value* k 12 | runtime.Value* v 13 | u8 color 14 | } 15 | RbtreeNode::red(){ 16 | this.color = 1 17 | } 18 | RbtreeNode::black(){ 19 | this.color = 0 20 | } 21 | RbtreeNode::min(sentinel){ 22 | node = this 23 | while node.left != sentinel { 24 | node = node.left 25 | } 26 | return node 27 | } -------------------------------------------------------------------------------- /library/std/mem.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | 3 | OP_T_THERS = 16 4 | 5 | OPSIZE = 8 6 | 7 | func byte_copy(dst,src,bytes){ 8 | 9 | while bytes > 0 { 10 | x = *src 11 | # mov pointer 12 | src += 1 13 | bytes -= 1 14 | *dst = x 15 | # mov pointer 16 | dst += 1 17 | } 18 | } 19 | func memcpy(dst,src,bytes){ 20 | byte_copy(dst,src,bytes) 21 | } 22 | func strcopy(dst,src){ 23 | ret = dst 24 | while *src != 0 { 25 | *dst = *src 26 | dst += 1 27 | src += 1 28 | } 29 | *dst = 0 30 | return ret 31 | } 32 | 33 | func strcmp(p1, p2) { 34 | s1 = p1 35 | s2 = p2 36 | c1 = 0 37 | c2 = 0 38 | 39 | while c1 == c2 { 40 | c1 = *s1 41 | s1 += 1 42 | 43 | c2 = *s2 44 | s2 += 1 45 | if c1 == 0 return c1 - c2 46 | } 47 | 48 | return c1 - c2 49 | } 50 | func strcat(dest , src){ 51 | strcopy(dest + strlen(dest) , src) 52 | return dest 53 | } 54 | func memcmp(vl , vr, n) 55 | { 56 | l = vl 57 | r = vr 58 | while n && *l == *r { 59 | n -= 1 60 | l += 1 61 | r += 1 62 | } 63 | if n > 0 { 64 | return *l - *r 65 | } 66 | return runtime.Zero 67 | } 68 | func memset(s , c , n) 69 | { 70 | uc = c 71 | su = s 72 | 73 | while n > 0 { 74 | *su = uc 75 | su += 1 76 | n -= 1 77 | } 78 | return s 79 | } 80 | -------------------------------------------------------------------------------- /library/std/regex/replace.tu: -------------------------------------------------------------------------------- 1 | use string 2 | use fmt 3 | use runtime 4 | use std 5 | 6 | 7 | func replace(str,src,dst){ 8 | ret = string.split(str,src) 9 | arr = ret.data 10 | 11 | s = "" 12 | p = arr.addr 13 | for( i = 1 ; i <= arr.used ; i += 1){ 14 | v = *p 15 | p += arr.size 16 | s += v 17 | if i < arr.used 18 | s += dst 19 | } 20 | return s 21 | 22 | } -------------------------------------------------------------------------------- /library/std/sys.tu: -------------------------------------------------------------------------------- 1 | //die;implement by asm 2 | func die(code) 3 | 4 | //time ; implement by asm 5 | func time(t) 6 | 7 | func clock_gettime(clockid,tp) 8 | 9 | //nanossleep ; implement by asm 10 | func nanosleep(req,rem) 11 | 12 | //brk ; implement by asm 13 | func brk(brk) 14 | 15 | //segsegv recv; implement by asm 16 | func segsegvrecv() 17 | 18 | //execv ; implement by asm 19 | func execve(filename,argv,envp) 20 | 21 | func sigreturn() 22 | //sig %rdi 23 | //act %rsi 24 | //oact %rdx 25 | //size %r10 26 | func rt_sigaction(sig,act,oact,size) 27 | 28 | //sys_mmap 0x9 29 | //@param addr u64 %rdi 30 | //@param len u64 %rsi 31 | //@param prot u64 %rdx 32 | //@param flags u64 %r10 33 | //@param fd u64 %r8 34 | //@param off u64 %r9 35 | func mmap(addr , len , prot ,flags, fd,off) 36 | //sys_madvise 28 37 | //@param start u64 38 | //@param len_in u64 39 | //@param behavior i32 40 | func madvise(start , len_in , behavior) 41 | //sys_munmap 11 42 | //@param addr u64 43 | //@param len u64 44 | func munmap(addr , len) 45 | //@return u64 46 | fn cputicks() 47 | //@return u64 48 | fn gettid() -------------------------------------------------------------------------------- /library/time/sleep.tu: -------------------------------------------------------------------------------- 1 | 2 | use std 3 | 4 | mem TimeSpec { 5 | i64 sec,nsec 6 | } 7 | Einter = 4 8 | 9 | func sleep(sec){ 10 | req = null 11 | rem = null 12 | req.sec = *sec 13 | req.nsec = 0 14 | 15 | ret = std.nanosleep(&req,&rem) 16 | } 17 | func usleep(mill){ 18 | req = null 19 | rem = null 20 | req.nsec = *mill 21 | std.nanosleep(&req,&rem) 22 | } 23 | -------------------------------------------------------------------------------- /linker/README.md: -------------------------------------------------------------------------------- 1 | 基于tu-lang自举实现的tol链接器,0依赖glibc,目前已支持任意amd64-linux下的编译执行,见:`linker/bin/amd64_bin` 2 | - [x] amd64 3 | - [x] linux 4 | - [ ] mac 5 | - [ ] windows 6 | 7 | # 链接测试demo 8 | ``` 9 | $ cd linker/demo 10 | $ tu -c . 11 | ``` 12 | 链接生成可执行文件生成可执行文件 13 | ``` 14 | $ ./bin/amd64_linux_tl2 -p ./demo 15 | $ chmod 777 a.out 16 | 17 | $ ./a.out 18 | > hello tu-lang! 19 | ``` 20 | ![image](./asserts/linker-demo.png#w50) 21 | 22 | # 自举测试 23 | ![image](./asserts/linker-compile.png#w50) -------------------------------------------------------------------------------- /linker/asserts/linker-compile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/linker/asserts/linker-compile.png -------------------------------------------------------------------------------- /linker/asserts/linker-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/linker/asserts/linker-demo.png -------------------------------------------------------------------------------- /linker/bin/amd64_linux_tl1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/linker/bin/amd64_linux_tl1 -------------------------------------------------------------------------------- /linker/bin/amd64_linux_tl2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/linker/bin/amd64_linux_tl2 -------------------------------------------------------------------------------- /linker/bin/demo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/linker/bin/demo -------------------------------------------------------------------------------- /linker/demo/init.s: -------------------------------------------------------------------------------- 1 | .text 2 | .global main 3 | main: 4 | call main_main 5 | call die 6 | leaveq 7 | retq 8 | -------------------------------------------------------------------------------- /linker/demo/main.s: -------------------------------------------------------------------------------- 1 | .data 2 | L001: 3 | .string "hello tu-lang!\n" 4 | 5 | .text 6 | .global write 7 | write: 8 | mov $0x1,%rax 9 | syscall 10 | retq 11 | 12 | .global die 13 | die: 14 | mov $0 , %rdi 15 | mov $60,%rax 16 | syscall 17 | retq 18 | 19 | .global main_main 20 | main_main: 21 | push %rbp 22 | mov %rsp, %rbp 23 | mov $1,%rdi 24 | lea L001(%rip), %rsi 25 | mov $15,%rdx 26 | call write 27 | mov %rbp, %rsp 28 | pop %rbp 29 | ret 30 | -------------------------------------------------------------------------------- /linker/link/block.tu: -------------------------------------------------------------------------------- 1 | mem Block { 2 | i8* data 3 | u32 offset 4 | u32 size 5 | } 6 | fn newBlock(d,off,si){ 7 | r = new Block 8 | r.data = d 9 | r.offset = off 10 | r.size = si 11 | return r 12 | } 13 | -------------------------------------------------------------------------------- /linker/link/define.tu: -------------------------------------------------------------------------------- 1 | 2 | MEM_ALIGN = 4096 3 | BASE_ADDR = 0x08040000 4 | DISC_ALIGN = 8 -------------------------------------------------------------------------------- /linker/link/sym_link.tu: -------------------------------------------------------------------------------- 1 | 2 | class SymLink 3 | { 4 | name 5 | recv 6 | prov 7 | } -------------------------------------------------------------------------------- /linker/linux/define.tu: -------------------------------------------------------------------------------- 1 | 2 | ET_NONE = 0 3 | ET_REL = 1 4 | ET_EXEC = 2 5 | ET_DYN = 3 6 | ET_CORE = 4 7 | ET_NUM = 5 8 | ET_LOOS = 0xfe00 9 | ET_HIOS = 0xfeff 10 | ET_LOPROC = 0xff00 11 | ET_HIPROC = 0xffff 12 | 13 | PF_X = 1 14 | PF_W = 2 15 | PF_R = 4 16 | PF_MASKOS = 0x0ff00000 17 | PF_MASKPROC = 0xf0000000 18 | 19 | SHN_UNDEF = 0 20 | SHN_COMMON = 0xfff2 21 | SHN_ABS = 0xfff1 22 | 23 | R_X86_64_PC32 = 2 24 | R_X86_64_PLT32 = 4 25 | R_X86_64_64 = 1 26 | R_X86_64_GOTPCREL = 9 27 | 28 | STB_LOCAL = 0 29 | STB_GLOBAL = 1 30 | STT_OBJECT = 1 31 | STT_FUNC = 2 32 | 33 | SHF_WRITE = 1 34 | SHF_ALLOC = 2 35 | SHF_EXECINSTR = 4 36 | 37 | EM_X86_64 = 62 38 | EV_NONE = 0 39 | EV_CURRENT = 1 40 | EV_NUM = 2 41 | 42 | PT_NULL = 0 43 | PT_LOAD = 1 44 | PT_DYNAMIC = 2 45 | PT_INTERP = 3 46 | PT_NOTE = 4 47 | PT_SHLIB = 5 48 | PT_PHDR = 6 49 | 50 | 51 | SHT_PROGBITS = 1 52 | SHT_SYMTAB = 2 53 | SHT_STRTAB = 3 54 | SHT_NOBITS = 8 55 | 56 | 57 | func ELF64_R_SYM(i) { 58 | return i >> 32 59 | } 60 | func ELF32_ST_BIND(val){ 61 | return val >> 4 62 | } 63 | func ELF64_ST_BIND(val){ 64 | return ELF32_ST_BIND(val) 65 | } 66 | func ELF64_ST_TYPE(val){ 67 | return val & 0xf 68 | } 69 | func ELF64_R_TYPE(val){ 70 | return val & 0xffffffff 71 | } -------------------------------------------------------------------------------- /linker/linux/file_write.tu: -------------------------------------------------------------------------------- 1 | 2 | use std 3 | use linker.utils 4 | use fmt 5 | File::writeHeader(out) 6 | { 7 | utils.debug("File::writeHeader >",out) 8 | ehdr = this.ehdr 9 | bytes = 0 10 | 11 | fp = utils.fopen(out,"w+") 12 | bytes += int(ehdr.e_ehsize) 13 | 14 | utils.fwrite(fp,ehdr,ehdr.e_ehsize) 15 | 16 | if std.len(this.phdrTab) != 0 { //program header section 17 | for (phd : this.phdrTab) { 18 | bytes += int(ehdr.e_phentsize) 19 | utils.fwrite(fp,phd,ehdr.e_phentsize) 20 | } 21 | } 22 | utils.fclose(fp) 23 | utils.debug("File::writeHeader successfully :",bytes) 24 | return bytes 25 | } 26 | 27 | File::writeSecSym(out) 28 | { 29 | utils.debug("File::writeSecSym") 30 | bytes = 0 31 | ehdr = this.ehdr 32 | shstrtabSize = *this.shstrtabSize 33 | 34 | fp = utils.fopen(out,"a+") 35 | bytes += int(shstrtabSize) 36 | utils.fwrite(fp,this.shstrtab,shstrtabSize) 37 | for ( sn : this.shdrNames) { 38 | 39 | sh = this.shdrTab[sn] 40 | bytes += int(ehdr.e_shentsize) 41 | utils.fwrite(fp,sh,ehdr.e_shentsize) 42 | } 43 | Elf64_Sym_Size = sizeof(Elf64_Sym) 44 | for (symname : this.symNames) { 45 | sym = this.symTab[symname] 46 | bytes += int(Elf64_Sym_Size) 47 | utils.fwrite(fp,sym,Elf64_Sym_Size) 48 | } 49 | bytes += this.shstrtabSize 50 | utils.fwrite(fp,this.strtab,shstrtabSize) 51 | utils.fclose(fp) 52 | return bytes 53 | } 54 | 55 | -------------------------------------------------------------------------------- /linker/linux/header_64.tu: -------------------------------------------------------------------------------- 1 | // u16 Elf32_Half; 2 | // u16 Elf64_Half; 3 | // u32 Elf32_Word; 4 | // int32 Elf32_Sword; 5 | // u32 Elf64_Word; 6 | // int32 Elf64_Sword; 7 | 8 | // u64 Elf32_Xword; 9 | // int64 Elf32_Sxword; 10 | // u64 Elf64_Xword; 11 | // int64 Elf64_Sxword; 12 | 13 | // u32 Elf32_Addr 14 | // u64 Elf64_Addr 15 | // u32 Elf32_Off; 16 | // u64 Elf64_Off 17 | // u16 Elf32_Section; 18 | // u16 Elf64_Section; 19 | // u16 Elf32_Versym; 20 | // u16 Elf64_Versym; 21 | 22 | mem Elf64_Ehdr 23 | { 24 | u8 e_ident[16] 25 | u16 e_type 26 | u16 e_machine 27 | u32 e_version 28 | u64 e_entry 29 | u64 e_phoff 30 | u64 e_shoff 31 | u32 e_flags 32 | 33 | u16 e_ehsize 34 | u16 e_phentsize 35 | u16 e_phnum 36 | u16 e_shentsize 37 | u16 e_shnum 38 | u16 e_shstrndx 39 | } 40 | 41 | mem Elf64_Phdr 42 | { 43 | u32 p_type 44 | u32 p_flags 45 | u64 p_offset 46 | u64 p_vaddr 47 | u64 p_paddr 48 | u64 p_filesz 49 | u64 p_memsz 50 | u64 p_align 51 | } 52 | 53 | mem Elf64_Shdr 54 | { 55 | u32 sh_name 56 | u32 sh_type 57 | u64 sh_flags 58 | u64 sh_addr 59 | u64 sh_offset 60 | u64 sh_size 61 | u32 sh_link 62 | u32 sh_info 63 | u64 sh_addralign 64 | u64 sh_entsize 65 | } 66 | 67 | mem Elf64_Sym 68 | { 69 | u32 st_name 70 | u8 st_info 71 | u8 st_other 72 | u16 st_shndx 73 | u64 st_value 74 | u64 st_size 75 | } 76 | 77 | mem Elf64_Rela 78 | { 79 | u64 r_offset 80 | u64 r_info 81 | i64 r_addend 82 | } -------------------------------------------------------------------------------- /linker/linux/rel.tu: -------------------------------------------------------------------------------- 1 | 2 | class RelItem 3 | { 4 | segname = sname 5 | rel = rel 6 | relname = rname 7 | func init(sname,rel,rname){} 8 | } -------------------------------------------------------------------------------- /linker/mac/README.md: -------------------------------------------------------------------------------- 1 | //mac -------------------------------------------------------------------------------- /linker/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | log(){ 3 | str="$1" 4 | echo -e "\033[32m$str \033[0m " 5 | } 6 | failed(){ 7 | str="$1" 8 | echo -e "\033[31m$str \033[0m" 9 | ps aux|grep test.sh|awk '{print $2}' |xargs kill -9 10 | exit 1 11 | } 12 | clean() { 13 | if ls $1 > /dev/null 2>&1; then 14 | rm -rf $1 15 | fi 16 | } 17 | check(){ 18 | if [ "$?" != 0 ]; then 19 | # actual=`./a.out` 20 | # if [ "$?" != 0 ]; then 21 | failed "exec failed" 22 | # fi 23 | # rm ./a.out 24 | fi 25 | 26 | } 27 | 28 | assert(){ 29 | log "[compile] tu -s linker/main.tu " 30 | tu -s linker/main.tu -std 31 | check 32 | echo "tu -c /usr/local/lib/coasm -c ." 33 | tu -c . -c /usr/local/lib/coasm 34 | echo "start linking..." 35 | log "[linker] tu -o ." 36 | tu -o . 37 | check 38 | chmod 777 a.out 39 | mv a.out tl_test 40 | cd linker/demo;tua -p . 41 | cd ../../ 42 | ./tl_test -p linker/demo 43 | check 44 | chmod 777 a.out 45 | echo "exec a.out..." 46 | ./a.out 47 | check 48 | rm ./a.out ./tl_test 49 | clean "*.s" 50 | clean "*.o" 51 | echo "exec done..." 52 | 53 | return 54 | # failed "[compile] $input failed" 55 | } 56 | assert 57 | log "all passing...." 58 | -------------------------------------------------------------------------------- /linker/utils/msg.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | use linker.link 4 | 5 | func debug(args...){ 6 | if link.trace { 7 | fmt.println(args) 8 | } 9 | } 10 | func print_green(red){ 11 | return " \033[32m" + red + "\033[0m" 12 | } 13 | func print_red(red){ 14 | return " \033[31m" + red + "\033[0m" 15 | } 16 | func msg(progress,str) { 17 | if progress == 0 { 18 | fmt.println(print_green(str)) 19 | }else { 20 | prefix = "[ " + progress + "%]" 21 | fmt.println(prefix + print_green(str)) 22 | } 23 | } 24 | func smsg(pre,str) { 25 | fmt.println(pre + print_green(str)) 26 | } 27 | func error(str) { 28 | fmt.println(print_red(str)) 29 | os.exit(-1) 30 | } 31 | func errorf(args...) { 32 | fmt.println( 33 | print_red( 34 | fmt.sprintf( 35 | args 36 | ) 37 | ) 38 | ) 39 | os.exit(-1) 40 | } -------------------------------------------------------------------------------- /linker/windows/README.md: -------------------------------------------------------------------------------- 1 | //windows -------------------------------------------------------------------------------- /release/tu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/release/tu -------------------------------------------------------------------------------- /release/tulang.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/release/tulang.a -------------------------------------------------------------------------------- /runtime/debug/common.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | use runtime 4 | 5 | // cfg(mod_static,true) 6 | 7 | lines = null 8 | elf = null 9 | //NOTICE: set by compiler 10 | enabled = 0 11 | 12 | func error(str){ 13 | os.die(str) 14 | } 15 | func debug(str){ 16 | fmt.println(str) 17 | } 18 | func check(ret){ 19 | if ret == 0 { 20 | error("check failed") 21 | } 22 | } 23 | // enable by compiler through -g parameter 24 | // like: tu run hello.tu -g 25 | func debug_init(){ 26 | //FIXME: "return" cause lost of initcall missing 27 | // if enabled != 1.(i8) return 1.(i8) 28 | if enabled == 1 { 29 | elf = new Elf { 30 | filepath : runtime.self_path 31 | } 32 | elf.init() 33 | 34 | seclines = elf.Section(".debug_line".(i8)) 35 | if seclines == null { 36 | debug("couldn't find .debug_line") 37 | return 0.(i8) 38 | } 39 | lines = new Lines { 40 | reader: Reader { 41 | buffer: elf.buffer + seclines.sh_offset, 42 | len: seclines.sh_size, 43 | offset: 0 44 | }, 45 | files: std.NewArray(), 46 | rows: std.NewArray() 47 | } 48 | lines.parse() 49 | } 50 | } 51 | func findpc(pc){ 52 | // must enable debug first 53 | // like: tu run hello.tu -g 54 | if enabled == 0.(i8) return int(pc) + ":??" 55 | 56 | row = lines.funcline(pc) 57 | if row == null return int(pc) + ":??" 58 | 59 | return fmt.sprintf( 60 | "%s:%d",string.new(row.filename),int(row.line) 61 | ) 62 | } -------------------------------------------------------------------------------- /runtime/debug/header.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | use std 4 | use string 5 | 6 | Null = 0 7 | True = 1 8 | False = 0 9 | 10 | EI_CLASS = 4 11 | EI_DATA = 5 12 | EI_VERSION = 6 13 | ELFCLASS64 = 2 14 | ELFDATA2LSB = 1 15 | EV_CURRENT = 1 16 | //OPTIMIZE: <*>= 17 | U8 = 1 18 | I8 = 1 19 | U16 = 2 20 | U32 = 4 21 | U64 = 8 22 | 23 | mem Reader { 24 | i8* buffer 25 | i32 len,offset 26 | } 27 | 28 | mem Row { 29 | u64 address 30 | i32 file,line 31 | } 32 | mem PcData { 33 | u64 address 34 | i32 line 35 | string.String* filename 36 | } 37 | 38 | 39 | mem LineHeader:pack{ 40 | u32 total 41 | u16 version 42 | u32 length 43 | u8 m_length 44 | u8 is_stmt 45 | i8 base 46 | u8 range 47 | u8 opcode_base 48 | } 49 | mem Lines { 50 | Reader reader 51 | i32 file_offset 52 | std.Array* files //[string] 53 | std.Array* rows // [Row] 54 | } -------------------------------------------------------------------------------- /runtime/debug/stack.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use runtime 3 | use os 4 | 5 | CUR_CALLER = 3 6 | func callerpc(){ 7 | sinfo = stack(CUR_CALLER) 8 | if std.len(sinfo) >= 3 { 9 | return sinfo[2] 10 | } 11 | return "??:??" 12 | } 13 | func stack(level){ 14 | bp = inter_get_bp() 15 | i = 0 16 | arr = [] 17 | //stack backtrace 18 | while i < level { 19 | pc = bp + 8 20 | rip = *pc 21 | if rip == null break 22 | arr[] = findpc(rip) 23 | bp = *bp 24 | if bp == null break 25 | i += 1 26 | } 27 | return arr 28 | } -------------------------------------------------------------------------------- /runtime/pools.tu: -------------------------------------------------------------------------------- 1 | use std 2 | use os 3 | use std.map 4 | 5 | chars = null 6 | strings = null 7 | enable_object_pool = 0 8 | 9 | func string_insert(temp, node,sentinel) 10 | { 11 | p = null 12 | 13 | loop { 14 | if node.key == temp.key { 15 | if node.k != temp.k { 16 | //TODO: 17 | //string hash conflict 18 | printf(*"[kv_update] hash conflict %s %s\n",node.v,temp.v) 19 | } 20 | temp.v = node.v 21 | return map.Update 22 | } 23 | if node.key < temp.key { 24 | p = &temp.left 25 | }else{ 26 | p = &temp.right 27 | } 28 | if *p == sentinel { 29 | break 30 | } 31 | temp = *p 32 | } 33 | 34 | *p = node 35 | node.parent = temp 36 | node.left = sentinel 37 | node.right = sentinel 38 | 39 | // make red 40 | node.color = 1 41 | return map.Insert 42 | } 43 | 44 | func pools_init(){ 45 | //init array 46 | chars = std.NewArray(256.(i8),8.(i8)) 47 | for i = 0 ; i < 256 ; i += 1 { 48 | c = new Value { 49 | type : Char, 50 | data : i 51 | } 52 | if chars.push(c) == Null { 53 | dief(*"chars pool init: memory failed") 54 | } 55 | } 56 | //init map 57 | strings = map.map_create(string_insert.(i64)) 58 | enable_object_pool = 1 59 | } -------------------------------------------------------------------------------- /syscall/asm_amd64.s: -------------------------------------------------------------------------------- 1 | .text 2 | .global main 3 | main: 4 | call std_stdinit 5 | call runtime_osinit 6 | mov (%rsp),%rdi 7 | lea 16(%rsp),%rsi 8 | push %rsi 9 | push %rdi 10 | call runtime_args_init 11 | call runtime_runtimeinit 12 | call main_init.0 13 | call main_main 14 | mov $0,%rdi 15 | call std_die 16 | leaveq 17 | retq 18 | -------------------------------------------------------------------------------- /syscall/std_atomic.s: -------------------------------------------------------------------------------- 1 | .text 2 | .global std_atomic_cas64 3 | std_atomic_cas64: 4 | mov %rdi , %rcx 5 | mov %rsi , %rax 6 | lock 7 | cmpxchgq %rdx,(%rcx) 8 | sete %al 9 | movzb %al, %rax 10 | retq 11 | 12 | .global std_atomic_cas 13 | std_atomic_cas: 14 | mov %rdi , %rcx 15 | mov %esi , %eax 16 | lock 17 | cmpxchgl %edx,(%rcx) 18 | sete %al 19 | movzb %al, %rax 20 | retq 21 | 22 | .global std_atomic_xchg 23 | std_atomic_xchg: 24 | mov %rdi , %rax 25 | mov %esi , %ecx 26 | xchg %ecx , (%rax) 27 | movsxd %ecx, %rax 28 | retq 29 | 30 | .global std_atomic_store 31 | std_atomic_store: 32 | mov %rdi , %rax 33 | mov %esi , %ecx 34 | xchg %ecx , (%rax) 35 | retq 36 | 37 | .global std_atomic_store64 38 | std_atomic_store64: 39 | mov %rdi , %rax 40 | mov %rsi , %rcx 41 | xchg %rcx , (%rax) 42 | retq 43 | 44 | .global std_atomic_xadd 45 | std_atomic_xadd: 46 | mov %rdi , %rdx 47 | mov %esi , %eax 48 | lock 49 | xadd %eax , (%rdx) 50 | add %esi , %eax 51 | ret 52 | 53 | .global std_atomic_xadd64 54 | std_atomic_xadd64: 55 | mov %rdi , %rdx 56 | mov %rsi , %rax 57 | lock 58 | xadd %rax , (%rdx) 59 | add %rsi , %rax 60 | retq 61 | 62 | .global std_atomic_or8 63 | std_atomic_or8: 64 | movq %rdi, %rdx 65 | movl %esi, %eax 66 | movzbl %al, %edi 67 | movzbl (%rdx), %eax 68 | sao_l1: 69 | movl %eax, %esi 70 | movl %eax, %ecx 71 | or %edi, %ecx 72 | lock cmpxchgb %cl, (%rdx) 73 | sete %cl 74 | je sao_l1 75 | movl %esi, %eax 76 | ret 77 | 78 | -------------------------------------------------------------------------------- /syscall/sys_os_amd64.s: -------------------------------------------------------------------------------- 1 | .text 2 | .global os__getpid 3 | os__getpid: 4 | mov $39,%rax 5 | syscall 6 | retq 7 | .global os__fork 8 | os__fork: 9 | mov $57 , %rax 10 | syscall 11 | retq 12 | .global os__wait4 13 | os__wait4: 14 | mov $61 , %rax 15 | syscall 16 | retq 17 | 18 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | 主要分为两大类测试 2 | - 包装类型的语法+依赖库+运行时测试 3 | - async 异步future状态机相关基础测试 4 | - class 类,基础,成员变量,成员函数 5 | - common 一些特性测试 6 | - datastruct 数据结构 7 | - internalpkg 内部依赖库 8 | - operator 操作符号相关 9 | - runtime 运行时测试 10 | - statement 语法测试 11 | - 原生类型的语法+依赖库+运行时测试 12 | - native 原生类型的依赖库相关测试 13 | - memory 原生类型的内存相关测试,操作符号相关等 14 | - 原生类型+动态类型复杂运算组合情况的测试 15 | - mixed 16 | 17 | -------------------------------------------------------------------------------- /tests/class/file/define.tu: -------------------------------------------------------------------------------- 1 | 2 | class File 3 | { 4 | var1 5 | var2 6 | var3 7 | } -------------------------------------------------------------------------------- /tests/class/file/file2/define.tu: -------------------------------------------------------------------------------- 1 | class File 2 | { 3 | var1 4 | var2 5 | var3 6 | } -------------------------------------------------------------------------------- /tests/class/file/file2/func.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | File::init(){ 4 | fmt.println("this is file.file2.File class") 5 | } 6 | File::SetVar1(b) 7 | { 8 | //FIXME: var1 = b 9 | this.var1 = b 10 | } -------------------------------------------------------------------------------- /tests/class/file/func.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | 4 | File::init(){ 5 | fmt.println("this is file.File class") 6 | } 7 | File::SetVar1(b) 8 | { 9 | //FIXME: var1 = b 10 | this.var1 = b 11 | } -------------------------------------------------------------------------------- /tests/class/object_func.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | use pkg 5 | 6 | class Http { 7 | request 8 | response 9 | func test_member(){ 10 | fmt.println("[object] test member") 11 | if this.request != "request" { 12 | fmt.println("[object] test member failed this.request != request") 13 | os.exit(1) 14 | } 15 | fmt.println("[object] test member success") 16 | } 17 | func test_func_arg(arg){ 18 | fmt.println("[object] test func arg") 19 | if this.request != "request" { 20 | fmt.println("[object] test member failed this.request != request") 21 | os.exit(1) 22 | } 23 | 24 | if arg != "args" { 25 | fmt.println("[object] test func arg failed arg != args") 26 | os.exit(1) 27 | } 28 | fmt.println("[object] test func arg success") 29 | } 30 | } 31 | 32 | func extern_func(this,str,num){ 33 | //b is this 34 | if str == "str" {} else os.die("str not str") 35 | if num == 100 {} else os.die("num not 100") 36 | return "extern_func" 37 | } 38 | Http::test_memberfunc(){ 39 | fmt.println("test_memberfunc") 40 | if this.fc("str",100) != "extern_func" { 41 | os.die("should be extern_func") 42 | } 43 | fmt.println("test_memberfunc success") 44 | } 45 | 46 | pkg.Factory::f3(){} 47 | func main(){ 48 | a = new Http() 49 | a.request = "request" 50 | // 成员函数调用测试 51 | a.test_member() 52 | // 成员函数参数测试 53 | a.test_func_arg("args") 54 | //成员变量当做成员函数调用 55 | a. fc = extern_func 56 | a.test_memberfunc() 57 | //包外成员函数定义 58 | obj = new pkg.Factory() 59 | obj.f1() obj.f2() obj.f3() 60 | } -------------------------------------------------------------------------------- /tests/class/object_inherit.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | 5 | class Grand { 6 | func grand_child(){ 7 | fmt.println("test grand_child") 8 | if this.who() != "child" 9 | os.die("this should be child") 10 | fmt.println("test grand_child success") 11 | } 12 | } 13 | Grand::teach(){ 14 | fmt.println("test grand teach child") 15 | if this.dynamic_var != 100 { 16 | os.die("child.dynmic_var should be 99") 17 | } 18 | fmt.println("test grand teach child success") 19 | } 20 | class Father : Grand { 21 | func father_child(){ 22 | fmt.println("test father_child") 23 | if this.who() != "child" 24 | os.die("this should be child") 25 | fmt.println("test father_child success") 26 | } 27 | } 28 | class Child : Father { 29 | func child(){ 30 | fmt.println("test child") 31 | if this.who() != "child" 32 | os.die("this should be child") 33 | fmt.println("test child success") 34 | } 35 | } 36 | Grand::who(){ return "grand" } 37 | Father::who(){ return "father" } 38 | Child::who() { return "child" } 39 | 40 | func test_inerit(obj){ 41 | obj.child() 42 | obj.father_child() 43 | obj.grand_child() 44 | } 45 | 46 | func test_access_child(obj){ 47 | obj.dynamic_var = 100 48 | obj.teach() 49 | } 50 | func main(){ 51 | obj = new Child() 52 | test_inerit(obj) 53 | test_access_child(obj) 54 | } 55 | -------------------------------------------------------------------------------- /tests/class/object_mem.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use std 3 | use string 4 | use os 5 | 6 | class Con { 7 | arr 8 | map 9 | } 10 | Con::test_inner(){ 11 | fmt.println("test_inner") 12 | if this.arr[1] != 23 os.die("this.arr[1] should eq 23") 13 | if this.map["sec"] != 24 os.die("this.map[sec] should eq 24") 14 | 15 | fmt.println("test_inner success " + this.arr) 16 | 17 | } 18 | //测试直接访问成员数组,map 19 | func test_local(obj){ 20 | fmt.println("test_local") 21 | if obj.arr[1] != 23 os.die("obj.arr[1] should eq 23") 22 | if obj.map["sec"] != 24 os.die("obj.map[sec] should eq 24") 23 | fmt.println("test_local success" + obj.arr) 24 | } 25 | 26 | gvar = new Con() 27 | func test_global(){ 28 | gvar.arr = [0,1,2] 29 | for k,v : gvar.arr { 30 | if k != v os.die( 31 | fmt.sprintf("kv:%d should be v:%d",k,v) 32 | ) 33 | } 34 | if 0 != gvar.arr[0] os.die("gvar.arr[0] != 0") 35 | gvar.arr[1] = 11 36 | if 11 != gvar.arr[1] os.die("gvar.arr[1] != 11") 37 | if 2 != gvar.arr[2] os.die("gvar.arr[2] != 2") 38 | fmt.println("test global var object member access success") 39 | } 40 | class Am { 41 | arr = [] 42 | func init(){ 43 | //varname as same as this.arr 44 | arr = 3 45 | this.arr[] = arr 46 | if this.arr[0] != 3 os.die("this.arr[0] should be 3") 47 | fmt.println("test arr_mem success") 48 | } 49 | } 50 | func main(){ 51 | obj = new Con() 52 | obj.arr = [1,23] 53 | obj.map = {"fir":23,"sec":24,"arr":obj.arr} 54 | test_local(obj) 55 | obj.test_inner() 56 | 57 | test_global() 58 | new Am() //test arr mem op 59 | } 60 | -------------------------------------------------------------------------------- /tests/class/object_split.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | use file 5 | use file.file2 6 | 7 | func testobj1(obj) 8 | { 9 | fmt.println("test file.File") 10 | obj.init() 11 | obj.SetVar1("obj.var1") 12 | if obj.var1 != "obj.var1" { 13 | fmt.println("obj.var1 != obj.var1 ",obj.var1) 14 | os.exit(-1) 15 | } 16 | fmt.println("test file.File passed") 17 | } 18 | func testobj2(obj) 19 | { 20 | fmt.println("test file.fle2.File") 21 | obj.init() 22 | obj.SetVar1("obj2.var1") 23 | if obj.var1 != "obj2.var1" { 24 | fmt.println("obj.var1 != obj2.var1") 25 | os.exit(-1) 26 | } 27 | fmt.println("test file.file2.File passed") 28 | } 29 | 30 | 31 | func main(){ 32 | obj = new file.File() 33 | obj2 = new file2.File() 34 | testobj1(obj) 35 | testobj2(obj2) 36 | } -------------------------------------------------------------------------------- /tests/class/object_super_synatx.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | 5 | class Grand { 6 | grand = "grand" 7 | } 8 | class Father : Grand { 9 | father = "father" 10 | func init(){ 11 | super.init() 12 | } 13 | func get(){ return "father"} 14 | } 15 | class Child : Father { 16 | child = "child" 17 | func init(){ 18 | super.init() 19 | } 20 | func get(){return "child"} 21 | func testsuper(){ 22 | if super.get() != "father" os.panic("super.get != father") 23 | if this.get() != "child" os.panic("this.get != child") 24 | fmt.println("testsuper success") 25 | } 26 | } 27 | func main(){ 28 | obj = new Child() 29 | obj.testsuper() 30 | if obj.grand != "grand" os.panic("obj.grand != grand") 31 | if obj.father != "father" os.panic("obj.father != father") 32 | if obj.child != "child" os.panic("obj.child != child") 33 | fmt.println("test inherit super success") 34 | } 35 | -------------------------------------------------------------------------------- /tests/class/pkg/a.tu: -------------------------------------------------------------------------------- 1 | class Factory { 2 | fn f1(){} 3 | } 4 | Factory::f2(){} -------------------------------------------------------------------------------- /tests/common/class_member_initialization.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | 4 | garr = [3,5,7] 5 | // use std 6 | class NotI { 7 | int = 100 8 | str = "File.str" 9 | arr = [1,"arr",3,this.int] 10 | map = {"g": garr} 11 | func test(){ 12 | fmt.println("test NotI ") 13 | if this.int != 100 14 | os.panic("int should be 100 %d",this.int) 15 | if this.str != "File.str" 16 | os.panic("File.str != %s",this.str) 17 | if this.arr[3] != 100 18 | os.panic("100 != %d",this.arr[3]) 19 | if this.map["g"][1] != 5 20 | os.panic("5 != %d",this.map["g"][1]) 21 | fmt.println("test NotI success") 22 | } 23 | } 24 | class WithI { 25 | int = 100 26 | str = "File.str" 27 | arr = [1,"arr",3,this.int] 28 | map = {"g": garr} 29 | func init(){ 30 | this.int = 1 this.str = 1 this.arr = 1 this.map = 1 31 | } 32 | func test(){ 33 | fmt.println("test WithI ") 34 | if this.int != 1 os.panic("int 1 != %d",this.int) 35 | if this.str != 1 os.panic("str 1 != %d",this.str) 36 | if this.map != 1 os.panic("map 1 != %d",this.map) 37 | if this.arr != 1 os.panic("arr 1 != %d",this.arr) 38 | fmt.println("test WithI success") 39 | } 40 | } 41 | 42 | 43 | func main(){ 44 | obj = new NotI() 45 | obj.test() 46 | obj = new WithI() 47 | obj.test() 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /tests/common/global_mem_var.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use gmv 3 | use os 4 | func main(){ 5 | gmv.test() 6 | 7 | if gmv.gv.a != 200 { 8 | os.die("gmv.gv.a != 200") 9 | } 10 | if gmv.gv.b != 100 { 11 | os.die("gmv.gv.b != 200") 12 | } 13 | fmt.println("test global_mem_var assign success") 14 | } -------------------------------------------------------------------------------- /tests/common/globalvar_initialization.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | use var 5 | 6 | func main(){ 7 | if var.allcheck != 4 { 8 | os.die("should be 4") 9 | } 10 | fmt.println("test global var automatic initialization passed") 11 | } -------------------------------------------------------------------------------- /tests/common/gmv/a.tu: -------------------------------------------------------------------------------- 1 | use os 2 | func test(){ 3 | //compiler parse this file first 4 | //so the compiler can't recognize the gv is the global MEM var 5 | //have to take it as plain var 6 | //handle this should at asmgen period 7 | gv.b = 100 8 | if gv.b != 100 os.die("gvb is global mem var, gv.b should be 100") 9 | gv.a = 200 10 | if gv.a != 200 os.die("gva is global mem var, gv.a should be 200") 11 | //check again 12 | if gv.b != 100 os.die("gvb is global mem var, gv.b should be 100") 13 | fmt.println("test global mem var assign success") 14 | } -------------------------------------------------------------------------------- /tests/common/gmv/b.tu: -------------------------------------------------------------------------------- 1 | mem T { 2 | i32 a 3 | i32 b 4 | } 5 | 6 | gv = new T -------------------------------------------------------------------------------- /tests/common/init/init.tu: -------------------------------------------------------------------------------- 1 | total 2 | use fmt 3 | use init.init1 4 | 5 | func init(){ 6 | fmt.println("init.total ++") 7 | total += 1 8 | } -------------------------------------------------------------------------------- /tests/common/init/init1/init1.tu: -------------------------------------------------------------------------------- 1 | use init 2 | use fmt 3 | 4 | func init(){ 5 | fmt.println("init.init1 total ++") 6 | init.total += 1 7 | } -------------------------------------------------------------------------------- /tests/common/init/init_1.tu: -------------------------------------------------------------------------------- 1 | use init.init1 2 | use fmt 3 | 4 | func init(){ 5 | fmt.println("init_1 total ++ ") 6 | total += 1 7 | } -------------------------------------------------------------------------------- /tests/common/init_automatic_call.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use init 3 | use os 4 | 5 | func init(){ 6 | fmt.println("main init") 7 | init.total = 0 8 | } 9 | func main(){ 10 | if init.total != 3 os.die("something wrong here") 11 | fmt.println("test init called by automatic successful") 12 | } -------------------------------------------------------------------------------- /tests/common/logor_logand_priority.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | i = 0 5 | func test_dyn(){ 6 | if i == 1 && os.panic("die if"){} 7 | if i == 0 || os.panic("die if"){} 8 | for(j = 0; i == 1 && os.panic("die for") ; j += 1) { 9 | os.die("die for block") 10 | } 11 | for(j = 0; i == 0 || os.panic("die for") ; j += 1) { 12 | break 13 | } 14 | while i == 1 && os.panic("die while") { 15 | os.die("die for while") 16 | } 17 | while i == 0 || os.panic("die while") { 18 | break 19 | } 20 | match i { 21 | 0 | os.panic("die match") : { 22 | fmt.println("test match ok") 23 | } 24 | _ : os.die("die match") 25 | } 26 | fmt.println("test dyn && || priority success") 27 | } 28 | j = 0 29 | func test_native(){ 30 | if j == 1 && os.panic("die") {} 31 | if j == 0 || os.panic("die") {} 32 | for(jj = 0; j == 1 && os.panic("die for") ; jj += 1) { 33 | os.die("die for block") 34 | } 35 | for(jj = 0; j == 0 || os.panic("die for") ; jj += 1) { 36 | break 37 | } 38 | while j == 1 && os.panic("die while") { 39 | os.die("die for while") 40 | } 41 | while j == 0 || os.panic("die while") { 42 | break 43 | } 44 | match j { 45 | 0 | os.panic("die match") : { 46 | fmt.println("test match ok") 47 | } 48 | _ : os.die("die match") 49 | } 50 | fmt.println("test native && || priority success") 51 | } 52 | func main(){ 53 | test_dyn() 54 | test_native() # should careful to every native codes 55 | } 56 | 57 | -------------------------------------------------------------------------------- /tests/common/objmember/a.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | globalvar = new A() 4 | 5 | class A { 6 | var1 = {} 7 | name = "test" 8 | func getpkgname(){ 9 | return this.name 10 | } 11 | } -------------------------------------------------------------------------------- /tests/common/objmember/b.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | func test(){ 5 | globalvar.var1["s"] = "22" 6 | for name,v : globalvar.var1 { 7 | gname = globalvar.getpkgname() 8 | if gname != "test" os.die("test failed") 9 | } 10 | fmt.println("test passed, globalmember.call") 11 | } -------------------------------------------------------------------------------- /tests/common/package_call.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use objmember 3 | 4 | func main(){ 5 | //测试对象函数调用 6 | //包名调用 7 | //因为parser顺序的问题导致混淆的bug 8 | objmember.test() 9 | } -------------------------------------------------------------------------------- /tests/common/var/dyn/check.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | use var 3 | use std 4 | use os 5 | use fmt 6 | 7 | funcvar = gen() 8 | func gen(){ 9 | return "gen" 10 | } 11 | 12 | func init(){ 13 | if nothing != null os.die("uninitialized global var should be null") 14 | if str != "str2" os.die("str != str2") 15 | if int != 12345 os.die("int != 12345") 16 | if std.len(arr1) != 0 os.die("native.arr1 should be 0") 17 | if std.len(arr2) != 3 os.die("native.arr2 should be 3") 18 | if arr2[1] != "test" os.die("native.arr2[1] should be test") 19 | if map2["len"] != 5 os.die("native.map2[test] should be 5") 20 | if funcvar != "gen" os.die("funcvar != gen") 21 | 22 | var.allcheck += 1 23 | fmt.println("var.dyn.test passed") 24 | } -------------------------------------------------------------------------------- /tests/common/var/dyn/var.tu: -------------------------------------------------------------------------------- 1 | nothing 2 | 3 | str = "str2" 4 | int = 12345 5 | arr1 = [] 6 | arr2 = [1,"test",3] 7 | map1 = {} 8 | map2 = {"arr":"arr","map":"map","len":5} -------------------------------------------------------------------------------- /tests/common/var/native/var.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | use var 3 | use std 4 | use os 5 | use fmt 6 | 7 | str = "str2" 8 | int = 12345 9 | arr1 = [] 10 | arr2 = [1,"test",3] 11 | arr3 = arr2.data 12 | 13 | map1 = {} 14 | map2 = {"arr":"arr","map":"map","len":5} 15 | 16 | True = 1 17 | False = 0 18 | func init(){ 19 | //strcmp return 0 if eq 20 | if std.strcmp(str,*"str2") != False { 21 | os.die("native.str != str2") 22 | } 23 | if int != 12345 os.die("native.int != 12345") 24 | if std.len(arr1) != 0 os.die("native.arr1 should be 0") 25 | if std.len(arr2) != 3 os.die("native.arr2 should be 3") 26 | if arr3.used != 3 os.die("native.arr2.len should be 3") 27 | _arr2 = arr2 28 | _map2 = map2 29 | if _arr2[1] != "test" os.die("native.arr2[1] should be test") 30 | if _map2["len"] != 5 os.die("native.map2[test] should be 5") 31 | 32 | //finish checkd 33 | var.allcheck += 1 34 | fmt.println("var.native.test passed") 35 | } -------------------------------------------------------------------------------- /tests/common/var/var.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | use var.dyn 4 | use var.native 5 | 6 | allcheck = 0 7 | func init(){ 8 | allcheck += 1 9 | } 10 | func init(){ 11 | allcheck += 1 12 | } -------------------------------------------------------------------------------- /tests/datastruct/bool.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func main(){ 6 | fmt.println("bool test") 7 | 8 | // 测试 bool 赋值 和 istrue判断 9 | isbool = true 10 | if isbool { 11 | fmt.println("bool test ok\n") 12 | }else{ 13 | fmt.println("bool test failed\n") 14 | os.exit(1) 15 | } 16 | 17 | // 测试 bool 赋值 和 istrue判断 18 | isbool = false 19 | if isbool { 20 | fmt.println("bool test failed\n") 21 | os.exit(1) 22 | }else{ 23 | fmt.println("bool test ok\n") 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /tests/datastruct/char.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | func test_equal(){ 4 | fmt.println("test char eq") 5 | str = "abcde" 6 | if str[0] == 'b' os.die("stro[0] should be a") 7 | if str[1] != 'b' os.die("stro[1] should be b") 8 | if str[2] != 'c' os.die("stro[2] should be c") 9 | if str[3] != 'd' os.die("stro[3] should be d") 10 | if str[4] != 'e' os.die("stro[4] should be e") 11 | //out 12 | if str[5] != 0 os.die("stro[5] should be outbound") 13 | fmt.println("test char eq success") 14 | } 15 | func test_unique(){ 16 | fmt.println("test char unique") 17 | 18 | str = "a" 19 | //TODO: 20 | //str = "\n\t\'\"\\" 21 | //str[0] == '\n' str[1] == '\t' str[2] == '\'' str[3] == '\"'.. 22 | if str[1] != '\0' os.die("str[1] should be \0") 23 | if '\n' != 10 os.die(" should be 10") 24 | if '\t' != 9 os.die(" should be 9") 25 | if '\\' != 92 os.die(" should be 92") 26 | if '\"' != 34 os.die(" should be 34") 27 | if '\'' != 39 os.die(" should be 39") 28 | 29 | 30 | fmt.println("test char unique success") 31 | } 32 | func main(){ 33 | test_equal() 34 | test_unique() 35 | } -------------------------------------------------------------------------------- /tests/datastruct/func.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use pkg1 4 | use os 5 | 6 | func func_noargs() { 7 | fmt.println("no args test") 8 | } 9 | func func_withargs(a,b,c) { 10 | if a != 1 { 11 | fmt.print("[failed] func_withargs a != 1 :",a,"\n") 12 | os.exit(1) 13 | } 14 | if b != "iamb" { 15 | fmt.print("[failed] func_withargs b != iamb :",b,"\n") 16 | os.exit(1) 17 | } 18 | if c[0] != "c[0]" { 19 | fmt.print("[failed] func_withargs c[0] != c[0] :",c,"\n") 20 | os.exit(1) 21 | } 22 | fmt.println(a,b,c) 23 | fmt.println("has args test") 24 | } 25 | func main(){ 26 | fc = func_noargs 27 | fc(1) 28 | fwn = func_withargs 29 | arr = ["c[0]"] 30 | fwn(1,"iamb",arr) 31 | p1 = pkg1.test 32 | p1() 33 | p2 = pkg1.test2 34 | p2() 35 | //TODO: support func pointer pass 36 | } 37 | -------------------------------------------------------------------------------- /tests/datastruct/global_int_string.tu: -------------------------------------------------------------------------------- 1 | 2 | // this is a global variable 3 | global_a 4 | 5 | use fmt 6 | use os 7 | 8 | use pkg1 9 | 10 | func current_pkg() 11 | { 12 | global_a = "test" 13 | } 14 | func current_pkg_test(){ 15 | 16 | global_a = 1 17 | if global_a != 1 { 18 | fmt.println("test global int failed") 19 | os.exit(1) 20 | } 21 | fmt.println("test global int successed") 22 | 23 | current_pkg() 24 | if global_a != "test" { 25 | fmt.println("test global string failed") 26 | os.exit(1) 27 | 28 | } 29 | fmt.println("test global string success ",global_a) 30 | 31 | } 32 | 33 | func other_pkg_test() 34 | { 35 | pkg1.global_a = 1000 36 | if pkg1.global_a != 1000 { 37 | fmt.println("other pkg test int failed",pkg1.global_a) 38 | os.exit(1) 39 | } 40 | fmt.print("pkg1.globals: ",pkg1.global_a, " \n") 41 | 42 | pkg1.change_global() 43 | if pkg1.global_a != "change" { 44 | fmt.println("other pkg test string failed",pkg1.global_a) 45 | os.exit(1) 46 | } 47 | fmt.println(pkg1.global_a) 48 | 49 | } 50 | 51 | 52 | func main(){ 53 | current_pkg_test() 54 | other_pkg_test() 55 | } 56 | -------------------------------------------------------------------------------- /tests/datastruct/global_map_array.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | use pkg2 5 | 6 | func array_test() 7 | { 8 | fmt.println("global array test") 9 | pkg2.global_var = [1,"two","three"] 10 | if pkg2.global_var[0] != 1 { 11 | fmt.print("pkg2.global_var[0] == 1 failed\n") 12 | os.exit(1) 13 | } 14 | if pkg2.global_var[1] != "two" { 15 | fmt.print("pkg2.global_var[1] == two failed\n") 16 | os.exit(1) 17 | } 18 | pkg2.global_var[1] = "not two anymore" 19 | if pkg2.global_var[1] != "not two anymore" { 20 | fmt.print("pkg2.global_var[1] == ... failed\n") 21 | os.exit(1) 22 | } 23 | pkg2.global_var[] = "four" 24 | if pkg2.global_var[3] != "four" { 25 | fmt.print("pkg2.global_var[3] == four failed\n") 26 | os.exit(1) 27 | } 28 | fmt.print("global array test success\n") 29 | } 30 | func map_test() 31 | { 32 | fmt.println("global map test\n") 33 | pkg2.global_var = {1:1,"two":"two","three":"three"} 34 | if pkg2.global_var[1] != 1 { 35 | fmt.print("pkg2.global_var[1] == 1 failed\n") 36 | os.exit(1) 37 | } 38 | if pkg2.global_var["two"] != "two" { 39 | fmt.print("pkg2.global_var[two] == two failed\n") 40 | os.exit(1) 41 | } 42 | pkg2.global_var["four"] = "four" 43 | if pkg2.global_var["four"] != "four" { 44 | fmt.print("pkg2.global_var[four] == four failed\n") 45 | os.exit(1) 46 | } 47 | fmt.print("global map test success\n") 48 | } 49 | 50 | func main(){ 51 | array_test() 52 | map_test() 53 | } 54 | -------------------------------------------------------------------------------- /tests/datastruct/int.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int() 6 | { 7 | a = 10 8 | b = 20 9 | if a != 10 { 10 | fmt.println("failed a") 11 | os.exit(1) 12 | } 13 | if b != 20 { 14 | fmt.println("failed b") 15 | os.exit(1) 16 | } 17 | a += b 18 | 19 | if a != 30 { 20 | fmt.println("failed a += b") 21 | os.exit(1) 22 | } 23 | a -= b 24 | if a != 10 { 25 | fmt.println("failed a -= b") 26 | os.exit(1) 27 | } 28 | a *= b 29 | if a != 200 { 30 | fmt.println("failed a *= b") 31 | os.exit(1) 32 | } 33 | a /= b 34 | if a != 10 { 35 | fmt.println("failed b /= a",a) 36 | os.exit(1) 37 | } 38 | fmt.print("success int + int ",a," ",b) 39 | 40 | } 41 | func test_string_int(num,str){ 42 | nstr = num + str 43 | if nstr != "99test" { 44 | fmt.println("failed num + str ",nstr) 45 | os.exit(1) 46 | } 47 | fmt.print("success num + str == ",nstr) 48 | } 49 | 50 | func main(){ 51 | test_int() 52 | test_string_int(99,"test") 53 | } 54 | -------------------------------------------------------------------------------- /tests/datastruct/pkg1/global_test.tu: -------------------------------------------------------------------------------- 1 | //pkg1.global_a 2 | global_a 3 | 4 | use fmt 5 | 6 | func change_global(){ 7 | fmt.print("test global1: ",global_a," \n") 8 | global_a = "change" 9 | fmt.print("test global1: ",global_a," \n") 10 | } -------------------------------------------------------------------------------- /tests/datastruct/pkg1/pkg2/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | obj 4 | 5 | 6 | func test() 7 | { 8 | fmt.println("path:/pkg1/pkg2/test.tu") 9 | obj = "pkg1.pkg2.obj" 10 | } -------------------------------------------------------------------------------- /tests/datastruct/pkg1/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test(){ 5 | fmt.println("this is pkg1\n") 6 | test2() 7 | } -------------------------------------------------------------------------------- /tests/datastruct/pkg1/test2.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test2(){ 5 | fmt.println("this is pkg1 function test2\n") 6 | } -------------------------------------------------------------------------------- /tests/datastruct/pkg2/array_map.tu: -------------------------------------------------------------------------------- 1 | // this is a global variable in package pkg2 2 | global_var -------------------------------------------------------------------------------- /tests/datastruct/pkg2/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test(){ 5 | fmt.println("this is pkg2\n") 6 | } -------------------------------------------------------------------------------- /tests/internalpkg/common.tu: -------------------------------------------------------------------------------- 1 | 2 | use os 3 | use fmt 4 | use std 5 | 6 | func test_rand(){ 7 | fmt.println("test_rand") 8 | 9 | v1 = std.rand(100) 10 | v2 = std.rand(100) 11 | if v1 > 100 os.die("v1 should < 100") 12 | if v2 > 100 os.die("v2 should < 100") 13 | if v1 == v2 os.die("v1 != v2") 14 | 15 | fmt.println("test_rand success") 16 | } 17 | func test_len(){ 18 | fmt.println("test std.len") 19 | a = null 20 | if std.len(a) == 0 {} else os.die("len should be 1 and with warning") 21 | a = 100 22 | if std.len(a) == 1 {} else os.die("len should be 1 and with warning") 23 | a = true 24 | if std.len(a) == 1 {} else os.die("len should be 1 and with warning") 25 | a = "test" 26 | if std.len(a) == 4 {} else os.dief("len should be 4 %d",std.len(a)) 27 | a = [1,2,4,"test"] 28 | if std.len(a) == 4 {} else os.dief("len should be 4") 29 | //unsupport map 30 | fmt.println("test std.len success") 31 | } 32 | gm = {} 33 | func test_hash64(){ 34 | for i = 0 ; i < 100000 ; i += 1 { 35 | s = i + "" 36 | l = std.len(s) 37 | p = int(std.hash64(*s,*l)) 38 | if gm[p] != null { 39 | os.dief("hashcode conflict %s %s %d",gm[p],s,p) 40 | } 41 | gm[p] = s 42 | } 43 | fmt.println("test hash64 success") 44 | } 45 | func main(){ 46 | test_rand() 47 | test_hash64() 48 | // fmt.println("before") 49 | //os.daemon() 50 | // fmt.println("after") 51 | } -------------------------------------------------------------------------------- /tests/internalpkg/dir/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/tests/internalpkg/dir/a -------------------------------------------------------------------------------- /tests/internalpkg/dir/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tu-lang/tu/8f2b87e5b0313bcc201c65b66118e0f96d6daf46/tests/internalpkg/dir/b -------------------------------------------------------------------------------- /tests/internalpkg/file/file.txt: -------------------------------------------------------------------------------- 1 | 1111111111 2 | 2222222222 3 | 3333333333 4 | 4444444444 5 | 5555555555 6 | 6666666666 7 | 7777777777 8 | 8888888888 9 | 9999999999 10 | 10 -------------------------------------------------------------------------------- /tests/internalpkg/fmt.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | 4 | func println(){ 5 | a = -1323 6 | if a { 7 | fmt.println("a is negative not should be here ",a,"wrong") 8 | }else{ 9 | fmt.println("ok ",a,"it's clear") 10 | } 11 | } 12 | func test_sprintf(){ 13 | fmt.println("test sprintf") 14 | //normal test 15 | //movq -16(%rsp) %rbp 16 | str = fmt.sprintf("%s %i(%%rsp) %%rbp","movq",-16) 17 | if str != "movq -16(%rsp) %rbp" { 18 | os.die("assert failed") 19 | } 20 | //stack params test,over 6 params 21 | dst = "first second third 1 2 -3 fourth fifth sixth" 22 | str = fmt.sprintf("%s %s %s %i %i %i %s %s %s" 23 | "first","second","third", 24 | 1,2,-3, 25 | "fourth","fifth","sixth" 26 | ) 27 | if dst != str { 28 | fmt.println(str) 29 | os.die("assert failed") 30 | } 31 | 32 | fmt.println("test sprintf success") 33 | } 34 | func main(){ 35 | println() 36 | test_sprintf() 37 | } 38 | 39 | -------------------------------------------------------------------------------- /tests/internalpkg/os_exit.tu: -------------------------------------------------------------------------------- 1 | 2 | use os 3 | 4 | func main(){ 5 | os.exit(0) 6 | } -------------------------------------------------------------------------------- /tests/internalpkg/os_gc.tu: -------------------------------------------------------------------------------- 1 | 2 | use os 3 | use fmt 4 | 5 | fn init(){ 6 | os.enablegc() 7 | } 8 | 9 | func string_test(){ 10 | fmt.println("[gc] string_test") 11 | a = "test" 12 | fmt.println(a) 13 | os.gc() 14 | if a != "test" { 15 | fmt.println("failed",a) 16 | os.exit(1) 17 | } 18 | fmt.println("[gc] string_test success") 19 | 20 | } 21 | func array_test(){ 22 | top = 1000000 23 | fmt.println("[gc] array_test") 24 | arr = [] 25 | i = 0 26 | while i < top { 27 | arr[] = i 28 | i += 1 29 | } 30 | os.gc() 31 | i = 0 32 | while i < top { 33 | if i % 100000 == 0 { 34 | os.gc() 35 | } 36 | if arr[i] != i { 37 | fmt.println("[gc] arr[%d] != %d\n",arr[i],i) 38 | os.die("") 39 | } 40 | i += 1 41 | } 42 | fmt.println("[gc] array_test success") 43 | } 44 | func print_pascal_triangle(level) 45 | { 46 | list = [ 1 ] 47 | i = 1 48 | while i <= level { 49 | k = 0 50 | temp_arr = [] 51 | 52 | while k < i { 53 | var = 0 54 | if k == 0 || k == i - 1 { 55 | var = 1 56 | }else{ 57 | var = list[k] + list[k - 1] 58 | } 59 | temp_arr[] = var 60 | k += 1 61 | } 62 | fmt.println("i=" + i + " " + temp_arr) 63 | //fmt.println(i) 64 | list = temp_arr 65 | i += 1 66 | } 67 | } 68 | 69 | func main(){ 70 | string_test() 71 | array_test() 72 | print_pascal_triangle(10) 73 | fmt.println("rt_os_gc test passed!") 74 | } -------------------------------------------------------------------------------- /tests/internalpkg/os_shell.tu: -------------------------------------------------------------------------------- 1 | use os 2 | 3 | func main(){ 4 | os.shell("echo yes") 5 | } -------------------------------------------------------------------------------- /tests/internalpkg/os_signal_test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use std 3 | use os 4 | use time 5 | 6 | func test(){ 7 | fmt.println("signal handler") 8 | os.exit(0) 9 | } 10 | func main(){ 11 | os.setsignal(os.SIGINT,test.(u64)) 12 | fmt.println("register signal") 13 | //kill self 14 | kstr = fmt.sprintf( 15 | "kill -2 %d", 16 | os.getpid() 17 | ) 18 | fmt.println(kstr) 19 | //kill self 20 | os.shell(kstr) 21 | time.sleep(2) 22 | os.die("register signal failed,not should be here") 23 | } 24 | -------------------------------------------------------------------------------- /tests/internalpkg/runtime_common.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use runtime 3 | use time 4 | use std 5 | 6 | lock 7 | fn thread1(){ 8 | println(*"test lock\n") 9 | lock.lock() 10 | println(*"test got lock 2\n") 11 | time.sleep(1) 12 | lock.unlock() 13 | println(*"test free lock\n") 14 | } 15 | fn println(arg1 , arg2 , arg3){ 16 | fmt.vfprintf(std.STDOUT,arg1,arg2,arg3) 17 | } 18 | fn test_lock(){ 19 | lock.init() 20 | println(*"start test_lock\n") 21 | lock.lock() 22 | runtime.newcore(thread1.(u64)) 23 | time.sleep(1) 24 | println(*"main prepare unlock\n") 25 | lock.unlock() 26 | time.sleep(1) 27 | lock.lock() 28 | println(*"test_lock success\n") 29 | } 30 | fn main(){ 31 | test_lock() 32 | } -------------------------------------------------------------------------------- /tests/internalpkg/std_dir.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use std 4 | use os 5 | 6 | func test_open_read(){ 7 | fmt.println("test open/read dir") 8 | 9 | fd = std.opendir("./dir") 10 | while true { 11 | file = fd.readdir() 12 | if !file break 13 | 14 | match file.name { 15 | "a" : fmt.assert(file.type,"file","a should be file") 16 | "b" : fmt.assert(file.type,"file","b should be file") 17 | "." : fmt.assert(file.type,"directory", ". should be directory") 18 | "..": fmt.assert(file.type,"directory",".. should be directory") 19 | _ : os.die("should be a,b,.,..") 20 | } 21 | fmt.println(file.name,file.type) 22 | } 23 | fmt.println("test open/read dir passed") 24 | } 25 | func test_tell(){ 26 | fmt.println("test is_dir && is_file") 27 | if !std.is_dir("./dir") { 28 | os.die("./dir should be directory") 29 | } 30 | if std.is_dir("./dir1") { 31 | os.die("./dir1 not exist") 32 | } 33 | if !std.is_file("./std_dir.tu") { 34 | os.die("./std_dir.tu is file") 35 | } 36 | if std.is_file("./std_dir.tu1") { 37 | os.die("./std_dir.tu1 not exist") 38 | } 39 | 40 | //test file or directory 41 | if std.is_dir("./std_dir.tu") { 42 | os.die("./std_dir.tu is file") 43 | } 44 | fmt.println("test is_dir && is_success") 45 | } 46 | func main(){ 47 | test_open_read() 48 | test_tell() 49 | } -------------------------------------------------------------------------------- /tests/internalpkg/std_file.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use std 3 | use os 4 | 5 | 6 | func test_open_read(filepath,fstr){ 7 | fmt.println("test open/read file:",filepath) 8 | file = new std.File(filepath) 9 | if !file.IsOpen() { 10 | os.die("open file failed") 11 | } 12 | 13 | //test size 14 | if file.Size() != std.len(fstr) { 15 | os.die("assert file body size failed" + file.Size() ) 16 | } 17 | body = file.ReadAll() 18 | fmt.println(std.len(body),std.len(fstr)) 19 | if body != fstr { 20 | fmt.println(body,fstr) 21 | os.die("file body assert failed") 22 | } 23 | fmt.println("test open/read file passed") 24 | } 25 | 26 | func main(){ 27 | fstr = "1111111111 28 | 2222222222 29 | 3333333333 30 | 4444444444 31 | 5555555555 32 | 6666666666 33 | 7777777777 34 | 8888888888 35 | 9999999999 36 | 10" 37 | test_open_read("./file/file.txt",fstr) 38 | } -------------------------------------------------------------------------------- /tests/internalpkg/std_regex.tu: -------------------------------------------------------------------------------- 1 | use std.regex 2 | use fmt 3 | 4 | func test_replace(){ 5 | fmt.println("test string regex replace") 6 | str = "/home/user/tu/file.tu" 7 | dst = "_home_user_tu_file.tu" 8 | if (ret = regex.replace(str,"/","_") ) != dst { 9 | os.die(ret + "should be dst") 10 | } 11 | fmt.println("test string regex success") 12 | 13 | } 14 | 15 | func main(){ 16 | test_replace() 17 | } -------------------------------------------------------------------------------- /tests/internalpkg/time_sleep.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use time 4 | 5 | 6 | func main(){ 7 | fmt.println("rt_time_sleep test") 8 | 9 | fmt.println("1s") 10 | //time.sleep(1) 11 | fmt.println("100 us") 12 | //time.usleep(100) 13 | fmt.println("1000000 us") 14 | //time.usleep(1000000) 15 | fmt.println("rt_time_sleep test passed!") 16 | } -------------------------------------------------------------------------------- /tests/memory/assign.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | mem Test 6 | { 7 | i8 a,b 8 | i16 c 9 | i16 arr[2] 10 | u32 d,e,f 11 | i64 g,h 12 | } 13 | func i8_assign(p){ 14 | p.a = 100 15 | //test arr 16 | fmt.assert(int(p.a),100) 17 | p.arr[0] = 1 18 | fmt.assert(int(p.arr[0]),1) 19 | p.arr[1] = 1000 20 | fmt.assert(int(p.arr[1]),1000) 21 | 22 | p.b = 100 23 | fmt.assert(int(p.b),100) 24 | fmt.println("test i8_assign successful") 25 | } 26 | func plus_agan(p){ 27 | p.a = 0 28 | for(i = 1; i < 10 ; i += 1){ 29 | p.a += 1 30 | fmt.assert(int(p.a),i) 31 | } 32 | //test arr 33 | p.arr[0] = 33 34 | p.arr[0] += 1 35 | fmt.assert(int(p.arr[0]),34) 36 | fmt.println(" pulus_agan success") 37 | } 38 | func main(){ 39 | p = new Test 40 | plus_agan(p) 41 | i8_assign(p) 42 | } -------------------------------------------------------------------------------- /tests/memory/byte.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | mem Byte{ 6 | i8 a1:1,a2:1,a3:1,a4:1,a5:1,a6:1,a7:1,a8:1 7 | } 8 | func assert(a,b,str){ 9 | if a != b { 10 | fmt.println(a + " != " + b ,str) 11 | os.exit(-1) 12 | } 13 | } 14 | func byte_assign(b){ 15 | fmt.println("byte_assign test") 16 | a = b 17 | // 1 0 0 0 0 0 0 0 => 1 18 | b.a1 = 1 19 | assert(1,int(*a),"test byte_assign failed u8 != 1") 20 | 21 | // 1 1 0 0 0 0 0 0 => 3 22 | b.a2 = 1 23 | assert(3,int(*a),"test byte_assign failed u8 != 3") 24 | 25 | // 1 1 1 0 0 0 0 0 => 7 26 | b.a3 = 1 27 | assert(7,int(*a),"test byte_assign failed u8 != 7") 28 | 29 | // 1 1 1 1 0 0 0 0 => 15 30 | b.a4 = 1 31 | assert(15,int(*a),"test byte_assign failed u8 != 15") 32 | 33 | // 1 1 1 1 1 0 0 0 => 31 34 | b.a5 = 1 35 | assert(31,int(*a),"test byte_assign failed u8 != 31") 36 | 37 | // 1 1 1 1 1 1 0 0 => 63 38 | b.a6 = 1 39 | assert(63,int(*a),"test byte_assign failed u8 != 63") 40 | 41 | // 1 1 1 1 1 1 1 0 => 127 42 | b.a7 = 1 43 | assert(127,int(*a),"test byte_assign failed u8 != 127") 44 | 45 | // 1 1 1 1 1 1 1 1 => 255 46 | b.a8 = 1 47 | assert(255,int(*a),"test byte_assign failed u8 != 255") 48 | 49 | fmt.println("test byte read write success") 50 | } 51 | 52 | func test_unsigned(b){ 53 | a = b 54 | assert(255,int(*a),"test failed u8 != 255") 55 | 56 | a2 = b 57 | assert(-1,int(*a2),"test failed i8 != -1") 58 | 59 | fmt.println("test byte unsigned success") 60 | } 61 | func main(){ 62 | b = new Byte 63 | byte_assign(b) 64 | test_unsigned(b) 65 | 66 | fmt.println("test byte success") 67 | 68 | } -------------------------------------------------------------------------------- /tests/memory/net/ip.tu: -------------------------------------------------------------------------------- 1 | 2 | 3 | mem Ip 4 | { 5 | u8 version:4 6 | u8 lhl:4 7 | u8 tos 8 | u16 totallen 9 | u16 identify 10 | u16 r:1 11 | u16 d:1 12 | u16 m:1 13 | u16 foffset:13 14 | u32 t1 15 | u64 t2 16 | } -------------------------------------------------------------------------------- /tests/memory/net/struct.tu: -------------------------------------------------------------------------------- 1 | mem Data { 2 | i8 a,b,c,d 3 | } -------------------------------------------------------------------------------- /tests/memory/op_div.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | mem Pointer{ 4 | i8 up 5 | i8 arr[2] 6 | } 7 | // i8测试 8 | func i8_test(){ 9 | fmt.println("i8 test") 10 | // var test 11 | t = 8 12 | t /= 10 13 | fmt.assert(int(t),0) 14 | t = 8 15 | t = t / 8 16 | fmt.assert(int(t),1) 17 | t = 8 18 | t = t / 7 19 | fmt.assert(int(t),1) 20 | fmt.println("i8 var test success") 21 | 22 | // member test 23 | p = new Pointer 24 | p.up = 8 25 | p.up /= 10 26 | fmt.assert(int(p.up),0,"8 != 8") 27 | p.up = 10 28 | p.up /= 10 29 | fmt.assert(int(p.up),1) 30 | p.up = 10 31 | p.up = p.up / 5 32 | fmt.assert(int(p.up),2) 33 | p.up = 8 34 | p.up = p.up / 2 35 | fmt.assert(int(p.up),4) 36 | 37 | p.arr[1] = 10 38 | p.arr[1] /= 5 39 | if p.arr[1] == 2 {} else os.die("p.arr[1] == 2") 40 | fmt.println("i8 test success") 41 | } 42 | 43 | mem Pu8{ 44 | u8 p 45 | u8 arr[2] 46 | } 47 | func u8_test(){ 48 | fmt.println("u8 test ") 49 | t = 200 50 | t /= 2 51 | fmt.assert(int(t),100) 52 | 53 | t = t / 5 54 | fmt.assert(int(t),20) 55 | 56 | t = 200 57 | t = t / 200 58 | fmt.assert(int(t),1) 59 | 60 | t2 = new Pu8 61 | 62 | t2.p = 200 63 | t2.p /= 200 64 | fmt.assert(int(t2.p),1) 65 | 66 | t2.p = 200 67 | t2.p = t2.p / 190 68 | fmt.assert(int(t2.p),1) 69 | 70 | t2.p = 200 71 | t2.p = t2.p / 4 72 | fmt.assert(int(t2.p),50) 73 | //test arr 74 | t2.arr[1] = 200 75 | t2.arr[1] /= 200 76 | if t2.arr[1] == 1 {} else os.die("t2.arr[1]==1") 77 | 78 | fmt.println("u8 test success") 79 | } 80 | func main(){ 81 | i8_test() 82 | u8_test() 83 | } -------------------------------------------------------------------------------- /tests/memory/op_mod.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | mem Pointer{ 4 | i8 up 5 | i8 arr[2] 6 | } 7 | // i8测试 8 | func i8_test(){ 9 | fmt.println("i8 test") 10 | // var test 11 | t = 8 12 | t %= 10 13 | fmt.assert(int(t),8) 14 | t = t % 8 15 | fmt.assert(int(t),0) 16 | t = 8 17 | t = t % 7 18 | fmt.assert(int(t),1) 19 | fmt.println("i8 var test success") 20 | 21 | // member test 22 | p = new Pointer 23 | p.up = 8 24 | p.up %= 10 25 | fmt.assert(int(p.up),8,"8 != 8") 26 | p.up %= 10 27 | fmt.assert(int(p.up),8) 28 | p.up = p.up % 8 29 | fmt.assert(int(p.up),0) 30 | p.up = 8 31 | p.up = p.up % 7 32 | fmt.assert(int(p.up),1) 33 | 34 | p.arr[0] = 8 35 | p.arr[0] %= 7 36 | if p.arr[0] == 1 {} else os.die("p.arr[0] == 1") 37 | fmt.println("i8 test success") 38 | } 39 | 40 | mem Pu8{ 41 | u8 p 42 | } 43 | func u8_test(){ 44 | fmt.println("u8 test ") 45 | t = 200 46 | t %= 201 47 | fmt.assert(int(t),200) 48 | 49 | t = t % 190 50 | fmt.assert(int(t),10) 51 | 52 | t = 200 53 | t = t % 200 54 | fmt.assert(int(t),0) 55 | 56 | t2 = new Pu8 57 | 58 | t2.p = 200 59 | t2.p %= 201 60 | fmt.assert(int(t2.p),200) 61 | 62 | t2.p = t2.p % 190 63 | fmt.assert(int(t2.p),10) 64 | 65 | t2.p = 200 66 | t2.p = t2.p % 200 67 | fmt.assert(int(t2.p),0) 68 | 69 | fmt.println("u8 test success") 70 | } 71 | func main(){ 72 | i8_test() 73 | u8_test() 74 | } -------------------------------------------------------------------------------- /tests/memory/op_mul.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | mem Test{ 4 | i8 a,b,c,d,e,f,g,h 5 | } 6 | mem Pointer{ 7 | i8* up 8 | } 9 | // i8测试 10 | func i8_test(){ 11 | p = new Test 12 | member = func(p){ 13 | pp = new Pointer 14 | pp.up = p 15 | for(i = 1 ; i <= 8 ; i += 1 ){ 16 | t = *i 17 | t = t * t 18 | fmt.assert(int(*pp.up),int(t)) 19 | pp.up += 1 20 | } 21 | fmt.println("test member Pointer + success") 22 | } 23 | // 测试 变量指针 + 24 | var = func(p){ 25 | p.a = 1 p.b = 4 p.c = 9 p.d = 16 p.e = 25 p.f = 36 p.g = 49 p.h = 64 26 | pp = p 27 | for(i = 1 ; i <= 8 ; i += 1 ){ 28 | t = *i 29 | t *= t 30 | fmt.assert(int(*pp),int(t)) 31 | pp += 1 32 | } 33 | fmt.println("test var Pointer + success") 34 | } 35 | var(p) 36 | member(p) 37 | } 38 | func main(){ 39 | i8_test() 40 | } -------------------------------------------------------------------------------- /tests/memory/op_plus.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | mem Test{ 4 | i8 a,b,c,d,e,f,g,h 5 | } 6 | mem Pointer{ 7 | i8* up 8 | } 9 | // i8测试 10 | func i8_test(){ 11 | p = new Test 12 | member = func(p){ 13 | pp = new Pointer 14 | pp.up = p 15 | for(i = 1 ; i <= 8 ; i += 1 ){ 16 | fmt.assert(int(*pp.up),i) 17 | pp.up += 1 18 | } 19 | fmt.println("test member pointer + success") 20 | } 21 | // 测试 变量指针 + 22 | var = func(p){ 23 | p.a = 1 p.b = 2 p.c = 3 p.d = 4 p.e = 5 p.f = 6 p.g = 7 p.h = 8 24 | pp = p 25 | for(i = 1 ; i <= 8 ; i += 1 ){ 26 | fmt.assert(int(*pp),i) 27 | pp += 1 28 | } 29 | fmt.println("test var pointer + success") 30 | } 31 | var(p) 32 | member(p) 33 | } 34 | 35 | mem Testu8{ 36 | u8 a,b,c,d,e,f,g,h 37 | } 38 | mem Pu8{ 39 | u8* p 40 | } 41 | func u8_test(){ 42 | p = new Testu8 43 | // 初始化值 u8 范围为 0 - 255 44 | pp = p 45 | for(i = 220 ; i <= 228 ; i += 1){ 46 | *pp = *i 47 | pp += 1 48 | } 49 | //FIXME: segfault 50 | member = func(p){ 51 | fmt.println("test member pointer") 52 | pp1 = new Pu8 53 | pp1.p = p 54 | for(i = 220 ; i <= 228 ; i += 1 ){ 55 | fmt.assert(int(*pp1.p),i) 56 | pp1.p = pp1.p + 1 57 | } 58 | fmt.println("test member pointer + success") 59 | } 60 | // 测试 变量指针 + 61 | var = func(p){ 62 | pp1 = p 63 | for(i = 220 ; i < 228 ; i += 1 ){ 64 | //FIXME: gc bug 65 | //b = int(*i) 66 | fmt.assert(int(*pp1),i) 67 | pp1 += 1 68 | } 69 | fmt.println("test var pointer + success") 70 | } 71 | var(p) 72 | member(p) 73 | } 74 | func main(){ 75 | i8_test() 76 | //u8_test() 77 | } -------------------------------------------------------------------------------- /tests/memory/statement.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use runtime 4 | use os 5 | 6 | func test_map(){ 7 | fmt.println("range for: test map") 8 | map = {"a":"b","c":"d"} 9 | for(k,v : map){ 10 | fmt.printf("[%s]:%s\n",k,v) 11 | } 12 | fmt.println("range for: test map success") 13 | return map 14 | } 15 | func test_range_for(){ 16 | fmt.println("range for: test array") 17 | map = test_map() 18 | arr = ["1",map,2,"sdfdsf","3"] 19 | for(i,v : arr){ 20 | if std.is_map(v) { 21 | for(k,v2 : v){ 22 | fmt.print("\t",k,"-",v2,"\n") 23 | } 24 | break 25 | }else{ 26 | fmt.print(i,"-",v,"\n") 27 | } 28 | } 29 | fmt.println("range for: test array success") 30 | } 31 | 32 | func test_while(){ 33 | a = 10 34 | b = 9 35 | 36 | while a != b { 37 | fmt.println(int(a),int(b)) 38 | b += 1 39 | } 40 | } 41 | mem T1 { 42 | i8 a 43 | i32 b 44 | } 45 | func test_new(){ 46 | fmt.println("test new expression") 47 | // 1. new struct 48 | a = new T1 49 | a.b = 1000 50 | if a.b != 1000 { 51 | fmt.println("test new mem failed") 52 | os.exit(-1) 53 | } 54 | // 2. new var(var must be memory type) 55 | b = 8 56 | bp = new b 57 | *bp = 10000 58 | if *bp != 10000 { 59 | fmt.println("test new var failed") 60 | os.exit(-1) 61 | } 62 | 63 | // 3. new num 64 | c = new 8 65 | *c = 10000 66 | if *c != 10000 { 67 | fmt.println("test new 8 failed") 68 | os.exit(-1) 69 | } 70 | fmt.println("test new expression end") 71 | } 72 | func main(){ 73 | test_range_for() 74 | test_while() 75 | test_new() 76 | } -------------------------------------------------------------------------------- /tests/memory/struct.tu: -------------------------------------------------------------------------------- 1 | 2 | use net 3 | use fmt 4 | use os 5 | 6 | mem Ip 7 | { 8 | u32 a 9 | u16 b,c 10 | } 11 | mem Ip2{ 12 | u16 a[3] 13 | u16 b 14 | } 15 | func test2(){ 16 | v = new Ip 17 | v.c = 100 18 | v2 = v 19 | if v2.b != 100 { 20 | fmt.println("v2.b should == 100 ",v2.b) 21 | os.exit(-1) 22 | } 23 | fmt.println("test member arr success") 24 | } 25 | func main(){ 26 | test1() 27 | test2() 28 | } 29 | 30 | // 测试内存读写,映射、转换 31 | func test1() 32 | { 33 | // 申请一份sizeof(net.Ip) 的gc内存 34 | p = new net.Ip 35 | p.identify = 10000 36 | fmt.assert(int(p.identify),10000) 37 | 38 | // 位的读写 39 | p.r = 1 40 | p.d = 1 41 | p.m = 1 42 | p.foffset = 3 43 | fmt.assert(int(p.foffset),3) 44 | // 重新映射一份结构 测试转换 45 | p2 = p 46 | // u16 r:1 47 | // u16 d:1 48 | // u16 m:1 49 | // u16 foffset:13 50 | // 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 == 31 51 | fmt.assert(int(p2.c),31) 52 | 53 | fmt.println(int(p2.a),int(p2.b),int(p2.c)) 54 | fmt.println("test mem successful") 55 | } -------------------------------------------------------------------------------- /tests/mixed/chain.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | enum { 4 | Normal 5 | Error 6 | } 7 | class A { 8 | next 9 | type = false 10 | native 11 | } 12 | func test_chain(){ 13 | //init 14 | obj = new A() 15 | obj.next = new A() 16 | obj.next.native = Error 17 | // dyn && native => native 18 | if obj.next.type == false && obj.next.native == Error {} else { 19 | os.die("should be true") 20 | } 21 | obj.next.type = Error // test wheather coredump in next right expression 22 | // native || dyn(if load instruct at here will coredump) 23 | if obj.next.native == Error || obj.next.type == false {} else { 24 | os.die("never happen at here") //cos obj.next.type will coredump 25 | } 26 | fmt.println("dyn with native in chain test success") 27 | 28 | } 29 | func main(){ 30 | test_chain() 31 | } -------------------------------------------------------------------------------- /tests/mixed/logor_and.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | func retfalse(){ 5 | return false 6 | } 7 | func rettrue(){ 8 | return true 9 | } 10 | //test && || 11 | func test_log_and_or(){ 12 | b = null 13 | c = 10 14 | // native type 15 | // next expression will be handled by special native compiler 16 | // b == null => dyn type 17 | // c == 10 => native type 18 | // b == null && c == 10 => native type 19 | if b == null && c == 10 {} else { 20 | os.die("b should be null") 21 | } 22 | if b != null && c == 10 { 23 | os.die("b should not be null") 24 | } 25 | if !retfalse() && c == 10 {} else { 26 | os.die("!fasle should be true") 27 | } 28 | 29 | if b == null || c == 11 {} else { 30 | os.die("b should be null") 31 | } 32 | if b != null || c == 11 { 33 | os.die("b should not be null") 34 | } 35 | fmt.println("test log_and_or success") 36 | } 37 | 38 | func main(){ 39 | test_log_and_or() 40 | } -------------------------------------------------------------------------------- /tests/mixed/typeassert_basetype.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | func test_int(){ 4 | num = 376.(i8) 5 | _tmp = 376 6 | 7 | if _tmp == num {} else os.die("num should be 376 static") 8 | 9 | if int(num) == 376 {} else os.die("num should be 376") 10 | 11 | fc = func(v){ 12 | if v == 444 {} else os.die("v should b 444") 13 | } 14 | fc(444.(i32)) 15 | fmt.println("test_int success") 16 | } 17 | func test_char(){ 18 | c = 'd'.(i8) 19 | _t = 'd' 20 | if _t == c {} else os.die("c != d") 21 | 22 | fc = func(v){ 23 | if v == 'x' {} else os.die(" v != x") 24 | } 25 | fc('x'.(i8)) 26 | fmt.println("test_char success") 27 | } 28 | func test_string(){ 29 | str = "45ss54".(i8) 30 | if string.new(str) == "45ss54" {} else os.die("str != 45ss54") 31 | fmt.println("test string success") 32 | } 33 | func main(){ 34 | test_int() 35 | test_char() 36 | test_string() 37 | } -------------------------------------------------------------------------------- /tests/mixed/typeassert_chain.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | mem Value{ 4 | i64 type 5 | i64 data 6 | } 7 | mem Str{ 8 | i8* a 9 | i32 b 10 | i32 c 11 | } 12 | Str::test(){ 13 | fmt.println(int(this.b)) 14 | return this.b 15 | } 16 | func test(v){ 17 | ret = v.data.(Str).test() 18 | if ret == 10 {} else os.die(" v.data.test() != 10") 19 | 20 | if v.data.(Str).test() == ret {} else os.die("v.data.test != 10") 21 | 22 | v.data.(Str).c = 333 23 | ret = 333 24 | if v.data.(Str).c == 333 {} else os.die("v.data.c != 333") 25 | fmt.println("test success") 26 | } 27 | 28 | func main(){ 29 | b = new Value{ 30 | data: new Str{ 31 | b: 10 32 | } 33 | } 34 | test(b) 35 | } -------------------------------------------------------------------------------- /tests/mixed/typeassert_staticmember.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use string 3 | 4 | mem String { 5 | i8* inner 6 | } 7 | String::putc(c){ 8 | this.inner = this.inner.(string.Str).putc(c) 9 | } 10 | String::str(){ 11 | return this.inner 12 | } 13 | 14 | func test_S2(){ 15 | a = new String { 16 | inner : string.empty() 17 | } 18 | b = 'c' 19 | a.putc(b) 20 | a.putc(b) 21 | a.putc(b) 22 | fmt.println(string.new(a.str())) 23 | if string.new(a.str()) == "ccc" {} else os.die("String.putc != ccc") 24 | fmt.println("test String.putc success") 25 | } 26 | 27 | func test_S1(){ 28 | // s = t.string() 29 | s = string.empty() 30 | b = 'c' 31 | s = s.(string.Str).putc(b) 32 | s = s.(string.Str).putc(b) 33 | s = s.(string.Str).putc(b) 34 | 35 | if string.new(s) == "ccc" {} else os.die(" ! ccc") 36 | fmt.println("test Str.putc success") 37 | } 38 | 39 | func main(){ 40 | test_S1() 41 | test_S2() 42 | } -------------------------------------------------------------------------------- /tests/native/base_op.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | gvar 4 | mem T1 { 5 | i8 a,b,d 6 | i8 c[3] 7 | } 8 | p = &gvar 9 | //1. test reference global stack 10 | //2. test global var stack 11 | func test_ref_global_var(){ 12 | //test reference global stack var gvar 13 | p2 = &gvar 14 | p2.d = 11 15 | p2.c[0] = 22 16 | p2.c[1] = 33 17 | p2.c[2] = 44 18 | 19 | if p2.d == 11 {} else os.die("p2.d != 11") 20 | if p2.c[0] == 22 {} else os.die("p2.c[0] != 22") 21 | if p2.c[1] == 33 {} else os.die("p2.c[0] != 33") 22 | if p2.c[2] == 44 {} else os.die("p2.c[0] != 44") 23 | //test global p 24 | if p.d == 11 {} else os.die("p.d != 11") 25 | if p.c[0] == 22 {} else os.die("p.c[0] != 22") 26 | if p.c[1] == 33 {} else os.die("p.c[0] != 33") 27 | if p.c[2] == 44 {} else os.die("p.c[0] != 44") 28 | 29 | fmt.println("test ref global var&& global stack var success") 30 | } 31 | func test_i8_i32_mixed(){ 32 | a = -5 33 | if a < 0 {} else os.die("-5 < 0") 34 | b = 5 35 | c = 14 36 | r = a + ( b % c) 37 | if r == 0 {} else os.die(" -5 + ( 5 % 15) != 0") 38 | fmt.println("test_i_i32_mixed success") 39 | } 40 | 41 | func main(){ 42 | test_ref_global_var() 43 | test_i8_i32_mixed() 44 | } -------------------------------------------------------------------------------- /tests/native/mem_chain_func.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | mem Test{ 5 | i64 a,b 6 | Inner plain 7 | Inner* pointer 8 | } 9 | mem Inner { 10 | i32 a,b 11 | i8 c,d 12 | i64 e 13 | } 14 | Inner::pointer(){ 15 | if this.a != 100 os.die("this.a != 100") 16 | if this.d != 20 os.die("this.d != 20") 17 | return this.e 18 | } 19 | Inner::plain(){ 20 | if this.a != 100 os.die("this.a != 100") 21 | if this.d != 20 os.die("this.d != 20") 22 | return this.e 23 | } 24 | func test_plan_member(var){ 25 | var.plain.a = 100 26 | var.plain.d = 20 27 | var.plain.e = 300 28 | ret = var.plain.plain() 29 | if ret != 300 os.die("plain ret != 300") 30 | fmt.println("test plan member sucess") 31 | } 32 | func test_pointer_member(var){ 33 | var.pointer = new Inner 34 | var.pointer.a = 100 35 | var.pointer.d = 20 36 | var.pointer.e = 400 37 | ret = var.pointer.pointer() 38 | if ret != 400 os.die("pointer ret != 300") 39 | fmt.println("test pointer member sucess") 40 | } 41 | func main(){ 42 | var = new Test{} 43 | test_pointer_member(var) 44 | test_plan_member(var) 45 | } -------------------------------------------------------------------------------- /tests/native/memclass.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | mem Test { 5 | i32 a 6 | i32 b,c 7 | i8* arr8 8 | i16* arr16 9 | i32* arr32 10 | i64* arr64 11 | i32 stack[3] 12 | } 13 | Test::value(){ 14 | return this.b 15 | } 16 | Test::test(com){ 17 | if this.a != 10 os.die("this.a != 10") 18 | if this.b != 20 os.die("this.a != 20") 19 | ret = this.value() 20 | if ret != 20 os.die("this.b != 20") 21 | this.arr8[0] = 13 22 | this.arr8[1] = 14 23 | if this.arr8[0] != 13 os.die("this.arr8[0] != 13") 24 | if this.arr8[1] != 14 os.die("this.arr8[1] != 14") 25 | 26 | if this.stack[1] != 2 os.die("this.stack[1] != 2") 27 | fmt.printf("test mem class by '%s' success \n",com) 28 | } 29 | func main(){ 30 | var = new Test { 31 | a : 10, 32 | b : 20, 33 | arr8: new i8[2], 34 | stack: [1,2,3], 35 | } 36 | var.test("test heap") 37 | //stack test 38 | var2 = null 39 | var2.a = 10 40 | var2.b = 20 41 | var2.arr8 = new i8[2] 42 | var2.stack[0] = 1 43 | var2.stack[1] = 2 44 | var2.stack[2] = 3 45 | var2.test("test stack") 46 | 47 | } -------------------------------------------------------------------------------- /tests/native/pkg/a.tu: -------------------------------------------------------------------------------- 1 | use os 2 | use fmt 3 | mem T2 { 4 | i8 a 5 | i32 b 6 | i64 c 7 | } 8 | T2::test(){ 9 | if this.b == 22 {} else os.die("this.b == 22") 10 | fmt.println("test t2 success") 11 | } 12 | mem T1 { 13 | i32 a 14 | i64 b[3] 15 | T2* c 16 | T2 d 17 | } 18 | 19 | gvar = new T1 { 20 | a : 1, 21 | b : [31,32,33], 22 | c : 3, 23 | d : T2 { 24 | a : 11, 25 | b : 22, 26 | c : 33 27 | } 28 | } 29 | T1::test(){ 30 | if this.a == 1 {} else os.die("this.a == 1") 31 | if this.b[0] == 31 {} else os.die("this.b[0] == 31") 32 | if this.b[1] == 32 {} else os.die("this.b[1] == 32") 33 | if this.b[2] == 33 {} else os.die("this.b[2] == 33") 34 | 35 | if this.d.a == 11 {} else os.die("this.d.a == 11") 36 | if this.d.b == 22 {} else os.die("this.d.a == 22") 37 | if this.d.c == 33 {} else os.die("this.d.a == 33") 38 | 39 | //test overflow 40 | this.b[3] = 35 41 | if this.c == 35 {} else os.die("this.c == 34") 42 | //test t2 43 | fmt.println("T1::test success") 44 | this.d.test() 45 | } -------------------------------------------------------------------------------- /tests/native/pkg2/a.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | 5 | mem B { 6 | i64 a,b 7 | } 8 | B::init(){ 9 | if this.a == 20 && this.b == 30 {} else { 10 | os.die("B::init something wrong here") 11 | } 12 | } 13 | mem A { 14 | i64 a 15 | B b 16 | i64 c 17 | } 18 | A::init(){ 19 | if this.a == 10 && this.c == 40 {} else { 20 | os.die("A::init somthing wrong") 21 | } 22 | this.b.init() 23 | } 24 | 25 | g = new A{ 26 | a : 10, 27 | b : B { 28 | a : 20, 29 | b : 30 30 | }, 31 | c : 40 32 | } 33 | 34 | -------------------------------------------------------------------------------- /tests/native/pkg2/b.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | func test(){ 3 | g.init() 4 | fmt.println("pkg2 test success") 5 | } -------------------------------------------------------------------------------- /tests/native/stack_local_arr.tu: -------------------------------------------------------------------------------- 1 | use os 2 | use fmt 3 | 4 | func test_var(){ 5 | ga = [ 6 | 0, 1, 2, 7, 3, 13, 8, 19, 7 | 4, 25, 14, 28, 9, 34, 20, 40, 8 | 5, 17, 26, 38, 15, 46, 29, 48, 9 | 10, 31, 35, 54, 21, 50, 41, 57, 10 | 63, 6, 12, 18, 24, 27, 33, 39, 11 | 16, 37, 45, 47, 30, 53, 49, 56, 12 | 62, 11, 23, 32, 36, 44, 52, 55, 13 | 61, 22, 43, 51, 60, 42, 59, 58, 14 | ] 15 | ga[14] = 41 16 | if ga[14] == 41 {} else os.die("ga[14] != 41") 17 | if ga[0] == 0 {} else os.die("ga[0] != 0") 18 | if ga[1] == 1 {} else os.die("ga[1] != 1") 19 | if ga[7] == 19 {} else os.die("ga[7] != 19") 20 | if ga[15] == 40 {} else os.die("ga[15] != 40") 21 | if ga[39] == 39 {} else os.die("ga[39] != 39") 22 | if ga[55] == 55 {} else os.die("ga[55] != 55") 23 | fmt.println("test_var success") 24 | } 25 | func test_var_ref(t){ 26 | t[14] = 100 27 | if t[14] == 100 {} else os.die("t[14] != 100") 28 | if t[0] == 0 {} else os.die("t[0] != 0") 29 | if t[1] == 1 {} else os.die("t[1] != 1") 30 | if t[7] == 19 {} else os.die("t[7] != 19") 31 | if t[15] == 40 {} else os.die("t[15] != 40") 32 | if t[39] == 39 {} else os.die("t[39] != 39") 33 | if t[55] == 55 {} else os.die("t[55] != 55") 34 | fmt.println("test_var_ref success") 35 | } 36 | 37 | func main(){ 38 | ga = [ 39 | 0, 1, 2, 7, 3, 13, 8, 19, 40 | 4, 25, 14, 28, 9, 34, 20, 40, 41 | 5, 17, 26, 38, 15, 46, 29, 48, 42 | 10, 31, 35, 54, 21, 50, 41, 57, 43 | 63, 6, 12, 18, 24, 27, 33, 39, 44 | 16, 37, 45, 47, 30, 53, 49, 56, 45 | 62, 11, 23, 32, 36, 44, 52, 55, 46 | 61, 22, 43, 51, 60, 42, 59, 58, 47 | ] 48 | test_var() 49 | test_var_ref(&ga) 50 | } -------------------------------------------------------------------------------- /tests/native/std_str.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use std 3 | use string 4 | 5 | func test_unsigned_longint(){ 6 | longint = string.S( 7 | *"11887393157837578923" 8 | ) 9 | fmt.println(longint.dyn()) 10 | number = std.strtoul(longint.str(),0.(i8),10.(i8)) 11 | 12 | dstlongint = string.S( 13 | string.fromulonglong( 14 | number 15 | ) 16 | ) 17 | fmt.println(longint.dyn()) 18 | //compare 19 | if longint.cmp(dstlongint) == string.Equal {} else { 20 | os.die("long int not equal") 21 | } 22 | fmt.println("test unsigned_longint success") 23 | } 24 | 25 | func main(){ 26 | test_unsigned_longint() 27 | } -------------------------------------------------------------------------------- /tests/native/string.putc.tu: -------------------------------------------------------------------------------- 1 | use std 2 | use string 3 | Null = null 4 | 5 | func test_puchar_native(){ 6 | s = string.empty() 7 | l = s.(string.Str).len() 8 | if l != 0 { 9 | os.die("len should be 0") 10 | } 11 | //put 10 char 12 | p = "0123456789" 13 | for i = 0 ; i < 10 ; i += 1 { 14 | s = s.(string.Str).putc(*p) 15 | p += 1 16 | } 17 | cc = '\\' 18 | s = s.(string.Str).putc(cc) 19 | l = s.(string.Str).len() 20 | if l != 11 { 21 | os.panic("l should be 10") 22 | } 23 | if std.strcmp(s,*"0123456789\\") != Null { 24 | os.die("s should be 0123456789\\") 25 | } 26 | fmt.println("test native put char success ") 27 | } 28 | func test_putchar_dyn(){ 29 | b = "test" 30 | b += '\\' 31 | b += '9' 32 | b += 's' 33 | if b != "test\\9s" { 34 | os.die("b should be test\\9s") 35 | } 36 | fmt.printf("test dyn put char success %s\n",b) 37 | } 38 | func main(){ 39 | test_puchar_native() 40 | test_putchar_dyn() 41 | } -------------------------------------------------------------------------------- /tests/native/t/a.tu: -------------------------------------------------------------------------------- 1 | ge = [ 2 | 0, 1, 2, 7, 3, 13, 8, 19, 3 | 4, 25, 14, 28, 9, 34, 20, 40, 4 | 5, 17, 26, 38, 15, 46, 29, 48, 5 | 10, 31, 35, 54, 21, 50, 41, 57, 6 | 63, 6, 12, 18, 24, 27, 33, 39, 7 | 16, 37, 45, 47, 30, 53, 49, 56, 8 | 62, 11, 23, 32, 36, 44, 52, 55, 9 | 61, 22, 43, 51, 60, 42, 59, 58, 10 | ] -------------------------------------------------------------------------------- /tests/native/t/define.tu: -------------------------------------------------------------------------------- 1 | ten = 10 2 | 3 | 4 | seven = 7 -------------------------------------------------------------------------------- /tests/operator/bitand_agn.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(){ 6 | a = 10 7 | b = 10 8 | a &= b 9 | if a != 10 { 10 | fmt.printf("test int bitand %d != 10 failed\n",a) 11 | os.exit(1) 12 | } 13 | c = 0 14 | a &= c 15 | if a != 0 { 16 | fmt.printf("test int bitand %d != 0 failed\n",a) 17 | os.exit(1) 18 | } 19 | // 10 & 2 = 2 20 | // e &= 2 21 | // if e != 2 { 22 | // fmt.printf("test int bitand %d != 2 failed\n",e) 23 | // os.exit(1) 24 | // } 25 | fmt.printf("test int bitand %d success\n",a) 26 | } 27 | func test_string(){ 28 | a = "abc" 29 | a &= 1 30 | if a != 0 { 31 | fmt.printf("test string bitand %s != 0 failed\n",a) 32 | os.exit(1) 33 | } 34 | a &= "sdfdsf" 35 | if a != 0 { 36 | fmt.printf("test string bitand %s != 0 failed\n",a) 37 | os.exit(1) 38 | } 39 | fmt.printf("test string bitand %d success\n",a) 40 | } 41 | func test_char(){ 42 | fmt.println("test_char agn") 43 | //test char & char => int 44 | c1 = 'd' 45 | c1 &= 'D' 46 | v = 100 & 68 47 | if c1 == v {} else { 48 | os.panic("c1:%d should be %d",c1,v) 49 | } 50 | //char & string => unsupport 51 | //char to int => int 52 | c1 = 'd' 53 | c1 &= 68 54 | if c1 == v {} else { 55 | os.panic("c1:%d should be %d",c1,v) 56 | } 57 | c1 = 68 58 | c1 &= 'd' 59 | if c1 == v {} else { 60 | os.panic("c1:%d should be %d",c1,v) 61 | } 62 | fmt.println("test_char agn success") 63 | } 64 | func main(){ 65 | test_int() 66 | test_string() 67 | test_char() 68 | } -------------------------------------------------------------------------------- /tests/operator/div_agn.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(){ 6 | a = 10 7 | b = 10 8 | a /= b 9 | if a != 1 { 10 | fmt.printf("test int div %d != 1 failed\n",a) 11 | os.exit(1) 12 | } 13 | a = 10 14 | a /= 2 15 | if a != 5 { 16 | fmt.printf("test int div %d != 5 failed\n",a) 17 | os.exit(1) 18 | } 19 | // e /= 25 20 | // if e != 25 { 21 | // fmt.printf("test int div %d != 25 failed\n",e) 22 | // os.exit(1) 23 | // } 24 | fmt.printf("test int div %d success\n",a) 25 | } 26 | // 对字符串做运算不做任何操作 都返回0 27 | func test_string(){ 28 | a = "abc" 29 | a /= 1 30 | if a != 0 { 31 | fmt.printf("test string div %d != 0 failed\n",a) 32 | os.exit(1) 33 | } 34 | // e /= "abc" 35 | // if e != "abc" { 36 | // fmt.printf("test string div %s != abc failed\n",e) 37 | // os.exit(1) 38 | // } 39 | fmt.printf("test string div %d success\n",a) 40 | } 41 | func test_char2_div(){ 42 | fmt.println("test_char2_div ") 43 | //test char & char => int 44 | c1 = 'd' 45 | c1 /= 'D' 46 | v = 100 / 68 47 | if c1 == v {} else { 48 | os.panic("c1:%d should be %d",c1,v) 49 | } 50 | //char & string => unsupport 51 | //char to int => int 52 | c1 = 'd' 53 | c1 /= 68 54 | if c1 == v {} else { 55 | os.panic("c1:%d should be %d",c1,v) 56 | } 57 | c1 = 68 58 | c1 /= 'd' 59 | if c1 == 68 / 100 {} else { 60 | os.panic("c1:%d should be %d",c1,v) 61 | } 62 | fmt.println("test_char2_div success") 63 | } 64 | // 除法测试 65 | func main(){ 66 | test_int() 67 | test_string() 68 | test_char2_div() 69 | } -------------------------------------------------------------------------------- /tests/operator/equal.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | //测试 值比较 6 | func test_value_equal(){ 7 | fmt.println("test value equal...\n") 8 | if 10 == 10 { 9 | fmt.println("test 1 == 1 sucess...\n") 10 | }else{ 11 | fmt.println("test 1 == 1 failed...\n") 12 | os.exit(1) 13 | } 14 | } 15 | //测试 变量比较 16 | func test_variable_equal(){ 17 | fmt.println("test variable equal...\n") 18 | a = 10 19 | abc = 12 20 | b = 10 21 | if a == b { 22 | fmt.println("test a(10) == b(10) sucess...\n") 23 | }else{ 24 | fmt.println("test a(10) == b(10) failed...\n") 25 | os.exit(1) 26 | } 27 | 28 | b = 12 29 | if a == b { 30 | fmt.println("test a(10) == b(12) failed...\n") 31 | os.exit(1) 32 | }else{ 33 | fmt.println("test a(10) == b(12) success...\n") 34 | } 35 | } 36 | func test_char2_equal(){ 37 | fmt.println("test_char2_equal ") 38 | //test char & char => int 39 | c1 = 'd' 40 | v = 'd' 41 | if c1 == v {} else { 42 | os.panic("c1:%d should be %d",c1,v) 43 | } 44 | //char & string => unsupport 45 | //char to int => int 46 | if c1 == 100 {} else { 47 | os.panic("c1:%d should be %d",c1,100) 48 | } 49 | if 100 == c1 {} else { 50 | os.panic("c1:%d should be %d",c1,100) 51 | } 52 | fmt.println("test_char2_equal success") 53 | } 54 | func main(){ 55 | test_value_equal() 56 | test_variable_equal() 57 | test_char2_equal() 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /tests/operator/equal_not.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | //测试 值比较 6 | func test_int_notequal(num){ 7 | fmt.println("test int var not equal...\n") 8 | if num != 100 { 9 | fmt.printf("test %d != 100 failed...\n",num) 10 | os.exit(1) 11 | }else{ 12 | fmt.printf("test %d != 100 success...\n",num) 13 | } 14 | if num != 10 { 15 | fmt.printf("test %d != 10 success...\n",num) 16 | }else{ 17 | fmt.printf("test %d != 10 failed...\n",num) 18 | os.exit(1) 19 | } 20 | } 21 | 22 | func test_string_notequal(str){ 23 | fmt.println("test string var not equal...\n") 24 | 25 | if str != "notequal" { 26 | fmt.printf("test %s != notequal failed...\n",str) 27 | os.exit(1) 28 | }else{ 29 | fmt.printf("test %s != notequal success...\n",str) 30 | } 31 | 32 | if str != 100 { 33 | fmt.printf("test %s != 100 success...\n",str) 34 | }else{ 35 | fmt.printf("test %s != 100 success...\n",str) 36 | os.exit(1) 37 | } 38 | } 39 | func test_char2_notequal(){ 40 | fmt.println("test_char2_notequal ") 41 | //test char & char => int 42 | c1 = 'd' 43 | v = 'd' 44 | if c1 != v { 45 | os.panic("c1:%d should be %d",c1,v) 46 | } 47 | //char & string => unsupport 48 | //char to int => int 49 | if c1 != 100 { 50 | os.panic("c1:%d should be %d",c1,100) 51 | } 52 | if 100 != c1 { 53 | os.panic("c1:%d should be %d",c1,100) 54 | } 55 | fmt.println("test_char2_notequal success") 56 | } 57 | func main(){ 58 | //测试 数字 59 | test_int_notequal(100) 60 | //测试字符串 61 | test_string_notequal("notequal") 62 | test_char2_notequal() 63 | } 64 | 65 | -------------------------------------------------------------------------------- /tests/operator/minus.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(){ 6 | a = 10 7 | if a != 10 { 8 | os.exit(1) 9 | } 10 | a = a - 1 11 | if a != 9 { 12 | fmt.printf("test int minus %d != 9 failed\n",a) 13 | os.exit(1) 14 | } 15 | fmt.printf("test int minus %d != 9 success\n",a) 16 | } 17 | // 对字符串做运算不做任何操作 18 | func test_string(){ 19 | a = "abc" 20 | a = a - 1 21 | if a != "abc" { 22 | fmt.printf("test string minus %s != abc failed\n",a) 23 | os.exit(1) 24 | } 25 | fmt.printf("test string minus %s != abc success\n",a) 26 | } 27 | 28 | func test_intp(str){ 29 | if str != "test-1" { 30 | fmt.printf("test string minus %s != abc failed\n",str) 31 | os.exit(1) 32 | } 33 | fmt.printf("test string minus %s != abc success\n",str) 34 | } 35 | func test_char2_minus(){ 36 | fmt.println("test_char2_minus") 37 | //test char - char => int 38 | c1 = 'd' # 100 39 | c2 = 'D' # 68 40 | if c1 - c2 == 32 {} else { 41 | os.panic("c1-c2:%d should be 32",c1 - c2) 42 | } 43 | //char - int => int 44 | if 100 - 'D' == 32 {} else { 45 | os.panic("100-D:%d should be 32",100 - 'D') 46 | } 47 | if 'D' - 100 == -32 {} else { 48 | os.panic("D - 100:%d should be -32",'D' - 100) 49 | } 50 | fmt.println("test_char2_minus success") 51 | } 52 | // 注意目前 减运算 需要留空格 53 | // a = b - 1 correct 54 | // a = b -1 incorrect 55 | func main(){ 56 | test_int() 57 | test_string() 58 | test_intp("test" + -1) 59 | test_char2_minus() 60 | } -------------------------------------------------------------------------------- /tests/operator/minus_agn.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(){ 6 | a = 10 7 | if a != 10 { 8 | os.exit(1) 9 | } 10 | a -= 1 11 | if a != 9 { 12 | fmt.printf("test int minus agn %d != 9 failed\n",a) 13 | os.exit(1) 14 | } 15 | fmt.printf("test int minus agn %d != 9 success\n",a) 16 | } 17 | // 对字符串做运算不做任何操作 18 | func test_string(){ 19 | a = "abc" 20 | a -= 1 21 | if a != "abc" { 22 | fmt.printf("test string minus agn %s != abc failed\n",a) 23 | os.exit(1) 24 | } 25 | fmt.printf("test string minus agn %s != abc success\n",a) 26 | } 27 | func test_char2_minus_agn(){ 28 | fmt.println("test_char2_minus agn ") 29 | //test char - char => int 30 | c1 = 'd' # 100 31 | c1 -= 'D' # 68 32 | 33 | if c1 == 32 {} else { 34 | os.panic("c1-c2:%d should be 32",c1) 35 | } 36 | //char - int => int 37 | c1 = 100 38 | c1 -= 'D' 39 | if c1 == 32 {} else { 40 | os.panic("100-D:%d should be 32",c1) 41 | } 42 | c1 = 'D' 43 | c1 -= 100 44 | if c1 == -32 {} else { 45 | os.panic("D - 100:%d should be -32",c1) 46 | } 47 | fmt.println("test_char2_minus agn success") 48 | } 49 | // 注意目前 减运算 需要留空格 50 | func main(){ 51 | test_int() 52 | test_string() 53 | test_char2_minus_agn() 54 | } -------------------------------------------------------------------------------- /tests/operator/minus_or_negative.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(){ 6 | fmt.println("test int minus or negative") 7 | 8 | a = 1 + -1 9 | if a != 0 { 10 | fmt.println("test 1 -1 != 0 failed") 11 | os.exit(1) 12 | } 13 | a = 1 - 1 14 | if a != 0 { 15 | fmt.println("test 1 - 1 != 0 failed") 16 | os.exit(1) 17 | } 18 | 19 | a = 1 + 1 20 | if a != 2 { 21 | fmt.println("test 1 1 != 2 failed") 22 | os.exit(1) 23 | } 24 | fmt.println("test int passed!") 25 | } 26 | func test_string(){ 27 | fmt.println("test string add operate by compiler") 28 | a = 100 + "test" 29 | if a != "100test" { 30 | fmt.println("test 100 test != 100test failed") 31 | os.exit(1) 32 | } 33 | fmt.println("test string add operate by compiler passed") 34 | } 35 | func test_arg(str){ 36 | fmt.print("test string arg:",str,"\n") 37 | if str != "3str1str2str34str4" { 38 | fmt.println("test str arg failed! str:",str) 39 | os.exit(1) 40 | } 41 | fmt.println("test string arg passed") 42 | 43 | } 44 | 45 | 46 | func main(){ 47 | test_int() 48 | test_string() 49 | test_arg(1 + 2 + "str1" + "str2" + "str3" + 4 + "str4") 50 | } -------------------------------------------------------------------------------- /tests/operator/precedence.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | func gen100(){ 4 | return 100 5 | } 6 | func test_base(){ 7 | fmt.println("test base") 8 | a = 3 b = 4 9 | // test (n + n1) * n2 10 | c = ( a + b) * 5 11 | fmt.assert(c , 35," (3 + 4) * 5 should be 35") 12 | c = a + b * 5 13 | fmt.assert(c , 23," 3 + 4 * 5 should be 23") 14 | // mulit complex 15 | d = ( (a+b) * 5) * ((10 - a) * b) 16 | //d = ( (3+4)*5) * ( (10 - 3) * 4)) 17 | //d = ( 35) * 28 = 980 18 | fmt.assert(d , 980,"d should be 980") 19 | 20 | if (a = gen100()) && a == 100 { 21 | 22 | }else{ 23 | os.die("a should be 100") 24 | } 25 | 26 | fmt.println("test base successfully") 27 | } 28 | 29 | func test_if(){ 30 | fmt.println("test if") 31 | a = 1 32 | b = 2 33 | if ( a == 1 || b == 3) && (a == 3 || b == 2) { 34 | }else{ 35 | os.die("should be true") 36 | } 37 | 38 | fmt.println("test if successfully") 39 | } 40 | func test_while(){ 41 | fmt.println("test while") 42 | c = 'b' 43 | count = 1 44 | while ( 45 | (c >= 'a' && c <= 'z') || 46 | (c >= 'A' && c <= 'Z') || 47 | (c >= '0' && c <= '9') ){ 48 | count += 1 49 | c = '2' 50 | if count == 3 break 51 | } 52 | if count != 3 { 53 | os.die("count should be 3") 54 | } 55 | if c != '2' { 56 | os.die("c should be 2") 57 | } 58 | 59 | fmt.println("test while successfully") 60 | } 61 | func test_for(){ 62 | fmt.println("test for ") 63 | 64 | b = 5 65 | for(i = 0 ; (i <= 3 || b == 1) && (i >= 5) ; i +=1 ) { 66 | os.die("should not be here") 67 | } 68 | fmt.println("test for successfully") 69 | } 70 | func main(){ 71 | test_base() 72 | test_if() 73 | test_while() 74 | test_for() 75 | } -------------------------------------------------------------------------------- /tests/runtime/kv_common.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use runtime 4 | use std 5 | 6 | func test_array(){ 7 | fmt.println("test array pop") 8 | arr = [] 9 | fmt.assert(std.pop(arr),null,"init array pop should be null") 10 | 11 | for(i = 1 ; i <= 8 ; i += 1){ 12 | arr[] = i 13 | } 14 | for(i = 8 ; i >= 1 ; i -= 1){ 15 | fmt.assert(std.pop(arr),i,"array pop should be " + i) 16 | } 17 | fmt.assert(std.pop(arr),null,"array pop should be null") 18 | fmt.println("test array pop success") 19 | } 20 | func test_tail_head(){ 21 | fmt.println("test tail_head") 22 | 23 | //test arr 24 | arr = [] 25 | // fmt.assert(std.tail(arr),null,"init array tail should be null") 26 | fmt.assert(std.head(arr),null,"init array head should be null") 27 | arr[] = 100 28 | fmt.assert(std.tail(arr),100,"init array tail should be 100") 29 | fmt.assert(std.head(arr),100,"init array head should be 100") 30 | //test map 31 | map = {} 32 | // fmt.assert(std.tail(map),null,"init map tail should be null") 33 | fmt.assert(std.head(map),null,"init map head should be null") 34 | map["test"] = "map" 35 | // fmt.assert(std.tail(map),"map","init map tail should be null") 36 | fmt.assert(std.head(map),"map","init map head should be null") 37 | 38 | fmt.println("test tail head success") 39 | } 40 | 41 | func main(){ 42 | test_array() 43 | test_tail_head() 44 | } 45 | -------------------------------------------------------------------------------- /tests/runtime/len_cap.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use runtime 4 | 5 | // 测试数组的长度和容量 6 | func test_array(){ 7 | fmt.println("test array len&cap") 8 | arr = [] 9 | fmt.assert(runtime.len(arr),0,"init array len should be 0") 10 | fmt.assert(runtime.cap(arr),8,"init array cap should be 8") 11 | 12 | for(i = 1 ; i <= 8 ; i += 1){ 13 | arr[] = i 14 | fmt.assert(runtime.len(arr),i,"init array len should be " + i) 15 | fmt.assert(runtime.cap(arr),8,"init array cap should be 8") 16 | } 17 | arr[] = 9 18 | fmt.assert(runtime.len(arr),9,"init array len should be 9") 19 | fmt.assert(runtime.cap(arr),16,"init array cap should be 16") 20 | fmt.println("test array len&cap success") 21 | } 22 | 23 | 24 | func main(){ 25 | test_array() 26 | } -------------------------------------------------------------------------------- /tests/runtime/malloc.tu: -------------------------------------------------------------------------------- 1 | use runtime 2 | use fmt 3 | use std 4 | func test_runtimemalloc() 5 | { 6 | fmt.println("test runtime multi thread malloc start") 7 | maxsize = 100 8 | //测试分配的个数 9 | count = 10000000 //分配1千万次 10 | //用个数组存起来 11 | arr = runtime.malloc(count * 8,0.(i8),1.(i8)) //每个元素为指针 12 | for(i = 0 ; i < count ; i += 1){ 13 | size = std.srand(maxsize)//每次分配的内存随机大小 14 | if size == 0 || size < 8 { 15 | size = 8 16 | } 17 | newp = runtime.malloc(size,0.(i8),1.(i8)) 18 | *newp = i 19 | //保存起来 20 | arr[i] = newp 21 | } 22 | //验证 23 | for(i = 0 ; i < count ; i += 1){ 24 | v = arr[i] 25 | if *v == i {} else { 26 | os.dief("v:%d != i:%d",int(*v),int(i)) 27 | } 28 | } 29 | fmt.println("test runtime multi thread malloc start end") 30 | 31 | 32 | } 33 | //std 内存分配器性能比较低,测试1万次分配 34 | func test_stdmalloc(){ 35 | maxsize = 100 36 | count = 10000 37 | //用个数组存起来 38 | arr = std.malloc(count * 8,0.(i8),1.(i8)) //每个元素为指针 39 | for(i = 0 ; i < count ; i += 1){ 40 | size = std.srand(maxsize)//每次分配的内存随机大小 41 | if size == 0 || size < 8 { 42 | size = 8 43 | } 44 | newp = std.malloc(size,0.(i8),1.(i8)) 45 | *newp = i 46 | //保存起来 47 | arr[i] = newp 48 | } 49 | //验证 50 | for(i = 0 ; i < count ; i += 1){ 51 | v = arr[i] 52 | if *v == i {} else { 53 | os.dief("v:%d != i:%d",int(*v),int(i)) 54 | } 55 | } 56 | fmt.println("test std malloc start end") 57 | } 58 | func main(){ 59 | test_runtimemalloc() 60 | test_stdmalloc() 61 | } 62 | -------------------------------------------------------------------------------- /tests/runtime/pools.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | func test_chars(){ 5 | a = 'a' 6 | b = 'a' 7 | _a = a 8 | _b = b 9 | if _a == _b {} else os.panic( 10 | "a != a" 11 | ) 12 | 13 | c = '?' 14 | d = '?' 15 | _c = c 16 | _d = d 17 | if _c == _d {} else os.panic( 18 | "? != ?" 19 | ) 20 | if _a == _c os.panic("a == ?") 21 | fmt.println("test chars pool success") 22 | } 23 | 24 | func main(){ 25 | test_chars() 26 | } -------------------------------------------------------------------------------- /tests/runtime/temp/class.tu: -------------------------------------------------------------------------------- 1 | class Inner1 {} 2 | class Inner2 {} -------------------------------------------------------------------------------- /tests/statement/break.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | 4 | func test_while_break(){ 5 | a = 10 6 | while a { 7 | if a == 5 { 8 | fmt.println("should break %d\n",a) 9 | break 10 | } 11 | a = a - 1 12 | fmt.printf("break %d\n",a) 13 | } 14 | } 15 | func break_double(){ 16 | b = 1 17 | while b { 18 | c = 1 19 | fmt.println("outside\n") 20 | while c { 21 | fmt.println("inside\n") 22 | break 23 | } 24 | break 25 | } 26 | } 27 | 28 | func main(){ 29 | test_while_break() 30 | //break_double() 31 | } -------------------------------------------------------------------------------- /tests/statement/chain.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use os 3 | 4 | func call(){ 5 | ca = func(){ 6 | return func(){ 7 | return 100 8 | } 9 | } 10 | re = ca()() 11 | if re != 100 { 12 | fmt.println("call()() test failed") 13 | os.exit(-1) 14 | } 15 | fmt.println("test call success!",re) 16 | } 17 | func call_arr(){ 18 | ca = func(){ 19 | arr = ["first","second","third"] 20 | return arr 21 | } 22 | re = ca()[0] 23 | if re != "first" { 24 | fmt.println("call()[0] test failed") 25 | os.exit(-1) 26 | } 27 | fmt.println("test call arr success!",re) 28 | } 29 | func arr_arr(){ 30 | ca = func(){ 31 | arr1 = ["arr1","arr1"] 32 | arr2 = ["arr2",arr1] 33 | return arr2 34 | } 35 | re = ca()[1][0] 36 | if re != "arr1" { 37 | fmt.println("arr[1][1] test failed") 38 | os.exit(-1) 39 | } 40 | fmt.println("test arr[][] success!",re) 41 | } 42 | class V 43 | { 44 | var 45 | } 46 | func call_var(){ 47 | ca = func(){ 48 | obj = new V() 49 | obj.var = "obj" 50 | return obj 51 | } 52 | re = ca().var 53 | if re != "obj" { 54 | fmt.println("func().objmemeber test failed") 55 | os.exit(-1) 56 | } 57 | fmt.println("test func().obj success!",re) 58 | 59 | } 60 | func all(){ 61 | fc = func(){ 62 | fn1 = func(){ 63 | arr = ["all"] 64 | return arr 65 | } 66 | obj = new V() 67 | obj.p = fn1 68 | arr = [obj] 69 | return arr 70 | } 71 | a = fc()[0].p()[0] 72 | if a != "all" { 73 | fmt.println("func().[0].p()[0]") 74 | os.exit(-1) 75 | } 76 | fmt.println("func().[0].p()[0] test sucess",a) 77 | 78 | } 79 | 80 | func main(){ 81 | call() 82 | call_arr() 83 | arr_arr() 84 | call_var() 85 | all() 86 | } -------------------------------------------------------------------------------- /tests/statement/continue.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | 4 | func test_continue(){ 5 | a = 10 6 | while a { 7 | a = a - 1 8 | if a == 5 { 9 | continue 10 | } 11 | fmt.println("continue a=%d\n",a) 12 | } 13 | } 14 | 15 | 16 | func main(){ 17 | test_continue() 18 | } -------------------------------------------------------------------------------- /tests/statement/for.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use time 4 | use os 5 | 6 | 7 | func test_tri_for(){ 8 | arr = [] 9 | for(a = 1 ; a < 10 ; a += 2){ 10 | arr[] = a 11 | } 12 | i = 0 13 | for(a = 1 ; a < 10 ; a += 2){ 14 | if arr[i] != a { 15 | fmt.printf("failed arr[i]:%d should be %d \n",i,a) 16 | os.exit(1) 17 | } 18 | fmt.printf("arr[%d]=%d a=%d\n",i,arr[i],a) 19 | i += 1 20 | } 21 | fmt.println("test for statment passed!") 22 | } 23 | 24 | fn test_for_range_break(){ 25 | fmt.println("test for range break") 26 | state = 2 27 | arr = [1,2,3] 28 | v = null 29 | v2 = null 30 | match state { 31 | 1 : { 32 | state_1: 33 | fmt.println("state_1:",v,v2) 34 | fmt.println("out:", v) 35 | goto state_4 36 | } 37 | 2 : { 38 | state_2: 39 | fmt.println("state_2:",v,v2) 40 | for v : arr { 41 | goto state_1 42 | main_continue00: 43 | } 44 | } 45 | 4 : { 46 | state_4: 47 | fmt.println("state_4:",v,v2) 48 | for v2 : arr { 49 | fmt.println("state_3:",v,v2) 50 | goto main_end11 51 | } 52 | main_end11: 53 | goto main_continue00 54 | } 55 | } 56 | fmt.println("test for range break sucess ") 57 | } 58 | 59 | 60 | fn main(){ 61 | test_tri_for() 62 | test_for_range_break() 63 | } -------------------------------------------------------------------------------- /tests/statement/func.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | use pkg1 3 | use os 4 | 5 | func f2(){ 6 | return "f2" 7 | } 8 | //test the varname is as same as package func name 9 | func test_func_same(){ 10 | test = pkg1.test() 11 | if test != "pkg1.test" { 12 | os.die("pkg1.test should return pkg1.test") 13 | } 14 | //FIXME: varname == funcname 15 | // f2 = f2() 16 | // if f2 != "f2" { 17 | // os.die("f2() should return f2()") 18 | // } 19 | 20 | fmt.println("test func same success") 21 | } 22 | 23 | //test block 24 | func block(){ 25 | { 26 | a = 1 27 | if a == 1 {} else { 28 | os.die("a should eq 1") 29 | } 30 | } 31 | } 32 | fn test_empty(){ 33 | fc = fn(){} 34 | a = fc() 35 | if a == null {} else os.die("a should be null") 36 | } 37 | 38 | func main(){ 39 | test_func_same() 40 | block() 41 | test_empty() 42 | } -------------------------------------------------------------------------------- /tests/statement/func_arg.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func equal_6(a,b,c,d,e,f){ 6 | fmt.println("equal_6:\n") 7 | fmt.printf("res:%d %d %d %d %d %d \n",a,b,c,d,e,f) 8 | } 9 | func over_6(a,b,c,d,e,f,h,i,j) 10 | { 11 | fmt.println("over_6:\n") 12 | if a != 1 { 13 | fmt.println("a == 1 failed",a) 14 | os.exit(1) 15 | } 16 | if b != 2 { 17 | fmt.println("b == 2 failed",b) 18 | os.exit(1) 19 | } 20 | if c != 3 { 21 | fmt.println("c == 3 failed",c) 22 | os.exit(1) 23 | } 24 | if d != 4 { 25 | fmt.println("d == 4 failed",d) 26 | os.exit(1) 27 | } 28 | if e != 5 { 29 | fmt.println("e == 5 failed",e) 30 | os.exit(1) 31 | } 32 | if f != 6 { 33 | fmt.println("f == 6 failed",f) 34 | os.exit(1) 35 | } 36 | if h != 7 { 37 | fmt.println("h == 7 failed",h) 38 | os.exit(1) 39 | } 40 | if i != 8 { 41 | fmt.println("i == 8 failed",i) 42 | os.exit(1) 43 | } 44 | if j != "this is j" { 45 | fmt.println("j == this is j failed",j) 46 | os.exit(1) 47 | } 48 | fmt.printf("res:%d %d %d %d %d %d %d %d %s\n",a,b,c,d,e,f,h,i,j) 49 | } 50 | 51 | func main(){ 52 | equal_6(1,2,3,4,5,6) 53 | 54 | over_6(1,2,3,4,5,6,7,8,"this is j") 55 | } -------------------------------------------------------------------------------- /tests/statement/if.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_no_brace(){ 6 | arr = [1,100,"test","else"] 7 | 8 | if arr[2] == "test1" os.die("not test1") 9 | else if arr[3] == "else" fmt.println("yes") 10 | 11 | if arr[1] == 102 os.die("not 102") 12 | else if false os.die("not false") 13 | else fmt.println("yes") 14 | 15 | if arr[2] == "te" os.die("not te") 16 | else if arr[3] == "else" fmt.println("yes") 17 | else os.die("not this one") 18 | 19 | if arr[2] != "test" os.die("should be test") 20 | 21 | f = func(){ 22 | var = 100 23 | if var == 200 os.die("not 200") 24 | else if var == 100 return 100 25 | return 300 26 | } 27 | if f() != 100 os.die("shoudl be 100") 28 | 29 | fmt.println("else if test success") 30 | } 31 | 32 | func test_has_brace(){ 33 | a = 1 34 | if a == 1 { 35 | fmt.println("OK\n") 36 | if a != 10 { 37 | fmt.println("OK\n") 38 | }else{ 39 | os.die("failed") 40 | } 41 | }else{ 42 | fmt.println("OK\n") 43 | os.exit(1) 44 | } 45 | 46 | map = {"one":100 , "second":333 , 44:55} 47 | if map["one"] == 101 { 48 | os.die("not this") 49 | }else if map["second"] == 3334 { 50 | os.die("not 333") 51 | }else if map[44] == 55 { 52 | fmt.println("this one") 53 | } 54 | fmt.println("test if else if has brace success") 55 | 56 | } 57 | fn test_common(){ 58 | fmt.println("test common") 59 | if (p = null) == null {} else os.die("p != null") 60 | fmt.println("test common success") 61 | } 62 | 63 | func main(){ 64 | test_has_brace() 65 | test_no_brace() 66 | test_common() 67 | } 68 | 69 | -------------------------------------------------------------------------------- /tests/statement/import.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | use pkg1 5 | use pkg2 6 | 7 | func main(){ 8 | fmt.println("use fmt.println\n") 9 | pkg1.test() 10 | pkg2.test() 11 | 12 | } -------------------------------------------------------------------------------- /tests/statement/nest/a.tu: -------------------------------------------------------------------------------- 1 | map = {0 : "nest"} -------------------------------------------------------------------------------- /tests/statement/nest/nest1/a.tu: -------------------------------------------------------------------------------- 1 | map = {0 : "nest1"} -------------------------------------------------------------------------------- /tests/statement/nest/nest1/nest2/a.tu: -------------------------------------------------------------------------------- 1 | map = {0 : "nest2"} -------------------------------------------------------------------------------- /tests/statement/package.tu: -------------------------------------------------------------------------------- 1 | use pkg1 2 | use pkg1.pkg2 3 | use os 4 | use nest 5 | use nest.nest1 6 | use nest.nest1.nest2 7 | 8 | func test_nest(){ 9 | // the reason of this wired if statment usage : 10 | // `if lhs != rhs` => if lhs is null or rhs is null will lead the entirly result is false 11 | if nest.map[0] == "nest" {} else 12 | os.panic("nes.map:%s should be nest",nest.map[0]) 13 | if nest1.map[0] == "nest1" {} else 14 | os.panic("nest1.map:%s should be nest1",nest1.map[0]) 15 | if nest2.map[0] == "nest2" {} else 16 | os.panic("nest2.map:%s should be nest2",nest2.map[0]) 17 | 18 | nest2.map["xxx"] = "yyy" 19 | if nest2.map["xxx"] == "yyy" {} else 20 | os.panic("nest2.map[\"xxx\"]:%s should be yyy",nest2.map["xxx"]) 21 | 22 | fmt.println("test multi layer pacakge global kv value op success") 23 | } 24 | func common(){ 25 | pkg1.test() 26 | pkg2.test() 27 | if pkg2.obj != "pkg1.pkg2.obj" { 28 | fmt.println("pkg1.pkg2 test failed") 29 | os.exit(-1) 30 | } 31 | fmt.println("pkg1.pkg2 test pass res:",pkg2.obj) 32 | } 33 | 34 | func main(){ 35 | common() 36 | test_nest() 37 | } -------------------------------------------------------------------------------- /tests/statement/pkg1/global_test.tu: -------------------------------------------------------------------------------- 1 | //pkg1.global_a 2 | global_a 3 | 4 | use fmt 5 | 6 | func change_global(){ 7 | fmt.print("test global1: ",global_a," \n") 8 | global_a = "change" 9 | fmt.print("test global1: ",global_a," \n") 10 | } -------------------------------------------------------------------------------- /tests/statement/pkg1/pkg2/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | obj 4 | 5 | 6 | func test() 7 | { 8 | fmt.println("path:/pkg1/pkg2/test.tu") 9 | obj = "pkg1.pkg2.obj" 10 | } -------------------------------------------------------------------------------- /tests/statement/pkg1/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test(){ 5 | fmt.println("this is pkg1\n") 6 | test2() 7 | return "pkg1.test" 8 | } -------------------------------------------------------------------------------- /tests/statement/pkg1/test2.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test2(){ 5 | fmt.println("this is pkg1 function test2\n") 6 | } -------------------------------------------------------------------------------- /tests/statement/pkg2/array_map.tu: -------------------------------------------------------------------------------- 1 | // this is a global variable in package pkg2 2 | global_var -------------------------------------------------------------------------------- /tests/statement/pkg2/test.tu: -------------------------------------------------------------------------------- 1 | use fmt 2 | 3 | 4 | func test(){ 5 | fmt.println("this is pkg2\n") 6 | } -------------------------------------------------------------------------------- /tests/statement/return.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use os 4 | 5 | func test_int(b){ 6 | fmt.printf("test int return %d \n",b) 7 | if b != 1 { 8 | fmt.printf("Error %d\n",b) 9 | os.exit(1) 10 | } 11 | return 2 12 | } 13 | func test_string(str){ 14 | fmt.printf("test string return %s \n",str) 15 | return str + "return" 16 | } 17 | func main(){ 18 | a = 1 19 | b = test_int(a) 20 | if b != 2 { 21 | fmt.printf("Error %d\n",b) 22 | os.exit(1) 23 | } 24 | fmt.printf("test int return success ret:%d\n",b) 25 | 26 | if a != 1 { 27 | fmt.printf("Error %d\n",a) 28 | os.exit(1) 29 | } 30 | 31 | str = test_string("str-") 32 | if str != "str-return" { 33 | fmt.printf("Error %s\n",str) 34 | os.exit(1) 35 | } 36 | fmt.printf("test string return success ret:%s\n",str) 37 | } -------------------------------------------------------------------------------- /tests/statement/while.tu: -------------------------------------------------------------------------------- 1 | 2 | use fmt 3 | use time 4 | use os 5 | 6 | func test_while(){ 7 | a = 4 8 | while a != 2 { 9 | fmt.printf("a=%d\n",a) 10 | //time.sleep(1) 11 | a = a - 1 12 | } 13 | //time.sleep(3) 14 | if a != 2 { 15 | fmt.printf("failed a:%d should be 2 \n",a) 16 | os.exit(1) 17 | } 18 | fmt.printf("success a:%d should be 2 \n",a) 19 | } 20 | 21 | 22 | func main(){ 23 | test_while() 24 | } -------------------------------------------------------------------------------- /tests/statement/while2.tu: -------------------------------------------------------------------------------- 1 | 2 | func test_while(){ 3 | a = 4 4 | while a != 2 { 5 | a = a - 1 6 | } 7 | } 8 | func test_dead_while(){ 9 | i = 1 10 | loop { 11 | if i == 10 break 12 | i += 1 13 | } 14 | } 15 | 16 | func main(){ 17 | test_while() 18 | test_dead_while() 19 | } -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | - a2l: address to line --------------------------------------------------------------------------------