Win32 Demo: OpenGL Context

This commit is contained in:
gingerBill
2016-08-16 20:08:40 +01:00
parent 2d49a61563
commit e8530ca883
14 changed files with 647 additions and 224 deletions

View File

@@ -1,117 +1,451 @@
%.string = type {i8*, i64} ; Basic_string
%.rawptr = type i8* ; Basic_rawptr
%HANDLE = type %.rawptr
%HWND = type %.rawptr
%HDC = type %.rawptr
%HINSTANCE = type %.rawptr
%HICON = type %.rawptr
%HCURSOR = type %.rawptr
%HMENU = type %.rawptr
%HBRUSH = type %.rawptr
%WPARAM = type i64
%LPARAM = type i64
%LRESULT = type i64
%ATOM = type i16
%POINT = type {i32, i32}
%BOOL = type i32
%WNDPROC = type %LRESULT (%HWND, i32, %WPARAM, %LPARAM)*
%WNDCLASSEXA = type {i32, i32, %WNDPROC, i32, i32, %HINSTANCE, %HICON, %HCURSOR, %HBRUSH, i8*, i8*, %HICON}
%MSG = type {%HWND, i32, %WPARAM, %LPARAM, i32, %POINT}
%HGLRC = type %.rawptr
%PROC = type void ()*
%wglCreateContextAttribsARBType = type %HGLRC (%HDC, %.rawptr, i32*)*
%PIXELFORMATDESCRIPTOR = type {i32, i32, i32, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i32}
declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1) argmemonly nounwind
@win32_perf_count_freq = global i64 zeroinitializer
define double @time_now() {
entry.-.0:
%0 = load i64, i64* @win32_perf_count_freq, align 8
%1 = icmp eq i64 %0, 0
br i1 %1, label %if.then.-.1, label %if.done.-.2
if.then.-.1:
call void @llvm.debugtrap()
br label %if.done.-.2
if.done.-.2:
%2 = alloca i64, align 8 ; counter
store i64 zeroinitializer, i64* %2
%3 = getelementptr inbounds i64, i64* %2
%4 = call i32 @QueryPerformanceCounter(i64* %3)
%5 = alloca double, align 8 ; result
store double zeroinitializer, double* %5
%6 = load i64, i64* @win32_perf_count_freq, align 8
%7 = sitofp i64 %6 to double
%8 = load i64, i64* %2, align 8
%9 = sitofp i64 %8 to double
%10 = fdiv double %9, %7
store double %10, double* %5
%11 = load double, double* %5, align 8
ret double %11
}
define void @win32_print_last_error() {
entry.-.0:
%0 = alloca i64, align 8 ; err_code
store i64 zeroinitializer, i64* %0
%1 = call i32 @GetLastError()
%2 = zext i32 %1 to i64
store i64 %2, i64* %0
%3 = load i64, i64* %0, align 8
%4 = icmp ne i64 %3, 0
br i1 %4, label %if.then.-.1, label %if.done.-.2
if.then.-.1:
%5 = getelementptr inbounds [14 x i8], [14 x i8]* @.str0, i64 0, i64 0
%6 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %6
%7 = getelementptr inbounds %.string, %.string* %6, i64 0, i32 0
%8 = getelementptr inbounds %.string, %.string* %6, i64 0, i32 1
store i8* %5, i8** %7
store i64 14, i64* %8
%9 = load %.string, %.string* %6, align 8
call void @print_string(%.string %9)
%10 = load i64, i64* %0, align 8
call void @print_int(i64 %10)
%11 = getelementptr inbounds [1 x i8], [1 x i8]* @.str1, i64 0, i64 0
%12 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %12
%13 = getelementptr inbounds %.string, %.string* %12, i64 0, i32 0
%14 = getelementptr inbounds %.string, %.string* %12, i64 0, i32 1
store i8* %11, i8** %13
store i64 1, i64* %14
%15 = load %.string, %.string* %12, align 8
call void @print_string(%.string %15)
br label %if.done.-.2
if.done.-.2:
ret void
}
define void @main() {
entry.-.0:
call void @__$startup_runtime()
%0 = getelementptr inbounds [8 x i8], [8 x i8]* @.str0, i64 0, i64 0
%1 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %1
%2 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 0
%3 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 1
store i8* %0, i8** %2
store i64 8, i64* %3
%4 = load %.string, %.string* %1, align 8
call void @print_string(%.string %4)
br label %for.init.-.1
%0 = alloca %WNDCLASSEXA, align 8 ; wc
store %WNDCLASSEXA zeroinitializer, %WNDCLASSEXA* %0
%1 = alloca %HINSTANCE, align 8 ; instance
store %HINSTANCE zeroinitializer, %HINSTANCE* %1
%2 = call %HINSTANCE @GetModuleHandleA(i8* null)
store %HINSTANCE %2, %HINSTANCE* %1
%3 = getelementptr inbounds i64, i64* @win32_perf_count_freq
%4 = call i32 @QueryPerformanceFrequency(i64* %3)
%5 = alloca i8*, align 8 ; class_name
store i8* zeroinitializer, i8** %5
%6 = getelementptr inbounds [18 x i8], [18 x i8]* @.str2, i64 0, i64 0
%7 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %7
%8 = getelementptr inbounds %.string, %.string* %7, i64 0, i32 0
%9 = getelementptr inbounds %.string, %.string* %7, i64 0, i32 1
store i8* %6, i8** %8
store i64 18, i64* %9
%10 = load %.string, %.string* %7, align 8
%11 = call i8* @main$to_c_string-0(%.string %10)
store i8* %11, i8** %5
%12 = alloca i8*, align 8 ; title
store i8* zeroinitializer, i8** %12
%13 = getelementptr inbounds [18 x i8], [18 x i8]* @.str3, i64 0, i64 0
%14 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %14
%15 = getelementptr inbounds %.string, %.string* %14, i64 0, i32 0
%16 = getelementptr inbounds %.string, %.string* %14, i64 0, i32 1
store i8* %13, i8** %15
store i64 18, i64* %16
%17 = load %.string, %.string* %14, align 8
%18 = call i8* @main$to_c_string-0(%.string %17)
store i8* %18, i8** %12
%19 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 0
store i32 80, i32* %19
%20 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 1
store i32 3, i32* %20
%21 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 5
%22 = load %HINSTANCE, %HINSTANCE* %1, align 8
store %HINSTANCE %22, %HINSTANCE* %21
%23 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 10
%24 = load i8*, i8** %5, align 8
store i8* %24, i8** %23
%25 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 2
store %WNDPROC @main$1, %WNDPROC* %25
%26 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0
%27 = call %ATOM @RegisterClassExA(%WNDCLASSEXA* %26)
%28 = icmp eq i16 %27, 0
br i1 %28, label %if.then.-.1, label %if.done.-.4
for.init.-.1:
%5 = alloca i64, align 8 ; i
store i64 zeroinitializer, i64* %5
store i64 0, i64* %5
br label %for.loop.-.3
if.then.-.1:
br label %defer.-.2
for.body.-.2:
%6 = load i64, i64* %5, align 8
%7 = icmp sgt i64 %6, 2
br i1 %7, label %if.then.-.5, label %if.done.-.9
defer.-.2:
%29 = load i8*, i8** %12, align 8
call void @free(%.rawptr %29)
br label %defer.-.3
for.loop.-.3:
%8 = load i64, i64* %5, align 8
%9 = icmp slt i64 %8, 4
br i1 %9, label %for.body.-.2, label %for.done.-.13
defer.-.3:
%30 = load i8*, i8** %5, align 8
call void @free(%.rawptr %30)
ret void
for.post.-.4:
%10 = load i64, i64* %5, align 8
%11 = add i64 %10, 1
store i64 %11, i64* %5
br label %for.loop.-.3
if.done.-.4:
%31 = alloca %HWND, align 8 ; hwnd
store %HWND zeroinitializer, %HWND* %31
%32 = load i8*, i8** %5, align 8
%33 = load i8*, i8** %12, align 8
%34 = load %HINSTANCE, %HINSTANCE* %1, align 8
%35 = call %HWND @CreateWindowExA(i32 0, i8* %32, i8* %33, i32 281673728, i32 2147483648, i32 2147483648, i32 854, i32 480, %HWND null, %HMENU null, %HINSTANCE %34, %.rawptr null)
store %HWND %35, %HWND* %31
%36 = load %HWND, %HWND* %31, align 8
%37 = icmp eq %.rawptr %36, null
br i1 %37, label %if.then.-.5, label %if.done.-.8
if.then.-.5:
call void @win32_print_last_error()
br label %defer.-.6
defer.-.6:
%12 = getelementptr inbounds [6 x i8], [6 x i8]* @.str1, i64 0, i64 0
%13 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %13
%14 = getelementptr inbounds %.string, %.string* %13, i64 0, i32 0
%15 = getelementptr inbounds %.string, %.string* %13, i64 0, i32 1
store i8* %12, i8** %14
store i64 6, i64* %15
%16 = load %.string, %.string* %13, align 8
call void @print_string(%.string %16)
%38 = load i8*, i8** %12, align 8
call void @free(%.rawptr %38)
br label %defer.-.7
defer.-.7:
%17 = load i64, i64* %5, align 8
call void @print_int(i64 %17)
call void @print_rune(i32 10)
br label %for.done.-.13
defer.-.8:
%18 = getelementptr inbounds [6 x i8], [6 x i8]* @.str2, i64 0, i64 0
%19 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %19
%20 = getelementptr inbounds %.string, %.string* %19, i64 0, i32 0
%21 = getelementptr inbounds %.string, %.string* %19, i64 0, i32 1
store i8* %18, i8** %20
store i64 6, i64* %21
%22 = load %.string, %.string* %19, align 8
call void @print_string(%.string %22)
br label %if.done.-.9
if.done.-.9:
%23 = load i64, i64* %5, align 8
%24 = icmp eq i64 %23, 2
br i1 %24, label %if.then.-.10, label %if.done.-.11
if.then.-.10:
br label %if.done.-.11
if.done.-.11:
br label %defer.-.12
defer.-.12:
%25 = load i64, i64* %5, align 8
call void @print_int(i64 %25)
call void @print_rune(i32 10)
br label %for.post.-.4
for.done.-.13:
%26 = getelementptr inbounds [13 x i8], [13 x i8]* @.str3, i64 0, i64 0
%27 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %27
%28 = getelementptr inbounds %.string, %.string* %27, i64 0, i32 0
%29 = getelementptr inbounds %.string, %.string* %27, i64 0, i32 1
store i8* %26, i8** %28
store i64 13, i64* %29
%30 = load %.string, %.string* %27, align 8
call void @print_string(%.string %30)
br label %defer.-.14
defer.-.14:
%31 = getelementptr inbounds [6 x i8], [6 x i8]* @.str4, i64 0, i64 0
%32 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %32
%33 = getelementptr inbounds %.string, %.string* %32, i64 0, i32 0
%34 = getelementptr inbounds %.string, %.string* %32, i64 0, i32 1
store i8* %31, i8** %33
store i64 6, i64* %34
%35 = load %.string, %.string* %32, align 8
call void @print_string(%.string %35)
%39 = load i8*, i8** %5, align 8
call void @free(%.rawptr %39)
ret void
if.done.-.8:
%40 = alloca %HDC, align 8 ; dc
store %HDC zeroinitializer, %HDC* %40
%41 = load %HWND, %HWND* %31, align 8
%42 = call %HDC @GetDC(%HANDLE %41)
store %HDC %42, %HDC* %40
%43 = alloca %HGLRC, align 8 ; opengl_context
store %HGLRC zeroinitializer, %HGLRC* %43
%44 = alloca [8 x i32], align 4 ; attribs
store [8 x i32] zeroinitializer, [8 x i32]* %44
%45 = alloca %PIXELFORMATDESCRIPTOR, align 4 ; pfd
store %PIXELFORMATDESCRIPTOR zeroinitializer, %PIXELFORMATDESCRIPTOR* %45
%46 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 0
store i32 44, i32* %46
%47 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 1
store i32 1, i32* %47
%48 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 2
store i32 37, i32* %48
%49 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 3
store i8 0, i8* %49
%50 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 4
store i8 32, i8* %50
%51 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 11
store i8 8, i8* %51
%52 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 18
store i8 24, i8* %52
%53 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 19
store i8 8, i8* %53
%54 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45, i64 0, i32 21
store i8 0, i8* %54
%55 = load %HDC, %HDC* %40, align 8
%56 = load %HDC, %HDC* %40, align 8
%57 = getelementptr inbounds %PIXELFORMATDESCRIPTOR, %PIXELFORMATDESCRIPTOR* %45
%58 = call i32 @ChoosePixelFormat(%HDC %56, %PIXELFORMATDESCRIPTOR* %57)
%59 = call %BOOL @SetPixelFormat(%HDC %55, i32 %58, %PIXELFORMATDESCRIPTOR* null)
%60 = load %HDC, %HDC* %40, align 8
%61 = call %HGLRC @wglCreateContext(%HDC %60)
store %HGLRC %61, %HGLRC* %43
%62 = load %HDC, %HDC* %40, align 8
%63 = load %HGLRC, %HGLRC* %43, align 8
%64 = call %BOOL @wglMakeCurrent(%HDC %62, %HGLRC %63)
%65 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%66 = getelementptr i32, i32* %65, i64 0
store i32 8337, i32* %66
%67 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%68 = getelementptr i32, i32* %67, i64 1
store i32 2, i32* %68
%69 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%70 = getelementptr i32, i32* %69, i64 2
store i32 8338, i32* %70
%71 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%72 = getelementptr i32, i32* %71, i64 3
store i32 1, i32* %72
%73 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%74 = getelementptr i32, i32* %73, i64 4
store i32 37158, i32* %74
%75 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%76 = getelementptr i32, i32* %75, i64 5
store i32 2, i32* %76
%77 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%78 = getelementptr i32, i32* %77, i64 6
store i32 0, i32* %78
%79 = alloca %.string, align 8 ; wgl_string
store %.string zeroinitializer, %.string* %79
%80 = getelementptr inbounds [27 x i8], [27 x i8]* @.str4, i64 0, i64 0
%81 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %81
%82 = getelementptr inbounds %.string, %.string* %81, i64 0, i32 0
%83 = getelementptr inbounds %.string, %.string* %81, i64 0, i32 1
store i8* %80, i8** %82
store i64 27, i64* %83
%84 = load %.string, %.string* %81, align 8
store %.string %84, %.string* %79
%85 = alloca %wglCreateContextAttribsARBType, align 8 ; wglCreateContextAttribsARB
store %wglCreateContextAttribsARBType zeroinitializer, %wglCreateContextAttribsARBType* %85
%86 = getelementptr inbounds %.string, %.string* %79, i64 0, i32 0
%87 = load i8*, i8** %86, align 8
%88 = getelementptr i8, i8* %87, i64 0
%89 = getelementptr inbounds i8, i8* %88
%90 = call %PROC @wglGetProcAddress(i8* %89)
%91 = bitcast void ()* %90 to %HGLRC (%HDC, %.rawptr, i32*)*
%92 = bitcast %HGLRC (%HDC, %.rawptr, i32*)* %91 to %HGLRC (%HDC, %.rawptr, i32*)*
store %wglCreateContextAttribsARBType %92, %wglCreateContextAttribsARBType* %85
%93 = alloca %HGLRC, align 8 ; rc
store %HGLRC zeroinitializer, %HGLRC* %93
%94 = load %wglCreateContextAttribsARBType, %wglCreateContextAttribsARBType* %85, align 8
%95 = load %HDC, %HDC* %40, align 8
%96 = getelementptr inbounds [8 x i32], [8 x i32]* %44, i64 0, i64 0
%97 = getelementptr i32, i32* %96, i64 0
%98 = getelementptr inbounds i32, i32* %97
%99 = call %HGLRC %94(%HDC %95, %.rawptr null, i32* %98)
store %HGLRC %99, %HGLRC* %93
%100 = load %HDC, %HDC* %40, align 8
%101 = load %HGLRC, %HGLRC* %93, align 8
%102 = call %BOOL @wglMakeCurrent(%HDC %100, %HGLRC %101)
%103 = load %HDC, %HDC* %40, align 8
%104 = call %BOOL @SwapBuffers(%HDC %103)
%105 = alloca double, align 8 ; start_time
store double zeroinitializer, double* %105
%106 = call double @time_now()
store double %106, double* %105
%107 = alloca i1, align 1 ; running
store i1 zeroinitializer, i1* %107
store i1 true, i1* %107
br label %for.loop.-.10
for.body.-.9:
%108 = alloca double, align 8 ; curr_time
store double zeroinitializer, double* %108
%109 = call double @time_now()
store double %109, double* %108
%110 = alloca double, align 8 ; dt
store double zeroinitializer, double* %110
%111 = load double, double* %105, align 8
%112 = load double, double* %108, align 8
%113 = fsub double %112, %111
store double %113, double* %110
%114 = load double, double* %110, align 8
%115 = fcmp ogt double %114, 0x4000000000000000
br i1 %115, label %if.then.-.11, label %if.done.-.12
for.loop.-.10:
%116 = load i1, i1* %107, align 1
br i1 %116, label %for.body.-.9, label %for.done.-.22
if.then.-.11:
store i1 false, i1* %107
br label %if.done.-.12
if.done.-.12:
%117 = alloca %MSG, align 8 ; msg
store %MSG zeroinitializer, %MSG* %117
br label %for.body.-.13
for.body.-.13:
%118 = alloca i1, align 1 ; ok
store i1 zeroinitializer, i1* %118
%119 = getelementptr inbounds %MSG, %MSG* %117
%120 = call %BOOL @PeekMessageA(%MSG* %119, %HWND null, i32 0, i32 0, i32 1)
%121 = icmp ne i32 %120, 0
store i1 %121, i1* %118
%122 = load i1, i1* %118, align 1
br i1 %122, label %if.done.-.15, label %if.then.-.14
if.then.-.14:
br label %for.done.-.21
if.done.-.15:
%123 = getelementptr inbounds %MSG, %MSG* %117, i64 0, i32 1
%124 = load i32, i32* %123, align 4
%125 = icmp eq i32 %124, 18
br i1 %125, label %if.then.-.16, label %if.else.-.17
if.then.-.16:
br label %defer.-.18
if.else.-.17:
%126 = getelementptr inbounds %MSG, %MSG* %117
%127 = call %BOOL @TranslateMessage(%MSG* %126)
%128 = getelementptr inbounds %MSG, %MSG* %117
%129 = call %LRESULT @DispatchMessageA(%MSG* %128)
br label %if.done.-.20
defer.-.18:
%130 = load i8*, i8** %12, align 8
call void @free(%.rawptr %130)
br label %defer.-.19
defer.-.19:
%131 = load i8*, i8** %5, align 8
call void @free(%.rawptr %131)
ret void
if.done.-.20:
br label %for.body.-.13
for.done.-.21:
%132 = load %HDC, %HDC* %40, align 8
%133 = call %BOOL @SwapBuffers(%HDC %132)
call void @sleep_ms(i32 2)
br label %for.loop.-.10
for.done.-.22:
br label %defer.-.23
defer.-.23:
%134 = load i8*, i8** %12, align 8
call void @free(%.rawptr %134)
br label %defer.-.24
defer.-.24:
%135 = load i8*, i8** %5, align 8
call void @free(%.rawptr %135)
ret void
}
define i8* @main$to_c_string-0(%.string %s) {
entry.-.0:
%0 = alloca %.string, align 8 ; s
store %.string zeroinitializer, %.string* %0
store %.string %s, %.string* %0
%1 = alloca i8*, align 8 ; c_str
store i8* zeroinitializer, i8** %1
%2 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1
%3 = load i64, i64* %2, align 8
%4 = add i64 %3, 1
%5 = call %.rawptr @malloc(i64 %4)
%6 = bitcast %.rawptr %5 to i8*
store i8* %6, i8** %1
%7 = load i8*, i8** %1, align 8
%8 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0
%9 = load i8*, i8** %8, align 8
%10 = getelementptr i8, i8* %9, i64 0
%11 = getelementptr inbounds i8, i8* %10
%12 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1
%13 = load i64, i64* %12, align 8
%14 = call i32 @memcpy(%.rawptr %7, %.rawptr %11, i64 %13)
%15 = load i8*, i8** %1, align 8
%16 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1
%17 = load i64, i64* %16, align 8
%18 = getelementptr i8, i8* %15, i64 %17
store i8 0, i8* %18
%19 = load i8*, i8** %1, align 8
ret i8* %19
}
define %LRESULT @main$1(%HWND %hwnd, i32 %msg, %WPARAM %wparam, %LPARAM %lparam) noinline {
entry.-.0:
%0 = alloca %HWND, align 8 ; hwnd
store %HWND zeroinitializer, %HWND* %0
store %HWND %hwnd, %HWND* %0
%1 = alloca i32, align 4 ; msg
store i32 zeroinitializer, i32* %1
store i32 %msg, i32* %1
%2 = alloca %WPARAM, align 8 ; wparam
store %WPARAM zeroinitializer, %WPARAM* %2
store %WPARAM %wparam, %WPARAM* %2
%3 = alloca %LPARAM, align 8 ; lparam
store %LPARAM zeroinitializer, %LPARAM* %3
store %LPARAM %lparam, %LPARAM* %3
%4 = load i32, i32* %1, align 4
%5 = icmp eq i32 %4, 2
br i1 %5, label %if.then.-.1, label %cmp-or.-.3
if.then.-.1:
call void @ExitProcess(i32 0)
ret %LRESULT 0
cmp-or.-.2:
%6 = load i32, i32* %1, align 4
%7 = icmp eq i32 %6, 18
br i1 %7, label %if.then.-.1, label %if.done.-.4
cmp-or.-.3:
%8 = load i32, i32* %1, align 4
%9 = icmp eq i32 %8, 16
br i1 %9, label %if.then.-.1, label %cmp-or.-.2
if.done.-.4:
%10 = load %HWND, %HWND* %0, align 8
%11 = load i32, i32* %1, align 4
%12 = load %WPARAM, %WPARAM* %2, align 8
%13 = load %LPARAM, %LPARAM* %3, align 8
%14 = call %LRESULT @DefWindowProcA(%HWND %10, i32 %11, %WPARAM %12, %LPARAM %13)
ret i64 %14
}
define void @print_string(%.string %s) {
@@ -749,6 +1083,54 @@ if.done.-.3:
ret void
}
declare %HANDLE @GetStdHandle(i32 %h) ; foreign
declare i32 @CloseHandle(%HANDLE %h) ; foreign
declare i32 @WriteFileA(%HANDLE %h, %.rawptr %buf, i32 %len, i32* %written_result, %.rawptr %overlapped) ; foreign
declare i32 @GetLastError() ; foreign
declare void @ExitProcess(i32 %exit_code) ; foreign
declare %HWND @GetDesktopWindow() ; foreign
declare i32 @GetCursorPos(%POINT* %p) ; foreign
declare i32 @ScreenToClient(%HWND %h, %POINT* %p) ; foreign
declare %HINSTANCE @GetModuleHandleA(i8* %module_name) ; foreign
declare i32 @QueryPerformanceFrequency(i64* %result) ; foreign
declare i32 @QueryPerformanceCounter(i64* %result) ; foreign
define void @sleep_ms(i32 %ms) {
entry.-.0:
%0 = alloca i32, align 4 ; ms
store i32 zeroinitializer, i32* %0
store i32 %ms, i32* %0
%1 = load i32, i32* %0, align 4
%2 = call i32 @Sleep(i32 %1)
ret void
}
declare i32 @Sleep(i32 %ms) declare void @OutputDebugStringA(i8* %c_str) ; foreign
declare %ATOM @RegisterClassExA(%WNDCLASSEXA* %wc) ; foreign
declare %HWND @CreateWindowExA(i32 %ex_style, i8* %class_name, i8* %title, i32 %style, i32 %x, i32 %y, i32 %w, i32 %h, %HWND %parent, %HMENU %menu, %HINSTANCE %instance, %.rawptr %param) ; foreign
declare %BOOL @ShowWindow(%HWND %hwnd, i32 %cmd_show) ; foreign
declare %BOOL @UpdateWindow(%HWND %hwnd) ; foreign
declare %BOOL @PeekMessageA(%MSG* %msg, %HWND %hwnd, i32 %msg_filter_min, i32 %msg_filter_max, i32 %remove_msg) ; foreign
declare %BOOL @TranslateMessage(%MSG* %msg) ; foreign
declare %LRESULT @DispatchMessageA(%MSG* %msg) ; foreign
declare %LRESULT @DefWindowProcA(%HWND %hwnd, i32 %msg, %WPARAM %wparam, %LPARAM %lparam) ; foreign
define i64 @GetQueryPerformanceFrequency() {
entry.-.0:
%0 = alloca i64, align 8 ; r
store i64 zeroinitializer, i64* %0
%1 = getelementptr inbounds i64, i64* %0
%2 = call i32 @QueryPerformanceFrequency(i64* %1)
%3 = load i64, i64* %0, align 8
ret i64 %3
}
declare %HDC @GetDC(%HANDLE %h) ; foreign
declare %BOOL @SetPixelFormat(%HDC %hdc, i32 %pixel_format, %PIXELFORMATDESCRIPTOR* %pfd) ; foreign
declare i32 @ChoosePixelFormat(%HDC %hdc, %PIXELFORMATDESCRIPTOR* %pfd) ; foreign
declare %HGLRC @wglCreateContext(%HDC %hdc) ; foreign
declare %BOOL @wglMakeCurrent(%HDC %hdc, %HGLRC %hglrc) ; foreign
declare %PROC @wglGetProcAddress(i8* %c_str) ; foreign
declare %BOOL @wglDeleteContext(%HGLRC %hglrc) ; foreign
declare %BOOL @SwapBuffers(%HDC %hdc) ; foreign
declare i32 @putchar(i32 %c) ; foreign
declare %.rawptr @malloc(i64 %sz) ; foreign
declare void @free(%.rawptr %ptr) ; foreign
@@ -995,17 +1377,19 @@ entry.-.0:
ret i1 %5
}
@.str0 = global [8 x i8] c"Hellope\0A"
@.str1 = global [6 x i8] c"break\0A"
@.str2 = global [6 x i8] c"break\0A"
@.str3 = global [13 x i8] c"Never\20called\0A"
@.str4 = global [6 x i8] c"World\0A"
@.str0 = global [14 x i8] c"GetLastError\3A\20"
@.str1 = global [1 x i8] c"\0A"
@.str2 = global [18 x i8] c"Odin-Language-Demo"
@.str3 = global [18 x i8] c"Odin\20Language\20Demo"
@.str4 = global [27 x i8] c"wglCreateContextAttribsARB\00"
@.str5 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$"
@.str6 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$"
@.str7 = global [4 x i8] c"true"
@.str8 = global [5 x i8] c"false"
define void @__$startup_runtime() noinline {
entry.-.0:
%0 = call i64 @GetQueryPerformanceFrequency()
store i64 %0, i64* @win32_perf_count_freq
ret void
}

