Index: .gitignore
===================================================================
--- .gitignore	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ .gitignore	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -9,6 +9,4 @@
 *.so.*
 *.d
-*.ag.probe.s
-*.ag.probe.c
 *.pyc
 tag
@@ -16,5 +14,4 @@
 common.h.new
 deps.mk
-Makefile.ag.depend
 /Makefile.config
 /Makefile.common
@@ -46,33 +43,11 @@
 boot/image.raw
 kernel/arch/amd64/_link.ld
-kernel/arch/amd64/include/arch/boot/memmap_struct.h
-kernel/arch/amd64/include/arch/common.h
-kernel/arch/amd64/include/arch/context_struct.h
-kernel/arch/amd64/include/arch/istate_struct.h
-kernel/arch/amd64/include/arch/kseg_struct.h
 kernel/arch/arm32/_link.ld
-kernel/arch/arm32/include/arch/context_struct.h
-kernel/arch/arm32/include/arch/istate_struct.h
 kernel/arch/ia32/_link.ld
-kernel/arch/ia32/include/arch/boot/memmap_struct.h
-kernel/arch/ia32/include/arch/context_struct.h
-kernel/arch/ia32/include/arch/istate_struct.h
 kernel/arch/ia64/_link.ld
-kernel/arch/ia64/include/arch/context_struct.h
-kernel/arch/ia64/include/arch/istate_struct.h
 kernel/arch/mips32/_link.ld
-kernel/arch/mips32/include/arch/context_struct.h
-kernel/arch/mips32/include/arch/fpu_context_struct.h
-kernel/arch/mips32/include/arch/istate_struct.h
 kernel/arch/ppc32/_link.ld
-kernel/arch/ppc32/include/arch/context_struct.h
-kernel/arch/ppc32/include/arch/fpu_context_struct.h
-kernel/arch/ppc32/include/arch/istate_struct.h
 kernel/arch/sparc64/_link.ld
-kernel/arch/sparc64/include/arch/context_struct.h
-kernel/arch/sparc64/include/arch/istate_struct.h
 kernel/arch/abs32le/_link.ld
-kernel/genarch/include/genarch/multiboot/multiboot_info_struct.h
-kernel/genarch/include/genarch/multiboot/multiboot_memmap_struct.h
 kernel/generic/src/debug/real_map.bin
 kernel/kernel.bin
@@ -344,49 +319,32 @@
 uspace/lib/c/arch/amd64/_link-shlib.ld
 uspace/lib/c/arch/amd64/_link.ld
-uspace/lib/c/arch/amd64/include/libarch/common.h
-uspace/lib/c/arch/amd64/include/libarch/fibril_context.h
-uspace/lib/c/arch/amd64/include/libarch/istate_struct.h
 uspace/lib/c/arch/arm32/_link-dlexe.ld
 uspace/lib/c/arch/arm32/_link-loader.ld
 uspace/lib/c/arch/arm32/_link-shlib.ld
 uspace/lib/c/arch/arm32/_link.ld
-uspace/lib/c/arch/arm32/include/libarch/fibril_context.h
-uspace/lib/c/arch/arm32/include/libarch/istate_struct.h
 uspace/lib/c/arch/ia32/_link-dlexe.ld
 uspace/lib/c/arch/ia32/_link-loader.ld
 uspace/lib/c/arch/ia32/_link-shlib.ld
 uspace/lib/c/arch/ia32/_link.ld
-uspace/lib/c/arch/ia32/include/libarch/fibril_context.h
-uspace/lib/c/arch/ia32/include/libarch/istate_struct.h
 uspace/lib/c/arch/ia64/_link-dlexe.ld
 uspace/lib/c/arch/ia64/_link-loader.ld
 uspace/lib/c/arch/ia64/_link-shlib.ld
 uspace/lib/c/arch/ia64/_link.ld
-uspace/lib/c/arch/ia64/include/libarch/fibril_context.h
-uspace/lib/c/arch/ia64/include/libarch/istate_struct.h
 uspace/lib/c/arch/mips32/_link-dlexe.ld
 uspace/lib/c/arch/mips32/_link-loader.ld
 uspace/lib/c/arch/mips32/_link-shlib.ld
 uspace/lib/c/arch/mips32/_link.ld
-uspace/lib/c/arch/mips32/include/libarch/fibril_context.h
-uspace/lib/c/arch/mips32/include/libarch/istate_struct.h
 uspace/lib/c/arch/mips32eb/_link-dlexe.ld
 uspace/lib/c/arch/mips32eb/_link-loader.ld
 uspace/lib/c/arch/mips32eb/_link-shlib.ld
 uspace/lib/c/arch/mips32eb/_link.ld
