99
1010#include "m3_api_libc.h"
1111
12- #include "m3_api_defs.h"
1312#include "m3_env.h"
1413#include "m3_exception.h"
1514
1615#include <time.h>
1716#include <errno.h>
1817#include <stdio.h>
1918
19+ typedef uint32_t wasm_ptr_t ;
20+ typedef uint32_t wasm_size_t ;
2021
2122m3ApiRawFunction (m3_libc_abort )
2223{
@@ -35,9 +36,9 @@ m3ApiRawFunction(m3_libc_memset)
3536{
3637 m3ApiReturnType (int32_t )
3738
38- m3ApiGetArgMem (void * , i_ptr )
39- m3ApiGetArg (int32_t , i_value )
40- m3ApiGetArg (int32_t , i_size )
39+ m3ApiGetArgMem (void * , i_ptr )
40+ m3ApiGetArg (int32_t , i_value )
41+ m3ApiGetArg (wasm_size_t , i_size )
4142
4243 m3ApiCheckMem (i_ptr , i_size );
4344
@@ -49,9 +50,9 @@ m3ApiRawFunction(m3_libc_memmove)
4950{
5051 m3ApiReturnType (int32_t )
5152
52- m3ApiGetArgMem (void * , o_dst )
53- m3ApiGetArgMem (void * , i_src )
54- m3ApiGetArg (int32_t , i_size )
53+ m3ApiGetArgMem (void * , o_dst )
54+ m3ApiGetArgMem (void * , i_src )
55+ m3ApiGetArg (wasm_size_t , i_size )
5556
5657 m3ApiCheckMem (o_dst , i_size );
5758 m3ApiCheckMem (i_src , i_size );
@@ -64,8 +65,8 @@ m3ApiRawFunction(m3_libc_print)
6465{
6566 m3ApiReturnType (uint32_t )
6667
67- m3ApiGetArgMem (void * , i_ptr )
68- m3ApiGetArg (uint32_t , i_size )
68+ m3ApiGetArgMem (void * , i_ptr )
69+ m3ApiGetArg (wasm_size_t , i_size )
6970
7071 m3ApiCheckMem (i_ptr , i_size );
7172
@@ -75,11 +76,114 @@ m3ApiRawFunction(m3_libc_print)
7576 m3ApiReturn (i_size );
7677}
7778
79+ static
80+ void internal_itoa (int n , char s [], int radix )
81+ {
82+ static char const HEXDIGITS [0x10 ] = {
83+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
84+ '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'
85+ };
86+
87+ int i , j , sign ;
88+ char c ;
89+
90+ if ((sign = n ) < 0 ) { n = - n ; }
91+ i = 0 ;
92+ do {
93+ s [i ++ ] = HEXDIGITS [n % radix ];
94+ } while ((n /= radix ) > 0 );
95+
96+ if (sign < 0 ) { s [i ++ ] = '-' ; }
97+ s [i ] = '\0' ;
98+
99+ // reverse
100+ for (i = 0 , j = strlen (s )- 1 ; i < j ; i ++ , j -- ) {
101+ c = s [i ];
102+ s [i ] = s [j ];
103+ s [j ] = c ;
104+ }
105+ }
106+
107+ m3ApiRawFunction (m3_libc_printf )
108+ {
109+ m3ApiReturnType (int32_t )
110+
111+ m3ApiGetArgMem (const char * , i_fmt )
112+ m3ApiGetArgMem (wasm_ptr_t * , i_args )
113+
114+ if (m3ApiIsNullPtr (i_fmt )) {
115+ m3ApiReturn (0 );
116+ }
117+
118+ m3ApiCheckMem (i_fmt , 1 );
119+ size_t fmt_len = strnlen (i_fmt , 1024 );
120+ m3ApiCheckMem (i_fmt , fmt_len + 1 ); // include `\0`
121+
122+ FILE * file = stdout ;
123+
124+ int32_t length = 0 ;
125+ char ch ;
126+ while ((ch = * i_fmt ++ )) {
127+ if ( '%' != ch ) {
128+ putc (ch , file );
129+ length ++ ;
130+ continue ;
131+ }
132+ ch = * i_fmt ++ ;
133+ switch (ch ) {
134+ case 'c' : {
135+ m3ApiCheckMem (i_args , sizeof (wasm_ptr_t ));
136+ char char_temp = * i_args ++ ;
137+ fputc (char_temp , file );
138+ length ++ ;
139+ break ;
140+ }
141+ case 'd' :
142+ case 'x' : {
143+ m3ApiCheckMem (i_args , sizeof (wasm_ptr_t ));
144+ int int_temp = * i_args ++ ;
145+ char buffer [32 ] = { 0 , };
146+ internal_itoa (int_temp , buffer , (ch == 'x' ) ? 16 : 10 );
147+ fputs (buffer , file );
148+ length += strnlen (buffer , sizeof (buffer ));
149+ break ;
150+ }
151+ case 's' : {
152+ m3ApiCheckMem (i_args , sizeof (wasm_ptr_t ));
153+ const char * string_temp ;
154+ size_t string_len ;
155+
156+ string_temp = (const char * )m3ApiOffsetToPtr (* i_args ++ );
157+ if (m3ApiIsNullPtr (string_temp )) {
158+ string_temp = "(null)" ;
159+ string_len = 6 ;
160+ } else {
161+ string_len = strnlen (string_temp , 1024 );
162+ m3ApiCheckMem (string_temp , string_len + 1 );
163+ }
164+
165+ fwrite (string_temp , 1 , string_len , file );
166+ length += string_len ;
167+ break ;
168+ default :
169+ fputc (ch , file );
170+ length ++ ;
171+ break ;
172+ }
173+ }
174+ }
175+
176+ m3ApiReturn (length );
177+ }
178+
78179m3ApiRawFunction (m3_libc_clock_ms )
79180{
80181 m3ApiReturnType (uint32_t )
81-
182+ #ifdef CLOCKS_PER_SEC
82183 m3ApiReturn (clock () / (CLOCKS_PER_SEC /1000 ));
184+ #else
185+ m3ApiReturn (clock ());
186+ #endif
83187}
84188
85189static
@@ -128,6 +232,7 @@ _ (SuppressLookupFailure (m3_LinkRawFunction (module, env, "_memcpy",
128232_ (SuppressLookupFailure (m3_LinkRawFunction (module , env , "_abort" , "v()" , & m3_libc_abort )));
129233_ (SuppressLookupFailure (m3_LinkRawFunction (module , env , "_exit" , "v(i)" , & m3_libc_exit )));
130234_ (SuppressLookupFailure (m3_LinkRawFunction (module , env , "clock_ms" , "i()" , & m3_libc_clock_ms )));
235+ _ (SuppressLookupFailure (m3_LinkRawFunction (module , env , "printf" , "i(**)" , & m3_libc_printf )));
131236
132237_catch :
133238 return result ;
0 commit comments