View File

@@ -1,30 +1,4 @@
#load "basic.odin"
main :: proc() {
print_string("Hellope\n");
defer print_string("World\n");
for i := 0; i < 4; i++ {
defer {
print_int(i);
print_rune('\n');
}
if i > 2 {
defer print_string("break\n");
break;
}
if i == 2 {
// return;
}
}
print_string("Never called\n");
}
/*
#load "win32.odin"
win32_perf_count_freq := GetQueryPerformanceFrequency();
@@ -50,7 +24,6 @@ win32_print_last_error :: proc() {
}
main :: proc() {
/*
wc: WNDCLASSEXA;
instance := GetModuleHandleA(null);
@@ -67,12 +40,13 @@ main :: proc() {
class_name := to_c_string("Odin-Language-Demo");
title := to_c_string("Odin Language Demo");
defer heap_free(class_name);
defer heap_free(title);
wc.cbSize = size_of(WNDCLASSEXA) as u32;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.hInstance = instance;
wc.className = class_name;
wc.hbrBackground = COLOR_BACKGROUND as HBRUSH;
wc.wndProc = proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
@@ -90,7 +64,7 @@ main :: proc() {
hwnd := CreateWindowExA(0,
class_name, title,
WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0, 0, 854, 480,
CW_USEDEFAULT, CW_USEDEFAULT, 854, 480,
null, null, instance, null);
@@ -99,9 +73,45 @@ main :: proc() {
return;
}
dc := GetDC(hwnd);
opengl_context: HGLRC;
{
attribs : [8]i32;
pfd: PIXELFORMATDESCRIPTOR;
pfd.nSize = size_of(PIXELFORMATDESCRIPTOR) as u32;
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cAlphaBits = 8;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
SetPixelFormat(dc, ChoosePixelFormat(dc, ^pfd), null);
opengl_context = wglCreateContext(dc);
wglMakeCurrent(dc, opengl_context);
attribs[0] = 0x2091; // WGL_CONTEXT_MAJOR_VERSION_ARB
attribs[1] = 2; // Major
attribs[2] = 0x2092; // WGL_CONTEXT_MINOR_VERSION_ARB
attribs[3] = 1; // Minor
attribs[4] = 0x9126; // WGL_CONTEXT_PROFILE_MASK_ARB
attribs[5] = 0x0002; // WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
attribs[6] = 0; // NOTE(bill): tells the proc that this is the end of attribs
wgl_string := "wglCreateContextAttribsARB\x00";
wglCreateContextAttribsARB := wglGetProcAddress(^wgl_string[0]) as wglCreateContextAttribsARBType;
rc := wglCreateContextAttribsARB(dc, 0, ^attribs[0]);
wglMakeCurrent(dc, rc);
SwapBuffers(dc);
}
start_time := time_now();
running := true;
tick_count := 0;
for running {
curr_time := time_now();
dt := curr_time - start_time;
@@ -124,13 +134,7 @@ main :: proc() {
}
}
print_string("Tick: ");
print_int(tick_count);
tick_count++;
print_rune('\n');
sleep_ms(16);
SwapBuffers(dc);
sleep_ms(2);
}
*/
}
*/