-uspace/lib/c/arch/mips32eb/include/libarch/fibril_context.h
-uspace/lib/c/arch/mips32eb/include/libarch/istate_struct.h
 uspace/lib/c/arch/ppc32/_link-dlexe.ld
 uspace/lib/c/arch/ppc32/_link-loader.ld
 uspace/lib/c/arch/ppc32/_link-shlib.ld
 uspace/lib/c/arch/ppc32/_link.ld
-uspace/lib/c/arch/ppc32/include/libarch/fibril_context.h
-uspace/lib/c/arch/ppc32/include/libarch/istate_struct.h
 uspace/lib/c/arch/sparc64/_link-dlexe.ld
 uspace/lib/c/arch/sparc64/_link-loader.ld
 uspace/lib/c/arch/sparc64/_link-shlib.ld
 uspace/lib/c/arch/sparc64/_link.ld
-uspace/lib/c/arch/sparc64/include/libarch/fibril_context.h
-uspace/lib/c/arch/sparc64/include/libarch/istate_struct.h
 uspace/lib/c/arch/abs32le/_link-dlexe.ld
 uspace/lib/c/arch/abs32le/_link-loader.ld
Index: kernel/Makefile
===================================================================
--- kernel/Makefile	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/Makefile	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -48,4 +48,5 @@
 #
 
+AUTOGEN = $(ROOT_PATH)/tools/autogen2.sh
 AGDEPEND = Makefile.ag.depend
 RAW = kernel.raw
@@ -73,4 +74,5 @@
 
 .PHONY: all clean autogen_clean depend
+.DELETE_ON_ERROR:
 
 all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(COMMON_HEADER) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(BIN) $(DISASM)
@@ -354,14 +356,6 @@
 GENARCH_DEPENDS := $(addsuffix .d,$(basename $(GENARCH_SOURCES)))
 
-GENARCH_AUTOGENS_H := $(addsuffix .h,$(basename $(GENARCH_AUTOGENS_AG)))
-GENARCH_AUTOGENS_PROBE_C := $(addsuffix .ag.probe.c,$(basename $(GENARCH_AUTOGENS_AG)))
-GENARCH_AUTOGENS_PROBE_S := $(addsuffix .ag.probe.s,$(basename $(GENARCH_AUTOGENS_AG)))
-
-ARCH_AUTOGENS_H := $(addsuffix .h,$(basename $(ARCH_AUTOGENS_AG)))
-ARCH_AUTOGENS_PROBE_C := $(addsuffix .ag.probe.c,$(basename $(ARCH_AUTOGENS_AG)))
-ARCH_AUTOGENS_PROBE_S := $(addsuffix .ag.probe.s,$(basename $(ARCH_AUTOGENS_AG)))
-
-AUTOGENS_H := $(ARCH_AUTOGENS_H) $(GENARCH_AUTOGENS_H)
 AUTOGENS_AG := $(ARCH_AUTOGENS_AG) $(GENARCH_AUTOGENS_AG)
+AUTOGEN_DEPENDS := $(addsuffix .d,$(AUTOGENS_AG))
 
 AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
@@ -371,4 +365,5 @@
 -include $(ARCH_DEPENDS)
 -include $(GENARCH_DEPENDS)
+-include $(AUTOGEN_DEPENDS)
 
 ifeq ($(COMPILER),clang)
@@ -402,8 +397,6 @@
 	$(CC) $(DEFS) $(CFLAGS) -D__ASM__ -D__LINKER__ -E -x c $< | grep -v "^\#" > $@
 
-%.h: %.ag
-	$(AUTOGEN) probe $< >$<.probe.c
-	$(CC_AUTOGEN) $(DEFS) $(CFLAGS) -S -o $<.probe.s $<.probe.c
-	$(AUTOGEN) generate $< <$<.probe.s >$@
+%.ag.h %.ag.s %.ag.c %.ag.d: %.h
+	CC="$(CC)" CFLAGS="$(CFLAGS)" $(AUTOGEN) $< $@
 
 %.o: %.S | depend
@@ -441,19 +434,6 @@
 	$(GENMAP) $(MAP_PREV) $(DUMP) $@
 
-$(AUTOGENS_H): $(AGDEPEND)
-
-$(AGDEPEND): $(AUTOGENS_AG)
-	echo "# DO NOT EDIT" >$@
-	for g in $^; do \
-		h=`dirname $$g`/`basename $$g .ag`.h; \
-		for l in `$(AUTOGEN) depend $$g`; do \
-			echo "$$h: $$l" >>$@; \
-		done \
-	done
-
-depend: $(AUTOGENS_H)
+depend: $(AUTOGENS_AG)
 
 autogen_clean:
