summaryrefslogtreecommitdiff
path: root/examples/nolibc.c
blob: 1a68383f16bebd241b0fed5ed5e11f8929e6b1e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define assert(X) do { if (!(X)) __asm__("int3"); } while (0)

#define IMXML_SIMD_DEFAULT SCALAR
#define IMXML_NO_SUPPORT_SINGLE_QUOTES
#define IMXML_NO_SUPPORT_COMMENTS
#define IMXML_NO_SUPPORT_CDATA
#define IMXML_NO_SUPPORT_ENTITIES
#define IMXML_NO_SUPPORT_NAMESPACES
#define THREADLOCAL
#define IMXML_NO_CHECK_BOUNDS

#define IMXML_IMPLEMENTATION
#include "imxml.h"

// some compilers require memset to be present
void* memset (void* s, int c, size_t n) {
    for (size_t i = 0; i < n; ++i) {
        ((char*)s)[i] = c;
    }
    return s;
}

static void write (ImxmlString s) {
    long ret;
    __asm__ volatile(
        "syscall"
        : "=a"(ret)
        : "a"(1), "D"(1), "S"(s.items), "d"(s.count)
        : "rcx", "r11", "memory"
    );
}

void start (void) {
    static ImxmlCharSequence xml = "<msg from=\"John\" to=\"Jane\">Hello, World!</msg>"IMXML_LIT_PAD;

    XmlParser p = {
        .head = xml,
    };

    assert(xml_tag_expect(&p, "msg"));
    assert(xml_attr_expect(&p, "from"));
    ImxmlString const from = xml_value(&p);
    assert(xml_attr_expect(&p, "to"));
    ImxmlString const to = xml_value(&p);

    XmlContent content = xml_content(&p);
    assert(content.type == XML_CONTENT_TEXT);
    ImxmlString const msg = content.data;

    write(from);
    write(imxml_strlit("->"));
    write(to);
    write(imxml_strlit(": "));
    write(msg);
    write(imxml_strlit("\n"));

    __asm__ volatile(
        "syscall\n"
        :
        : "a"(60), "D"(0)
        : "rcx", "r11", "memory"
    );
}

void _start (void) {
    __asm__ volatile (
        "andq $-16, %rsp\n" // required because the compiler emits aligned stack loads later
        "call start\n"
    );
}