🐖

LuaをFreeDOS用にgccを使ってビルドする

2022/02/13に公開

はじめに

LuaFreeDOS用にgccを使ってビルドしてみます。

そもそもできるかどうかもよくわかっていないので、
とりあえず以下のことができる環境を目指します。

  • FreeDOS上でビルドしたLua.exeを実行。print("hello")と入力してhelloと表示されること。

gccのインストール

ubuntu用のパッケージが提供されているようなので
こちらを利用しインストールします。

バージョンを表示されると以下のように表示されました。

$ ia16-elf-gcc --version
ia16-elf-gcc (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Lua

Lua Ver5.4が最新のようなのでこちらを使用します。

$ wget https://www.lua.org/ftp/lua-5.4.4.tar.gz

とりあえず、こんな感じでいけるのでは?と思ってやった修正

src/linit.csrc/Makefileを以下のように修正してみました。

src/Makefile
diff --git a/src/Makefile b/src/Makefile
index 1907381..ed8375e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -4,9 +4,9 @@
 # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

 # Your platform. See PLATS for possible values.
-PLAT= guess
+PLAT= dos

-CC= gcc -std=gnu99
+CC= ia16-elf-gcc
 CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS)
 LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
 LIBS= -lm $(SYSLIBS) $(MYLIBS)
@@ -14,13 +14,13 @@ LIBS= -lm $(SYSLIBS) $(MYLIBS)
 AR= ar rcu
 RANLIB= ranlib
 RM= rm -f
-UNAME= uname
+UNAME= dos

 SYSCFLAGS=
 SYSLDFLAGS=
 SYSLIBS=

-MYCFLAGS=
+MYCFLAGS= -mcmodel=medium
 MYLDFLAGS=
 MYLIBS=
 MYOBJS=
@@ -34,13 +34,13 @@ PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix

 LUA_A= liblua.a
 CORE_O=        lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o
-LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o
+LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o loadlib.o linit.o
 BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

-LUA_T= lua
-LUA_O= lua.o
+LUA_T= lua.exe
+LUA_O= lua.o

-LUAC_T=        luac
+LUAC_T=        luac.exe
 LUAC_O=        luac.o

 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
@@ -140,6 +140,9 @@ posix:
 SunOS solaris:
        $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl"

+dos:
+       $(MAKE) $(ALL) SYSLIBS="-mcmodel=medium -Wl,-E"
+
 # Targets that do not create files (not all makes understand .PHONY).
 .PHONY: all $(PLATS) help test clean default o a depend echo

@@ -218,5 +221,5 @@ lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
  ltable.h lvm.h ljumptab.h
 lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
  lobject.h ltm.h lzio.h
src/linit.c
diff --git a/src/linit.c b/src/linit.c
index 69808f8..e819496 100644
--- a/src/linit.c
+++ b/src/linit.c
@@ -43,13 +43,13 @@ static const luaL_Reg loadedlibs[] = {
   {LUA_GNAME, luaopen_base},
   {LUA_LOADLIBNAME, luaopen_package},
   {LUA_COLIBNAME, luaopen_coroutine},
-  {LUA_TABLIBNAME, luaopen_table},
-  {LUA_IOLIBNAME, luaopen_io},
-  {LUA_OSLIBNAME, luaopen_os},
-  {LUA_STRLIBNAME, luaopen_string},
-  {LUA_MATHLIBNAME, luaopen_math},
-  {LUA_UTF8LIBNAME, luaopen_utf8},
-  {LUA_DBLIBNAME, luaopen_debug},
+  // {LUA_TABLIBNAME, luaopen_table},
+  // {LUA_IOLIBNAME, luaopen_io},
+  // {LUA_OSLIBNAME, luaopen_os},
+  // {LUA_STRLIBNAME, luaopen_string},
+  // {LUA_MATHLIBNAME, luaopen_math},
+  // {LUA_UTF8LIBNAME, luaopen_utf8},
+  // {LUA_DBLIBNAME, luaopen_debug},
   {NULL, NULL}
 };