-	-rm $(ARCH_AUTOGENS_H) $(ARCH_AUTOGENS_PROBE_C) $(ARCH_AUTOGENS_PROBE_S)
-	-rm $(GENARCH_AUTOGENS_H) $(GENARCH_AUTOGENS_PROBE_C) $(GENARCH_AUTOGENS_PROBE_S)
-	-rm $(AGDEPEND)
+	-rm $(AUTOGENS_AG) $(AUTOGEN_DEPENDS) $(AUTOGENS_AG)
Index: kernel/arch/amd64/Makefile.inc
===================================================================
--- kernel/arch/amd64/Makefile.inc	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/amd64/Makefile.inc	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -86,6 +86,6 @@
 
 ARCH_AUTOGENS_AG = \
-	arch/$(KARCH)/include/arch/istate_struct.ag \
-	arch/$(KARCH)/include/arch/context_struct.ag \
-	arch/$(KARCH)/include/arch/kseg_struct.ag \
-	arch/$(KARCH)/include/arch/boot/memmap_struct.ag
+	arch/$(KARCH)/include/arch/istate_struct.ag.h \
+	arch/$(KARCH)/include/arch/context_struct.ag.h \
+	arch/$(KARCH)/include/arch/kseg_struct.ag.h \
+	arch/$(KARCH)/include/arch/boot/memmap_struct.ag.h
Index: rnel/arch/amd64/include/arch/boot/memmap_struct.ag
===================================================================
--- kernel/arch/amd64/include/arch/boot/memmap_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,1 +1,0 @@
-../../../../ia32/include/arch/boot/memmap_struct.ag
Index: kernel/arch/amd64/include/arch/boot/memmap_struct.h
===================================================================
--- kernel/arch/amd64/include/arch/boot/memmap_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/arch/amd64/include/arch/boot/memmap_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,36 @@
+/* Copyright (c) 2016 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <stdint.h>
+
+typedef struct e820memmap {
+	uint64_t base_address;
+	uint64_t size;
+	uint32_t type;
+} __attribute__((packed)) e820memmap_t;
+
Index: rnel/arch/amd64/include/arch/context_struct.ag
===================================================================
--- kernel/arch/amd64/include/arch/context_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,87 +1,0 @@
-# Copyright (c) 2014 Jakub Jermar
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-#   notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-# - The name of the author may not be used to endorse or promote products
-#   derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-{
-        name : context,
-
-        includes : [
-                {
-                        include : <typedefs.h>
-                }
-        ],
-
-        members : [
-                #
-                # We include only registers that must be preserved
-                # during function call.
-                #
-
-                {
-                        name : sp,
-                        type : uint64_t
-                },
-                {
-                        name : pc,
-                        type : uint64_t
-                },
-
-                {
-                        name : rbx,
-                        type : uint64_t
-                },
-                {
-                        name : rbp,
-                        type : uint64_t
-                },
-
-                {
-                        name : r12,
-                        type : uint64_t
-                },
-                {
-                        name : r13,
-                        type : uint64_t
-                },
-                {
-                        name : r14,
-                        type : uint64_t
-                },
-                {
-                        name : r15,
-                        type : uint64_t
-                },
-                {
-                        name : tp,
-                        type : uint64_t
-                },
-
-                {
-                        name : ipl,
-                        type : ipl_t
-                }
-        ]
-}
Index: kernel/arch/amd64/include/arch/context_struct.h
===================================================================
--- kernel/arch/amd64/include/arch/context_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/arch/amd64/include/arch/context_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,46 @@
+/* Copyright (c) 2014 Jakub Jermar
+ * All rights preserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <typedefs.h>
+
+typedef struct context {
+	/* We include only registers that must be preserved
+	 * during function call.
+	 */
+	uint64_t sp;
+	uint64_t pc;
+	uint64_t rbx;
+	uint64_t rbp;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+	uint64_t tp;
+	ipl_t ipl;
+} context_t;
+
Index: rnel/arch/amd64/include/arch/istate_struct.ag
===================================================================
--- kernel/arch/amd64/include/arch/istate_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,150 +1,0 @@
-# Copyright (c) 2014 Jakub Jermar
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-#   notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-# - The name of the author may not be used to endorse or promote products
-#   derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-{
-        name : istate,
-
-        includes : [
-                {
-                        guard : KERNEL,
-                        include : <typedefs.h>
-                },
-                {
-                        negative-guard: KERNEL,
-                        include : <stddef.h>
-                },
-                {
-                        negative-guard: KERNEL,
-                        include : <stdint.h>
-                }
-        ],
-
-        members : [
-                {
-                        name : rax,
-                        type : uint64_t
-                },
-                {
-                        name : rbx,
-                        type : uint64_t
-                },
-                {
-                        name : rcx,
-                        type : uint64_t
-                },
-                {
-                        name : rdx,
-                        type : uint64_t
-                },
-                {
-                        name : rsi,
-                        type : uint64_t
-                },
-                {
-                        name : rdi,
-                        type : uint64_t
-                },
-                {
-                        name : rbp,
-                        type : uint64_t
-                },
-                {
-                        name : r8,
-                        type : uint64_t
-                },
-                {
-                        name : r9,
-                        type : uint64_t
-                },
-                {
-                        name : r10,
-                        type : uint64_t
-                },
-                {
-                        name : r11,
-                        type : uint64_t
-                },
-                {
-                        name : r12,
-                        type : uint64_t
-                },
-                {
-                        name : r13,
-                        type : uint64_t
-                },
-                {
-                        name : r14,
-                        type : uint64_t
-                },
-                {
-                        name : r15,
-                        type : uint64_t
-                },
-                {
-                        # align rbp_frame on multiple of 16
-                        name : alignment,
-                        type : uint64_t
-                },
-                {
-                        # imitation of frame pointer linkage
-                        name : rbp_frame,
-                        type : uint64_t
-                },
-                {
-                        # imitation of frame address linkage
-                        name : rip_frame,
-                        type : uint64_t
-                },
-                {
-                        # real or fake error word
-                        name : error_word,
-                        type : uint64_t
-                },
-                {
-                        name : rip,
-                        type : uint64_t
-                },
-                {
-                        name : cs,
-                        type : uint64_t
-                },
-                {
-                        name : rflags,
-                        type : uint64_t
-                },
-                {
-                        # only if istate_t is from uspace
-                        name : rsp,
-                        type : uint64_t
-                },
-                {
-                        # only if istate_t is from uspace
-                        name : ss,
-                        type : uint64_t
-                }
-        ]
-}
Index: kernel/arch/amd64/include/arch/istate_struct.h
===================================================================
--- kernel/arch/amd64/include/arch/istate_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/arch/amd64/include/arch/istate_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,63 @@
+/* Copyright (c) 2014 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <stdint.h>
+
+typedef struct istate {
+	uint64_t rax;
+	uint64_t rbx;
+	uint64_t rcx;
+	uint64_t rdx;
+	uint64_t rsi;
+	uint64_t rdi;
+	uint64_t rbp;
+	uint64_t r8;
+	uint64_t r9;
+	uint64_t r10;
+	uint64_t r11;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+	/* align rbp_frame on multiple of 16 */
+	uint64_t alignment;
+	/* imitation of frame pointer linkage */
+	uint64_t rbp_frame;
+	/* imitation of frame address linkage */
+	uint64_t rip_frame;
+	/* real or fake error word */
+	uint64_t error_word;
+	uint64_t rip;
+	uint64_t cs;
+	uint64_t rflags;
+	/* only if istate_t is from uspace */
+	uint64_t rsp;
+	/* only if istate_t is from uspace */
+	uint64_t ss;
+} istate_t;
+
Index: rnel/arch/amd64/include/arch/kseg_struct.ag
===================================================================
--- kernel/arch/amd64/include/arch/kseg_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,51 +1,0 @@
-# Copyright (c) 2016 Jakub Jermar
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-#   notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-# - The name of the author may not be used to endorse or promote products
-#   derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-{
-        name : kseg,
-
-        includes : [
-                {
-                        include : <typedefs.h>
-                }
-        ],
-
-        members : [
-                {
-                        name : ustack_rsp,
-                        type : uint64_t
-                },
-                {
-                        name : kstack_rsp,
-                        type : uint64_t
-                },
-                {
-                        name : fsbase,
-                        type : uint64_t
-                }
-        ]
-}
Index: kernel/arch/amd64/include/arch/kseg_struct.h
===================================================================
--- kernel/arch/amd64/include/arch/kseg_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/arch/amd64/include/arch/kseg_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,36 @@
+/* Copyright (c) 2016 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <stdint.h>
+
+typedef struct kseg {
+	uint64_t ustack_rsp;
+	uint64_t kstack_rsp;
+	uint64_t fsbase;
+} kseg_t;
+
Index: kernel/arch/amd64/src/asm.S
===================================================================
--- kernel/arch/amd64/src/asm.S	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/amd64/src/asm.S	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -30,6 +30,6 @@
 #include <arch/pm.h>
 #include <arch/mm/page.h>
-#include <arch/istate_struct.h>
-#include <arch/kseg_struct.h>
+#include <arch/istate_struct.ag.h>
+#include <arch/kseg_struct.ag.h>
 #include <arch/cpu.h>
 #include <arch/smp/apic.h>
@@ -541,2 +541,3 @@
 	ret
 FUNCTION_END(early_putchar)
+
Index: kernel/arch/amd64/src/boot/multiboot.S
===================================================================
--- kernel/arch/amd64/src/boot/multiboot.S	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/amd64/src/boot/multiboot.S	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -720,2 +720,3 @@
 status_main:
 	.asciz "[main] "
+
Index: kernel/arch/amd64/src/context.S
===================================================================
--- kernel/arch/amd64/src/context.S	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/amd64/src/context.S	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -28,5 +28,5 @@
 
 #include <abi/asmtool.h>
-#include <arch/context_struct.h>
+#include <arch/context_struct.ag.h>
 #include <arch/vreg.h>
 
Index: kernel/arch/amd64/src/smp/ap.S
===================================================================
--- kernel/arch/amd64/src/smp/ap.S	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/amd64/src/smp/ap.S	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -39,5 +39,5 @@
 #include <arch/cpu.h>
 #include <arch/cpuid.h>
-#include <arch/context_struct.h>
+#include <arch/context_struct.ag.h>
 
 .section K_TEXT_START, "ax"
Index: kernel/arch/ia32/include/arch/boot/memmap.h
===================================================================
--- kernel/arch/ia32/include/arch/boot/memmap.h	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/arch/ia32/include/arch/boot/memmap.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -36,5 +36,5 @@
 #define KERN_ia32_MEMMAP_H_
 
-#include <arch/boot/memmap_struct.h>
+
 
 /* E820h memory range types */