View File

@@ -21,25 +21,25 @@ WM_QUIT :: 0x12;
PM_REMOVE :: 1;
COLOR_BACKGROUND : rawptr : 1; // NOTE(bill): cast to HBRUSH when needed
COLOR_BACKGROUND: rawptr : 1; // NOTE(bill): cast to HBRUSH when needed
type HANDLE: rawptr;
type HWND: HANDLE;
type HDC: HANDLE;
type HINSTANCE: HANDLE;
type HICON: HANDLE;
type HCURSOR: HANDLE;
type HMENU: HANDLE;
type HBRUSH: HANDLE;
type WPARAM: uint;
type LPARAM: int;
type LRESULT: int;
type ATOM: i16;
type HANDLE: rawptr
type HWND: HANDLE
type HDC: HANDLE
type HINSTANCE: HANDLE
type HICON: HANDLE
type HCURSOR: HANDLE
type HMENU: HANDLE
type HBRUSH: HANDLE
type WPARAM: uint
type LPARAM: int
type LRESULT: int
type ATOM: i16
type POINT: struct { x, y: i32 }
type BOOL: i32;
type BOOL: i32
type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT;
type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
type WNDCLASSEXA: struct {
cbSize, style: u32,
@@ -89,7 +89,8 @@ RegisterClassExA :: proc(wc: ^WNDCLASSEXA) -> ATOM #foreign
CreateWindowExA :: proc(ex_style: u32,
class_name, title: ^u8,
style: u32,
x, y, w, h: i32,
x, y: u32,
w, h: i32,
parent: HWND, menu: HMENU, instance: HINSTANCE,
param: rawptr) -> HWND #foreign
@@ -109,3 +110,76 @@ GetQueryPerformanceFrequency :: proc() -> i64 {
_ = QueryPerformanceFrequency(^r);
return r;
}
// Windows OpenGL
PFD_TYPE_RGBA :: 0;
PFD_TYPE_COLORINDEX :: 1;
PFD_MAIN_PLANE :: 0;
PFD_OVERLAY_PLANE :: 1;
PFD_UNDERLAY_PLANE :: -1;
PFD_DOUBLEBUFFER :: 1;
PFD_STEREO :: 2;
PFD_DRAW_TO_WINDOW :: 4;
PFD_DRAW_TO_BITMAP :: 8;
PFD_SUPPORT_GDI :: 16;
PFD_SUPPORT_OPENGL :: 32;
PFD_GENERIC_FORMAT :: 64;
PFD_NEED_PALETTE :: 128;
PFD_NEED_SYSTEM_PALETTE :: 0x00000100;
PFD_SWAP_EXCHANGE :: 0x00000200;
PFD_SWAP_COPY :: 0x00000400;
PFD_SWAP_LAYER_BUFFERS :: 0x00000800;
PFD_GENERIC_ACCELERATED :: 0x00001000;
PFD_DEPTH_DONTCARE :: 0x20000000;
PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000;
PFD_STEREO_DONTCARE :: 0x80000000;
type HGLRC: HANDLE
type PROC: proc()
type wglCreateContextAttribsARBType: proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC
type PIXELFORMATDESCRIPTOR: struct {
nSize,
nVersion,
dwFlags: u32,
iPixelType,
cColorBits,
cRedBits,
cRedShift,
cGreenBits,
cGreenShift,
cBlueBits,
cBlueShift,
cAlphaBits,
cAlphaShift,
cAccumBits,
cAccumRedBits,
cAccumGreenBits,
cAccumBlueBits,
cAccumAlphaBits,
cDepthBits,
cStencilBits,
cAuxBuffers,
iLayerType,
bReserved: byte,
dwLayerMask,
dwVisibleMask,
dwDamageMask: u32,
}
GetDC :: proc(h: HANDLE) -> HDC #foreign
SetPixelFormat :: proc(hdc: HDC, pixel_format: i32, pfd: ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign
ChoosePixelFormat :: proc(hdc: HDC, pfd: ^PIXELFORMATDESCRIPTOR) -> i32 #foreign
wglCreateContext :: proc(hdc: HDC) -> HGLRC #foreign
wglMakeCurrent :: proc(hdc: HDC, hglrc: HGLRC) -> BOOL #foreign
wglGetProcAddress :: proc(c_str: ^u8) -> PROC #foreign
wglDeleteContext :: proc(hglrc: HGLRC) -> BOOL #foreign
SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign

View File

@@ -5,6 +5,4 @@ rem call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
set _NO_DEBUG_HEAP=1
set path=w:\Odin\misc;%path%
wmic
cls

View File

@@ -633,14 +633,6 @@ void check_parsed_files(Checker *c) {
add_file_entity(c, td->name, e, d);
case_end;
case_ast_node(ad, AliasDecl, decl);
ast_node(n, Ident, ad->name);
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, n->token, NULL);
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
d->type_expr = ad->type;
add_file_entity(c, ad->name, e, d);
case_end;
case_ast_node(pd, ProcDecl, decl);
ast_node(n, Ident, pd->name);
Token token = n->token;

View File

@@ -600,10 +600,10 @@ void check_is_expressible(Checker *c, Operand *o, Type *type) {
if (!is_type_integer(o->type) && is_type_integer(type)) {
error(&c->error_collector, ast_node_token(o->expr), "`%s` truncated to `%s`", a, b);
} else {
error(&c->error_collector, ast_node_token(o->expr), "`%s` overflows `%s`", a, b);
error(&c->error_collector, ast_node_token(o->expr), "`%s = %lld` overflows `%s`", a, o->value.value_integer, b);
}
} else {
error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b);
}
o->mode = Addressing_Invalid;
@@ -867,6 +867,11 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
return true;
}
// proc <-> proc
if (is_type_proc(xb), is_type_proc(yb)) {
return true;
}
return false;
}

