aboutsummaryrefslogtreecommitdiff
path: root/tcc/examples
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /tcc/examples
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'tcc/examples')
-rw-r--r--tcc/examples/dll.c13
-rw-r--r--tcc/examples/fib.c24
-rw-r--r--tcc/examples/hello_dll.c20
-rw-r--r--tcc/examples/hello_win.c163
-rw-r--r--tcc/examples/libtcc_test.c96
5 files changed, 316 insertions, 0 deletions
diff --git a/tcc/examples/dll.c b/tcc/examples/dll.c
new file mode 100644
index 0000000..052a056
--- /dev/null
+++ b/tcc/examples/dll.c
@@ -0,0 +1,13 @@
+//+---------------------------------------------------------------------------
+//
+// dll.c - Windows DLL example - dynamically linked part
+//
+
+#include <windows.h>
+
+__declspec(dllexport) const char *hello_data = "(not set)";
+
+__declspec(dllexport) void hello_func (void)
+{
+ MessageBox (0, hello_data, "From DLL", MB_ICONINFORMATION);
+}
diff --git a/tcc/examples/fib.c b/tcc/examples/fib.c
new file mode 100644
index 0000000..8da26bc
--- /dev/null
+++ b/tcc/examples/fib.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h> // atoi()
+
+int fib(n)
+{
+ if (n <= 2)
+ return 1;
+ else
+ return fib(n-1) + fib(n-2);
+}
+
+int main(int argc, char **argv)
+{
+ int n;
+ if (argc < 2) {
+ printf("usage: fib n\n"
+ "Compute nth Fibonacci number\n");
+ return 1;
+ }
+
+ n = atoi(argv[1]);
+ printf("fib(%d) = %d\n", n, fib(n));
+ return 0;
+}
diff --git a/tcc/examples/hello_dll.c b/tcc/examples/hello_dll.c
new file mode 100644
index 0000000..4813c5b
--- /dev/null
+++ b/tcc/examples/hello_dll.c
@@ -0,0 +1,20 @@
+//+---------------------------------------------------------------------------
+//
+// HELLO_DLL.C - Windows DLL example - main application part
+//
+
+#include <windows.h>
+
+void hello_func (void);
+__declspec(dllimport) extern const char *hello_data;
+
+int WINAPI WinMain(
+ HINSTANCE hInstance,
+ HINSTANCE hPrevInstance,
+ LPSTR lpCmdLine,
+ int nCmdShow)
+{
+ hello_data = "Hello World!";
+ hello_func();
+ return 0;
+}
diff --git a/tcc/examples/hello_win.c b/tcc/examples/hello_win.c
new file mode 100644
index 0000000..96546e4
--- /dev/null
+++ b/tcc/examples/hello_win.c
@@ -0,0 +1,163 @@
+//+---------------------------------------------------------------------------
+//
+// HELLO_WIN.C - Windows GUI 'Hello World!' Example
+//
+//+---------------------------------------------------------------------------
+
+#include <windows.h>
+
+#define APPNAME "HELLO_WIN"
+
+char szAppName[] = APPNAME; // The name of this application
+char szTitle[] = APPNAME; // The title bar text
+const char *pWindowText;
+
+void CenterWindow(HWND hWnd);
+
+//+---------------------------------------------------------------------------
+//
+// Function: WndProc
+//
+// Synopsis: very unusual type of function - gets called by system to
+// process windows messages.
+//
+// Arguments: same as always.
+//----------------------------------------------------------------------------
+
+LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+ switch (message) {
+
+ // ----------------------- first and last
+ case WM_CREATE:
+ CenterWindow(hwnd);
+ break;
+
+ case WM_DESTROY:
+ PostQuitMessage(0);
+ break;
+
+ // ----------------------- get out of it...
+ case WM_RBUTTONUP:
+ DestroyWindow(hwnd);
+ break;
+
+ case WM_KEYDOWN:
+ if (VK_ESCAPE == wParam)
+ DestroyWindow(hwnd);
+ break;
+
+ // ----------------------- display our minimal info
+ case WM_PAINT:
+ {
+ PAINTSTRUCT ps;
+ HDC hdc;
+ RECT rc;
+ hdc = BeginPaint(hwnd, &ps);
+
+ GetClientRect(hwnd, &rc);
+ SetTextColor(hdc, RGB(240,240,96));
+ SetBkMode(hdc, TRANSPARENT);
+ DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
+
+ EndPaint(hwnd, &ps);
+ break;
+ }
+
+ // ----------------------- let windows do all other stuff
+ default:
+ return DefWindowProc(hwnd, message, wParam, lParam);
+ }
+ return 0;
+}
+
+//+---------------------------------------------------------------------------
+//
+// Function: WinMain
+//
+// Synopsis: standard entrypoint for GUI Win32 apps
+//
+//----------------------------------------------------------------------------
+int APIENTRY WinMain(
+ HINSTANCE hInstance,
+ HINSTANCE hPrevInstance,
+ LPSTR lpCmdLine,
+ int nCmdShow
+ )
+{
+ MSG msg;
+ WNDCLASS wc;
+ HWND hwnd;
+
+ pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
+
+ // Fill in window class structure with parameters that describe
+ // the main window.
+
+ ZeroMemory(&wc, sizeof wc);
+ wc.hInstance = hInstance;
+ wc.lpszClassName = szAppName;
+ wc.lpfnWndProc = (WNDPROC)WndProc;
+ wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
+ wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
+ wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+ wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+
+ if (FALSE == RegisterClass(&wc))
+ return 0;
+
+ // create the browser
+ hwnd = CreateWindow(
+ szAppName,
+ szTitle,
+ WS_OVERLAPPEDWINDOW|WS_VISIBLE,
+ CW_USEDEFAULT,
+ CW_USEDEFAULT,
+ 360,//CW_USEDEFAULT,
+ 240,//CW_USEDEFAULT,
+ 0,
+ 0,
+ hInstance,
+ 0);
+
+ if (NULL == hwnd)
+ return 0;
+
+ // Main message loop:
+ while (GetMessage(&msg, NULL, 0, 0) > 0) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+
+ return msg.wParam;
+}
+
+//+---------------------------------------------------------------------------
+
+//+---------------------------------------------------------------------------
+
+void CenterWindow(HWND hwnd_self)
+{
+ HWND hwnd_parent;
+ RECT rw_self, rc_parent, rw_parent;
+ int xpos, ypos;
+
+ hwnd_parent = GetParent(hwnd_self);
+ if (NULL == hwnd_parent)
+ hwnd_parent = GetDesktopWindow();
+
+ GetWindowRect(hwnd_parent, &rw_parent);
+ GetClientRect(hwnd_parent, &rc_parent);
+ GetWindowRect(hwnd_self, &rw_self);
+
+ xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2;
+ ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2;
+
+ SetWindowPos(
+ hwnd_self, NULL,
+ xpos, ypos, 0, 0,
+ SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
+ );
+}
+
+//+---------------------------------------------------------------------------
diff --git a/tcc/examples/libtcc_test.c b/tcc/examples/libtcc_test.c
new file mode 100644
index 0000000..480d314
--- /dev/null
+++ b/tcc/examples/libtcc_test.c
@@ -0,0 +1,96 @@
+/*
+ * Simple Test program for libtcc
+ *
+ * libtcc can be useful to use tcc as a "backend" for a code generator.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libtcc.h"
+
+/* this function is called by the generated code */
+int add(int a, int b)
+{
+ return a + b;
+}
+
+/* this strinc is referenced by the generated code */
+const char hello[] = "Hello World!";
+
+char my_program[] =
+"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
+"extern int add(int a, int b);\n"
+"#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */
+" __attribute__((dllimport))\n"
+"#endif\n"
+"extern const char hello[];\n"
+"int fib(int n)\n"
+"{\n"
+" if (n <= 2)\n"
+" return 1;\n"
+" else\n"
+" return fib(n-1) + fib(n-2);\n"
+"}\n"
+"\n"
+"int foo(int n)\n"
+"{\n"
+" printf(\"%s\\n\", hello);\n"
+" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
+" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
+" return 0;\n"
+"}\n";
+
+int main(int argc, char **argv)
+{
+ TCCState *s;
+ int i;
+ int (*func)(int);
+
+ s = tcc_new();
+ if (!s) {
+ fprintf(stderr, "Could not create tcc state\n");
+ exit(1);
+ }
+
+ /* if tcclib.h and libtcc1.a are not installed, where can we find them */
+ for (i = 1; i < argc; ++i) {
+ char *a = argv[i];
+ if (a[0] == '-') {
+ if (a[1] == 'B')
+ tcc_set_lib_path(s, a+2);
+ else if (a[1] == 'I')
+ tcc_add_include_path(s, a+2);
+ else if (a[1] == 'L')
+ tcc_add_library_path(s, a+2);
+ }
+ }
+
+ /* MUST BE CALLED before any compilation */
+ tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
+
+ if (tcc_compile_string(s, my_program) == -1)
+ return 1;
+
+ /* as a test, we add symbols that the compiled program can use.
+ You may also open a dll with tcc_add_dll() and use symbols from that */
+ tcc_add_symbol(s, "add", add);
+ tcc_add_symbol(s, "hello", hello);
+
+ /* relocate the code */
+ if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
+ return 1;
+
+ /* get entry symbol */
+ func = tcc_get_symbol(s, "foo");
+ if (!func)
+ return 1;
+
+ /* run the code */
+ func(32);
+
+ /* delete the state */
+ tcc_delete(s);
+
+ return 0;
+}