@@ -61,7 +61,12 @@
 #define MEMMAP_E820_MAX_RECORDS  32
 
-#ifndef __ASM__
+#ifdef __ASM__
+
+#include <arch/boot/memmap_struct.ag.h>
+
+#else
 
 #include <stdint.h>
+#include <arch/boot/memmap_struct.h>
 
 extern e820memmap_t e820table[MEMMAP_E820_MAX_RECORDS];
Index: kernel/genarch/Makefile.inc
===================================================================
--- kernel/genarch/Makefile.inc	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/genarch/Makefile.inc	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -173,6 +173,6 @@
 	genarch/src/multiboot/multiboot2.c
 GENARCH_AUTOGENS_AG += \
-	genarch/include/genarch/multiboot/multiboot_memmap_struct.ag \
-	genarch/include/genarch/multiboot/multiboot_info_struct.ag
+	genarch/include/genarch/multiboot/multiboot_memmap_struct.ag.h \
+	genarch/include/genarch/multiboot/multiboot_info_struct.ag.h
 endif
 
Index: kernel/genarch/include/genarch/multiboot/multiboot.h
===================================================================
--- kernel/genarch/include/genarch/multiboot/multiboot.h	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ kernel/genarch/include/genarch/multiboot/multiboot.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -36,6 +36,4 @@
 #define KERN_MULTIBOOT_H_
 