View File

@@ -878,12 +878,5 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
add_entity(c, c->context.scope, td->name, e);
check_type_decl(c, e, td->type, NULL);
case_end;
case_ast_node(ad, AliasDecl, node);
ast_node(name, Ident, ad->name);
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->token, NULL);
add_entity(c, c->context.scope, ad->name, e);
check_alias_decl(c, e, ad->type, NULL);
case_end;
}
}

View File

@@ -382,6 +382,9 @@ b32 is_type_u8_slice(Type *t) {
b32 is_type_vector(Type *t) {
return t->kind == Type_Vector;
}
b32 is_type_proc(Type *t) {
return t->kind == Type_Proc;
}
Type *base_vector_type(Type *t) {
if (is_type_vector(t)) {
return t->vector.elem;
@@ -747,10 +750,12 @@ gbString write_type_to_string(gbString str, Type *type) {
if (type->tuple.variable_count > 0) {
for (isize i = 0; i < type->tuple.variable_count; i++) {
Entity *var = type->tuple.variables[i];
GB_ASSERT(var->kind == Entity_Variable);
if (i > 0)
str = gb_string_appendc(str, ", ");
str = write_type_to_string(str, var->type);
if (var != NULL) {
GB_ASSERT(var->kind == Entity_Variable);
if (i > 0)
str = gb_string_appendc(str, ", ");
str = write_type_to_string(str, var->type);
}
}
}
break;

View File

@@ -210,7 +210,15 @@ void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type
ssa_fprintf(f, "\"");
} break;
case ExactValue_Integer: {
ssa_fprintf(f, "%lld", value.value_integer);
if (is_type_pointer(get_base_type(type))) {
if (value.value_integer == 0) {
ssa_fprintf(f, "null");
} else {
GB_PANIC("TODO(bill): Pointer constant");
}
} else {
ssa_fprintf(f, "%lld", value.value_integer);
}
} break;
case ExactValue_Float: {
u64 u = *cast(u64*)&value.value_float;

View File

@@ -1355,6 +1355,11 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
}
// proc <-> proc
if (is_type_proc(src) && is_type_proc(dst)) {
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
}
// []byte/[]u8 <-> string
if (is_type_u8_slice(src) && is_type_string(dst)) {
@@ -1743,7 +1748,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
// NOTE(bill): Regular call
ssaValue *value = ssa_build_expr(proc, ce->proc);
Type *proc_type_ = ssa_value_type(value);
Type *proc_type_ = get_base_type(ssa_value_type(value));
GB_ASSERT(proc_type_->kind == Type_Proc);
auto *type = &proc_type_->proc;

View File

@@ -76,7 +76,7 @@ int main(int argc, char **argv) {
output_name, cast(int)base_name_len, output_name);
win32_exec_command_line_app(
"clang %.*s.bc -o %.*s.exe -Wno-override-module "
"-lkernel32.lib -luser32.lib",
"-lkernel32.lib -luser32.lib -lgdi32.lib -lopengl32.lib",
cast(int)base_name_len, output_name,
cast(int)base_name_len, output_name);

View File

@@ -177,7 +177,6 @@ AST_NODE_KIND(_DeclBegin, struct{}) \
String foreign_name; \
}) \
AST_NODE_KIND(TypeDecl, struct { Token token; AstNode *name, *type; }) \
AST_NODE_KIND(AliasDecl, struct { Token token; AstNode *name, *type; }) \
AST_NODE_KIND(LoadDecl, struct { Token token, filepath; }) \
AST_NODE_KIND(_DeclEnd, struct{}) \
AST_NODE_KIND(_TypeBegin, struct{}) \
@@ -336,8 +335,6 @@ Token ast_node_token(AstNode *node) {
return node->ProcDecl.name->Ident.token;
case AstNode_TypeDecl:
return node->TypeDecl.token;
case AstNode_AliasDecl:
return node->AliasDecl.token;
case AstNode_LoadDecl:
return node->LoadDecl.token;
case AstNode_Field: {
@@ -759,15 +756,6 @@ gb_inline AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNod
return result;
}
gb_inline AstNode *make_alias_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_AliasDecl);
result->AliasDecl.token = token;
result->AliasDecl.name = name;
result->AliasDecl.type = type;
return result;
}
gb_inline AstNode *make_load_decl(AstFile *f, Token token, Token filepath) {
AstNode *result = make_node(f, AstNode_LoadDecl);
result->LoadDecl.token = token;
@@ -1948,31 +1936,7 @@ AstNode *parse_stmt(AstFile *f) {
AstNode *name = parse_identifier(f);
expect_token(f, Token_Colon);
AstNode *type = parse_type(f);
AstNode *type_decl = make_type_decl(f, token, name, type);
if (type->kind != AstNode_StructType &&
type->kind != AstNode_ProcType) {
expect_token(f, Token_Semicolon);
}
return type_decl;
} break;
case Token_alias: {
Token token = expect_token(f, Token_alias);
AstNode *name = parse_identifier(f);
expect_token(f, Token_Colon);
AstNode *type = parse_type(f);
AstNode *alias_decl = make_alias_decl(f, token, name, type);
if (type->kind != AstNode_StructType &&
type->kind != AstNode_ProcType) {
expect_token(f, Token_Semicolon);
}
return alias_decl;
return make_type_decl(f, token, name, type);
} break;
// Operands

View File

@@ -152,14 +152,6 @@ void print_ast(AstNode *node, isize indent) {
print_ast(node->TypeDecl.type, indent+1);
break;
case AstNode_AliasDecl:
print_indent(indent);
gb_printf("(alias)\n");
print_ast(node->AliasDecl.name, indent+1);
print_ast(node->AliasDecl.type, indent+1);
break;
case AstNode_ProcType:
print_indent(indent);
gb_printf("(type:proc)(%td -> %td)\n", node->ProcType.param_count, node->ProcType.result_count);

View File

@@ -78,7 +78,6 @@ TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
\
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_type, "type"), \
TOKEN_KIND(Token_alias, "alias"), \
TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_match, "match"), \
TOKEN_KIND(Token_break, "break"), \