これでビルドしてみたら、リンクでエラーが出ました。

$ make
make[1]: Entering directory '/home/----------/lua/lua-5.4.4/src'
/bin/sh: 1: dos: not found
Guessing
/bin/sh: 1: dos: not found
make[2]: Entering directory '/home/----------/lua/lua-5.4.4/src'
make all SYSLIBS="-mcmodel=medium -Wl,-E"
make[3]: Entering directory '/home/----------/lua/lua-5.4.4/src'
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lapi.o lapi.c
In file included from lapi.h:11:0,
                 from lapi.c:19:
lapi.c: In function ‘lua_topointer’:
llimits.h:123:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define cast(t, exp) ((t)(exp))
                       ^
llimits.h:126:23: note: in expansion of macro ‘cast’
 #define cast_voidp(i) cast(void *, (i))
                       ^~~~
lapi.c:482:27: note: in expansion of macro ‘cast_voidp’
     case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
                           ^~~~~~~~~~
llimits.h:134:23: note: in expansion of macro ‘cast’
 #define cast_sizet(i) cast(size_t, (i))
                       ^~~~
lapi.c:482:38: note: in expansion of macro ‘cast_sizet’
     case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
                                      ^~~~~~~~~~
lapi.c: In function ‘lua_getiuservalue’:
lapi.c:812:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (n <= 0 || n > uvalue(o)->nuvalue) {
                   ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c lcode.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lctype.o lctype.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldebug.o ldebug.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldo.o ldo.c
In file included from lapi.h:12:0,
                 from ldo.c:19:
ldo.c: In function ‘luaD_callnoyield’:
lstate.h:112:14: warning: overflow in implicit constant conversion [-Woverflow]
 #define nyci (0x10000 | 1)
              ^
ldo.c:627:28: note: in expansion of macro ‘nyci’
   ccall(L, func, nResults, nyci);
                            ^~~~
ldo.c: In function ‘luaD_rawrunprotected’:
ldo.c:138:47: warning: argument ‘f’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
                                               ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldump.o ldump.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lfunc.o lfunc.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lgc.o lgc.c
lgc.c: In function ‘traverseudata’:
lgc.c:560:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (i = 0; i < u->nuvalue; i++)
                 ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c llex.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lmem.o lmem.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lobject.o lobject.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lopcodes.o lopcodes.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c lparser.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lstate.o lstate.c
In file included from lapi.h:11:0,
                 from lstate.c:18:
lstate.c: In function ‘luai_makeseed’:
llimits.h:123:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define cast(t, exp) ((t)(exp))
                       ^
llimits.h:134:23: note: in expansion of macro ‘cast’
 #define cast_sizet(i) cast(size_t, (i))
                       ^~~~
lstate.c:68:16: note: in expansion of macro ‘cast_sizet’
   { size_t t = cast_sizet(e); \
                ^~~~~~~~~~
lstate.c:77:3: note: in expansion of macro ‘addbuff’
   addbuff(buff, p, &lua_newstate);  /* public function */
   ^~~~~~~
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lstring.o lstring.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ltable.o ltable.c
In file included from ltable.c:38:0:
ltable.c: In function ‘mainpositionTV’:
llimits.h:78:39: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
                                       ^
ltable.h:13:32: note: in definition of macro ‘gnode’
 #define gnode(t,i) (&(t)->node[i])
                                ^
ltable.c:88:26: note: in expansion of macro ‘hashmod’
 #define hashpointer(t,p) hashmod(t, point2uint(p))
                          ^~~~~~~
ltable.c:88:37: note: in expansion of macro ‘point2uint’
 #define hashpointer(t,p) hashmod(t, point2uint(p))
                                     ^~~~~~~~~~
ltable.c:179:14: note: in expansion of macro ‘hashpointer’
       return hashpointer(t, f);
              ^~~~~~~~~~~
ltable.c: In function ‘luaH_realasize’:
ltable.c:260:19: warning: right shift count >= width of type [-Wshift-count-overflow]
     size |= (size >> 16);
                   ^~
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ltm.o ltm.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lundump.o lundump.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lvm.o lvm.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lzio.o lzio.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lauxlib.o lauxlib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lbaselib.o lbaselib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lcorolib.o lcorolib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldblib.o ldblib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o liolib.o liolib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o loadlib.o loadlib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o loadlib.o linit.o
ar: `u' modifier ignored since `D' is the default (see `U')
ranlib liblua.a
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lua.o lua.c
ia16-elf-gcc -o lua.exe   lua.o liblua.a -lm -mcmodel=medium -Wl,-E
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: /usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/lib/medium/libc.a(lib_a-signal.o): in function `_raise_r':
(.fartext.f.signal.c.34216$+0xc9): undefined reference to `_getpid_r'
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: R_386_OZSEG16 or R_386_OZRELSEG16 for symbol with no output section
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: lua.exe(.fartext.f.signal.c.34216$): relocation "_getpid_r+0x0 (type R_386_OZSEG16)" returns an unrecognized value %x
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: (.fartext.f.signal.c.34216$+0xd3): undefined reference to `_kill_r'
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: R_386_OZSEG16 or R_386_OZRELSEG16 for symbol with no output section
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: lua.exe(.fartext.f.signal.c.34216$): relocation "_kill_r+0x0 (type R_386_OZSEG16)" returns an unrecognized value %x
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:64: lua.exe] Error 1
make[3]: Leaving directory '/home/----------/lua/lua-5.4.4/src'
make[2]: *** [Makefile:144: dos] Error 2
make[2]: Leaving directory '/home/----------/lua/lua-5.4.4/src'
make[1]: *** [Makefile:99: guess] Error 2
make[1]: Leaving directory '/home/----------/lua/lua-5.4.4/src'
make: *** [Makefile:55: guess] Error 2
----------@ysgmbp01:~/lua/lua-5.4.4$

リンクエラーの原因を探らない

_getpid_r_kill_rが無いよ、と言われているようです。

ia16-elf-gcc -o lua.exe   lua.o liblua.a -lm -mcmodel=medium -Wl,-E
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: /usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/lib/medium/libc.a(lib_a-signal.o): in function `_raise_r':
(.fartext.f.signal.c.34216$+0xc9): undefined reference to `_getpid_r'
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: R_386_OZSEG16 or R_386_OZRELSEG16 for symbol with no output section
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: lua.exe(.fartext.f.signal.c.34216$): relocation "_getpid_r+0x0 (type R_386_OZSEG16)" returns an unrecognized value %x
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: (.fartext.f.signal.c.34216$+0xd3): undefined reference to `_kill_r'
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: R_386_OZSEG16 or R_386_OZRELSEG16 for symbol with no output section
/usr/lib/x86_64-linux-gnu/gcc/ia16-elf/6.3.0/../../../../../ia16-elf/bin/ld: lua.exe(.fartext.f.signal.c.34216$): relocation "_kill_r+0x0 (type R_386_OZSEG16)" returns an unrecognized value %x
collect2: error: ld returned 1 exit status

なぜこのようなエラーが出たのか、ということを調べるのは後回しにします。
最初に書いたように今回の目標は以下です。

  • FreeDOS上でビルドしたLua.exeを実行。print("hello")と入力してhelloと表示されること。

なんとなく(??) _getpid_r_kill_rprint("hello")には関係なさそうなので 適当に_getpid_r_kill_r`を自前で実装します。

_getpid_r_kill_rの実装

以下のように_getpid_r_kill_rを書きました。

getpid.c
void _getpid_r (void){
    return;
}

void _kill_r (void){
    return;
}

とりあえずリンクができればよいので

Makefileの修正

再度src/Makefileの修正をします。以下のようにしました。
getpid.oを追加しています。

diff --git a/src/Makefile b/src/Makefile
index 1907381..ed8375e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -4,9 +4,9 @@
 # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

 # Your platform. See PLATS for possible values.
-PLAT= guess
+PLAT= dos

-CC= gcc -std=gnu99
+CC= ia16-elf-gcc
 CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS)
 LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
 LIBS= -lm $(SYSLIBS) $(MYLIBS)
@@ -14,13 +14,13 @@ LIBS= -lm $(SYSLIBS) $(MYLIBS)
 AR= ar rcu
 RANLIB= ranlib
 RM= rm -f
-UNAME= uname
+UNAME= dos

 SYSCFLAGS=
 SYSLDFLAGS=
 SYSLIBS=

-MYCFLAGS=
+MYCFLAGS= -mcmodel=medium
 MYLDFLAGS=
 MYLIBS=
 MYOBJS=
@@ -34,13 +34,13 @@ PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix

 LUA_A= liblua.a
 CORE_O=        lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o
-LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o
+LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o loadlib.o linit.o
 BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

-LUA_T= lua
-LUA_O= lua.o
+LUA_T= lua.exe
+LUA_O= getpid.o lua.o

-LUAC_T=        luac
+LUAC_T=        luac.exe
 LUAC_O=        luac.o

 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
@@ -140,6 +140,9 @@ posix:
 SunOS solaris:
        $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl"

+dos:
+       $(MAKE) $(ALL) SYSLIBS="-mcmodel=medium -Wl,-E"
+
 # Targets that do not create files (not all makes understand .PHONY).
 .PHONY: all $(PLATS) help test clean default o a depend echo

@@ -218,5 +221,5 @@ lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
  ltable.h lvm.h ljumptab.h
 lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
  lobject.h ltm.h lzio.h
-
+getpid.o: getpid.c

再度ビルド

今度はエラーは発生せずlua.exeが作成されました。

$ make clean
make[1]: Entering directory '/home/linux----------/lua/lua-5.4.4/src'
rm -f liblua.a lua.exe luac.exe lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o loadlib.o linit.o  getpid.o lua.o luac.o
make[1]: Leaving directory '/home/linux----------/lua/lua-5.4.4/src'
linux----------@ysgmbp01:~/lua/lua-5.4.4$ make
make[1]: Entering directory '/home/linux----------/lua/lua-5.4.4/src'
/bin/sh: 1: dos: not found
Guessing
/bin/sh: 1: dos: not found
make[2]: Entering directory '/home/linux----------/lua/lua-5.4.4/src'
make all SYSLIBS="-mcmodel=medium -Wl,-E"
make[3]: Entering directory '/home/linux----------/lua/lua-5.4.4/src'
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lapi.o lapi.c
In file included from lapi.h:11:0,
                 from lapi.c:19:
lapi.c: In function ‘lua_topointer’:
llimits.h:123:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define cast(t, exp) ((t)(exp))
                       ^
llimits.h:126:23: note: in expansion of macro ‘cast’
 #define cast_voidp(i) cast(void *, (i))
                       ^~~~
lapi.c:482:27: note: in expansion of macro ‘cast_voidp’
     case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
                           ^~~~~~~~~~
llimits.h:134:23: note: in expansion of macro ‘cast’
 #define cast_sizet(i) cast(size_t, (i))
                       ^~~~
lapi.c:482:38: note: in expansion of macro ‘cast_sizet’
     case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
                                      ^~~~~~~~~~
lapi.c: In function ‘lua_getiuservalue’:
lapi.c:812:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (n <= 0 || n > uvalue(o)->nuvalue) {
                   ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c lcode.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lctype.o lctype.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldebug.o ldebug.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldo.o ldo.c
In file included from lapi.h:12:0,
                 from ldo.c:19:
ldo.c: In function ‘luaD_callnoyield’:
lstate.h:112:14: warning: overflow in implicit constant conversion [-Woverflow]
 #define nyci (0x10000 | 1)
              ^
ldo.c:627:28: note: in expansion of macro ‘nyci’
   ccall(L, func, nResults, nyci);
                            ^~~~
ldo.c: In function ‘luaD_rawrunprotected’:
ldo.c:138:47: warning: argument ‘f’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
                                               ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldump.o ldump.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lfunc.o lfunc.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lgc.o lgc.c
lgc.c: In function ‘traverseudata’:
lgc.c:560:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (i = 0; i < u->nuvalue; i++)
                 ^
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c llex.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lmem.o lmem.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lobject.o lobject.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lopcodes.o lopcodes.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium  -c lparser.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lstate.o lstate.c
In file included from lapi.h:11:0,
                 from lstate.c:18:
lstate.c: In function ‘luai_makeseed’:
llimits.h:123:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define cast(t, exp) ((t)(exp))
                       ^
llimits.h:134:23: note: in expansion of macro ‘cast’
 #define cast_sizet(i) cast(size_t, (i))
                       ^~~~
lstate.c:68:16: note: in expansion of macro ‘cast_sizet’
   { size_t t = cast_sizet(e); \
                ^~~~~~~~~~
lstate.c:77:3: note: in expansion of macro ‘addbuff’
   addbuff(buff, p, &lua_newstate);  /* public function */
   ^~~~~~~
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lstring.o lstring.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ltable.o ltable.c
In file included from ltable.c:38:0:
ltable.c: In function ‘mainpositionTV’:
llimits.h:78:39: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
                                       ^
ltable.h:13:32: note: in definition of macro ‘gnode’
 #define gnode(t,i) (&(t)->node[i])
                                ^
ltable.c:88:26: note: in expansion of macro ‘hashmod’
 #define hashpointer(t,p) hashmod(t, point2uint(p))
                          ^~~~~~~
ltable.c:88:37: note: in expansion of macro ‘point2uint’
 #define hashpointer(t,p) hashmod(t, point2uint(p))
                                     ^~~~~~~~~~
ltable.c:179:14: note: in expansion of macro ‘hashpointer’
       return hashpointer(t, f);
              ^~~~~~~~~~~
ltable.c: In function ‘luaH_realasize’:
ltable.c:260:19: warning: right shift count >= width of type [-Wshift-count-overflow]
     size |= (size >> 16);
                   ^~
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ltm.o ltm.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lundump.o lundump.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lvm.o lvm.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lzio.o lzio.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lauxlib.o lauxlib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lbaselib.o lbaselib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lcorolib.o lcorolib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o ldblib.o ldblib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o liolib.o liolib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o loadlib.o loadlib.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o loadlib.o linit.o
ar: `u' modifier ignored since `D' is the default (see `U')
ranlib liblua.a
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o getpid.o getpid.c
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o lua.o lua.c
ia16-elf-gcc -o lua.exe   getpid.o lua.o liblua.a -lm -mcmodel=medium -Wl,-E
ia16-elf-gcc -O2 -Wall -Wextra -DLUA_COMPAT_5_3  -mcmodel=medium   -c -o luac.o luac.c
ia16-elf-gcc -o luac.exe   luac.o liblua.a -lm -mcmodel=medium -Wl,-E
make[3]: Leaving directory '/home/linux----------/lua/lua-5.4.4/src'
make[2]: Leaving directory '/home/linux----------/lua/lua-5.4.4/src'
make[1]: Leaving directory '/home/linux----------/lua/lua-5.4.4/src'
$ file src/lua.exe
src/lua.exe: MS-DOS executable

実行

出来上がったlua.exeをFreeDOS(VirtualBox上)で実行してみます。
うまく動きました。

https://youtu.be/ZQ6sYD4Y2FQ

まとめ

できるかなと思い初めて見ましたが、何とか動いてよかったです。
print("hello")が動いただけなので、いろいろ動かしてみると、
問題があるような気もしますが、それはこれから見ていこうかと思います。
とりあえず、動いてよかったです。

Discussion