-#include <genarch/multiboot/multiboot_memmap_struct.h>
-#include <genarch/multiboot/multiboot_info_struct.h>
 
 #define MULTIBOOT_HEADER_MAGIC  0x1badb002
@@ -52,5 +50,13 @@
 #define MULTIBOOT_INFO_FLAGS_MMAP	0x40
 
-#ifndef __ASM__
+#ifdef __ASM__
+
+#include <genarch/multiboot/multiboot_memmap_struct.ag.h>
+#include <genarch/multiboot/multiboot_info_struct.ag.h>
+
+#else
+
+#include <genarch/multiboot/multiboot_memmap_struct.h>
+#include <genarch/multiboot/multiboot_info_struct.h>
 
 #include <typedefs.h>
Index: rnel/genarch/include/genarch/multiboot/multiboot_info_struct.ag
===================================================================
--- kernel/genarch/include/genarch/multiboot/multiboot_info_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,81 +1,0 @@
-# Copyright (c) 2016 Jakub Jermar
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-#   notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-# - The name of the author may not be used to endorse or promote products
-#   derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-{
-        name : multiboot_info,
-        packed : true,
-
-        includes : [
-                {
-                        include : <typedefs.h>
-                },
-        ],
-
-        members : [
-                {
-                        name : flags,
-                        type : uint32_t
-                },
-                {
-                        name : mem_lower,
-                        type : uint32_t
-                },
-                {
-                        name : mem_upper,
-                        type : uint32_t
-                },
-                {
-                        name : boot_device,
-                        type : uint32_t
-                },
-                {
-                        name : cmd_line,
-                        type : uint32_t
-                },
-                {
-                        name : mods_count,
-                        type : uint32_t
-                },
-                {
-                        name : mods_addr,
-                        type : uint32_t
-                },
-                {
-                        name : syms,
-                        type : uint32_t,
-                        elements : 4,
-                },
-                {
-                        name : mmap_length,
-                        type : uint32_t,
-                },
-                {
-                        name : mmap_addr,
-                        type : uint32_t,
-                }
-        ]
-}
Index: kernel/genarch/include/genarch/multiboot/multiboot_info_struct.h
===================================================================
--- kernel/genarch/include/genarch/multiboot/multiboot_info_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/genarch/include/genarch/multiboot/multiboot_info_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,43 @@
+/* Copyright (c) 2016 Jakub Jermar
+ * All rights preserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <stdint.h>
+
+typedef struct multiboot_info {
+	uint32_t flags;
+	uint32_t mem_lower;
+	uint32_t mem_upper;
+	uint32_t boot_device;
+	uint32_t cmd_line;
+	uint32_t mods_count;
+	uint32_t mods_addr;
+	uint32_t syms[4];
+	uint32_t mmap_length;
+	uint32_t mmap_addr;
+} __attribute__((packed)) multiboot_info_t;
+
Index: rnel/genarch/include/genarch/multiboot/multiboot_memmap_struct.ag
===================================================================
--- kernel/genarch/include/genarch/multiboot/multiboot_memmap_struct.ag	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ 	(revision )
@@ -1,52 +1,0 @@
-# Copyright (c) 2016 Jakub Jermar
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-#   notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in the
-#   documentation and/or other materials provided with the distribution.
-# - The name of the author may not be used to endorse or promote products
-#   derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-{
-        name : multiboot_memmap,
-        packed : true,
-
-        includes : [
-                {
-                        include : <typedefs.h>
-                },
-                {
-                        include : <arch/boot/memmap_struct.h>,
-                        depends: arch/$(KARCH)/include/arch/boot/memmap_struct.h,
-                }
-        ],
-
-        members : [
-                {
-                        name : size,
-                        type : uint32_t
-                },
-                {
-                        name : mm_info,
-                        type : e820memmap_t
-                }
-        ]
-}
Index: kernel/genarch/include/genarch/multiboot/multiboot_memmap_struct.h
===================================================================
--- kernel/genarch/include/genarch/multiboot/multiboot_memmap_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ kernel/genarch/include/genarch/multiboot/multiboot_memmap_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,36 @@
+/* Copyright (c) 2016 Jakub Jermar
+ * All rights preserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+#include <stdint.h>
+#include <arch/boot/memmap_struct.h>
+
+typedef struct multiboot_memmap {
+	uint32_t size;
+	e820memmap_t mm_info;
+} __attribute__((packed)) multiboot_memmap_t;
+
Index: tools/autogen2.awk
===================================================================
--- tools/autogen2.awk	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ tools/autogen2.awk	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,23 @@
+#!/usr/bin/awk -f
+
+/}.*;/ {
+	if (($0 != "};") && ($0 != "} " struct_name "_t;")) {
+		print("Bad struct ending: " $0) > "/dev/stderr"
+		exit 1
+	}
+	print "DEFINE_STRUCT(" struct_name ", " toupper(struct_name) ")"
+	struct_name = ""
+}
+
+/;$/ {
+	if (struct_name != "") {
+		# Remove array subscript, if any.
+		sub("(\\[.*\\])?;", "", $0)
+		member = $NF
+		print "DEFINE_MEMBER(" struct_name ", " toupper(struct_name) ", " member ", " toupper(member) ")"
+	}
+}
+
+/^typedef struct .* \{/ {
+	struct_name = $3
+}
Index: tools/autogen2.sh
===================================================================
--- tools/autogen2.sh	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ tools/autogen2.sh	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -1,35 +1,57 @@
 #!/bin/sh
 
-filename="$1"
-struct_name="$2"
+# There's a bunch of restriction. The script assumes that the structure definition begins with "typedef struct struct_name {",
+# ends with "struct_name_t;", and that every word in between that ends with ";" is a struct member name with optional
+# array subscript. Error handling is mostly omitted for simplicity, so any input that does not follow these rules will result
+# in cryptic errors.
 
-members=`cat $filename | sed -n -e "1,/typedef struct $struct_name {/d" -e "/} ${struct_name}_t;/,$$d" -e "s/.* \([^ ]\+\);/\1/p"`
+echo $PWD
 
-ustruct_name=`echo "$struct_name" | awk '{print toupper($0)}'`
-dmembers=`echo "$members" | awk '{print "DEFINE_MEMBER(" $0 ", " toupper($0) ")" }'`
+input="$1"
+output="$2"
+toolsdir="$(readlink -f $(dirname "$0"))"
+awkscript="$toolsdir/autogen2.awk"
 
+# Set default value for $CC.
 if [ -z "${CC}" ]; then
 	CC=cc
 fi
 
+# If $CC is clang, we need to make sure integrated assembler is not used.
 if ( ${CC} --version | grep clang > /dev/null ) ; then
 	CFLAGS="${CFLAGS} -no-integrated-as"
 fi
 
-echo "/* Autogenerated file, do not modify. */"
-echo "#pragma once"
+# Tell the compiler to generate makefile dependencies.
+CFLAGS="${CFLAGS} -MD -MP -MT $output -MF $output.d"
 
-( ${CC} ${CFLAGS} -w -S -x c - -o - | sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' ) <<- EOF
+# Generate defines
+defs=`$awkscript -- $input || exit 1`
 
-#include "$filename"
+# Generate C file.
+cat > $output.c <<- EOF
 
-#define DEFINE_MEMBER(lc, uc) \
-	asm volatile ("/* #define ${ustruct_name}_OFFSET_"#uc" %0 */" :: "i" (__builtin_offsetof(struct $struct_name, lc))); \
-	asm volatile ("/* #define ${ustruct_name}_SIZE_"#uc" %0 */" :: "i" (sizeof((struct $struct_name){}.lc)));
+#include "`basename $input`"
+
+#define DEFINE_MEMBER(ls, us, lm, um) \
+	asm volatile ("/* #define "#us"_OFFSET_"#um" %0 */" :: "i" (__builtin_offsetof(struct ls, lm))); \
+	asm volatile ("/* #define "#us"_SIZE_"#um" %0 */" :: "i" (sizeof((struct ls){}.lm)));
+
+#define DEFINE_STRUCT(ls, us) \\
+	asm volatile ("/* #define "#us"_SIZE %0 */" :: "i" (sizeof(struct ls)));
 
 void autogen()
 {
-	$dmembers
+	$defs
 }
 
 EOF
+
+# Turn the C file into assembly.
+${CC} ${CFLAGS} -w -S -o $output.s $output.c || exit 1
+
+# Process the output.
+
+echo "/* Autogenerated file, do not modify. */" > $output
+echo "#pragma once" >> $output
+sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' < $output.s > $output || exit 1
Index: tools/autotool.py
===================================================================
--- tools/autotool.py	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ tools/autotool.py	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -681,6 +681,4 @@
 		sandbox_leave(owd)
 
-	common['AUTOGEN'] = "%s/autogen.py" % os.path.dirname(os.path.abspath(sys.argv[0]))
-
 	create_makefile(MAKEFILE, common)
 	create_header(HEADER, macros)
Index: uspace/lib/c/arch/amd64/include/libarch/fibril_context.h
===================================================================
--- uspace/lib/c/arch/amd64/include/libarch/fibril_context.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ uspace/lib/c/arch/amd64/include/libarch/fibril_context.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,51 @@
+
+/*****************************************************************************
+ * AUTO-GENERATED FILE, DO NOT EDIT!!!
+ * Generated by: tools/autogen.py
+ * Generated from: arch/amd64/include/libarch/fibril_context.ag
+ *****************************************************************************/
+
+#ifndef AUTOGEN_CONTEXT_H
+#define AUTOGEN_CONTEXT_H
+
+#ifndef __ASM__
+#include <stddef.h>
+#include <stdint.h>
+#endif
+
+#define CONTEXT_OFFSET_SP 0
+#define CONTEXT_SIZE_SP 8
+#define CONTEXT_OFFSET_PC 8
+#define CONTEXT_SIZE_PC 8
+#define CONTEXT_OFFSET_RBX 16
+#define CONTEXT_SIZE_RBX 8
+#define CONTEXT_OFFSET_RBP 24
+#define CONTEXT_SIZE_RBP 8
+#define CONTEXT_OFFSET_R12 32
+#define CONTEXT_SIZE_R12 8
+#define CONTEXT_OFFSET_R13 40
+#define CONTEXT_SIZE_R13 8
+#define CONTEXT_OFFSET_R14 48
+#define CONTEXT_SIZE_R14 8
+#define CONTEXT_OFFSET_R15 56
+#define CONTEXT_SIZE_R15 8
+#define CONTEXT_OFFSET_TLS 64
+#define CONTEXT_SIZE_TLS 8
+#define CONTEXT_SIZE 72
+
+#ifndef __ASM__
+typedef struct context {
+	uint64_t sp;
+	uint64_t pc;
+	uint64_t rbx;
+	uint64_t rbp;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+	uint64_t tls;
+} context_t;
+#endif
+
+#endif
+	
Index: uspace/lib/c/arch/amd64/include/libarch/istate_struct.h
===================================================================
--- uspace/lib/c/arch/amd64/include/libarch/istate_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ uspace/lib/c/arch/amd64/include/libarch/istate_struct.h	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,103 @@
+
+/*****************************************************************************
+ * AUTO-GENERATED FILE, DO NOT EDIT!!!
+ * Generated by: tools/autogen.py
+ * Generated from: arch/amd64/include/libarch/istate_struct.ag
+ *****************************************************************************/
+
+#ifndef AUTOGEN_ISTATE_H
+#define AUTOGEN_ISTATE_H
+
+#ifndef __ASM__
+#ifdef KERNEL
+#include <typedefs.h>
+#endif
+#ifndef KERNEL
+#include <stddef.h>
+#endif
+#ifndef KERNEL
+#include <stdint.h>
+#endif
+#endif
+
+#define ISTATE_OFFSET_RAX 0
+#define ISTATE_SIZE_RAX 8
+#define ISTATE_OFFSET_RBX 8
+#define ISTATE_SIZE_RBX 8
+#define ISTATE_OFFSET_RCX 16
+#define ISTATE_SIZE_RCX 8
+#define ISTATE_OFFSET_RDX 24
+#define ISTATE_SIZE_RDX 8
+#define ISTATE_OFFSET_RSI 32
+#define ISTATE_SIZE_RSI 8
+#define ISTATE_OFFSET_RDI 40
+#define ISTATE_SIZE_RDI 8
+#define ISTATE_OFFSET_RBP 48
+#define ISTATE_SIZE_RBP 8
+#define ISTATE_OFFSET_R8 56
+#define ISTATE_SIZE_R8 8
+#define ISTATE_OFFSET_R9 64
+#define ISTATE_SIZE_R9 8
+#define ISTATE_OFFSET_R10 72
+#define ISTATE_SIZE_R10 8
+#define ISTATE_OFFSET_R11 80
+#define ISTATE_SIZE_R11 8
+#define ISTATE_OFFSET_R12 88
+#define ISTATE_SIZE_R12 8
+#define ISTATE_OFFSET_R13 96
+#define ISTATE_SIZE_R13 8
+#define ISTATE_OFFSET_R14 104
+#define ISTATE_SIZE_R14 8
+#define ISTATE_OFFSET_R15 112
+#define ISTATE_SIZE_R15 8
+#define ISTATE_OFFSET_ALIGNMENT 120
+#define ISTATE_SIZE_ALIGNMENT 8
+#define ISTATE_OFFSET_RBP_FRAME 128
+#define ISTATE_SIZE_RBP_FRAME 8
+#define ISTATE_OFFSET_RIP_FRAME 136
+#define ISTATE_SIZE_RIP_FRAME 8
+#define ISTATE_OFFSET_ERROR_WORD 144
+#define ISTATE_SIZE_ERROR_WORD 8
+#define ISTATE_OFFSET_RIP 152
+#define ISTATE_SIZE_RIP 8
+#define ISTATE_OFFSET_CS 160
+#define ISTATE_SIZE_CS 8
+#define ISTATE_OFFSET_RFLAGS 168
+#define ISTATE_SIZE_RFLAGS 8
+#define ISTATE_OFFSET_RSP 176
+#define ISTATE_SIZE_RSP 8
+#define ISTATE_OFFSET_SS 184
+#define ISTATE_SIZE_SS 8
+#define ISTATE_SIZE 192
+
+#ifndef __ASM__
+typedef struct istate {
+	uint64_t rax;
+	uint64_t rbx;
+	uint64_t rcx;
+	uint64_t rdx;
+	uint64_t rsi;
+	uint64_t rdi;
+	uint64_t rbp;
+	uint64_t r8;
+	uint64_t r9;
+	uint64_t r10;
+	uint64_t r11;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+	uint64_t alignment;
+	uint64_t rbp_frame;
+	uint64_t rip_frame;
+	uint64_t error_word;
+	uint64_t rip;
+	uint64_t cs;
+	uint64_t rflags;
+	uint64_t rsp;
+	uint64_t ss;
+} istate_t;
+#endif
+
+#endif
+	
