Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using doubles in the LUA53 build. #3225

Merged
merged 9 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/include/lwip/app/espconn_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#define espconn_manual_recv_disabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_MANUALRECV) != 0)
#define espconn_manual_recv_enabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_MANUALRECV) == 0)

extern int ets_task();
extern int ets_post();

/******************************************************************************
* FunctionName : espconn_kill_oldest_pcb
* Description : A oldest incoming connection has been killed.
Expand Down
8 changes: 8 additions & 0 deletions app/include/lwip/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ extern "C" {

typedef size_t mem_size_t;

void *pvPortMalloc (size_t sz, const char *, unsigned, bool);
void vPortFree (void *p, const char *, unsigned);
void *pvPortZalloc (size_t sz, const char *, unsigned);
void *pvPortRealloc (void *p, size_t n, const char *, unsigned);
void* pvPortCalloc(size_t count,size_t size,const char *,unsigned);
void* pvPortCallocIram(size_t count,size_t size,const char *,unsigned);
void *pvPortZallocIram (size_t sz, const char *, unsigned);

/* aliases for C library malloc() */
#define mem_init()
/* in case C library malloc() needs extra protection,
Expand Down
3 changes: 3 additions & 0 deletions app/include/mbedtls/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ extern "C" {
* \{
*/

void *pvPortCalloc(unsigned int count, unsigned int size, const char*, unsigned);
void vPortFree (void *p, const char *, unsigned);

#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
#include <stdio.h>
#include <stdlib.h>
Expand Down
7 changes: 7 additions & 0 deletions app/include/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ typedef enum{
NETCONN_STATE_SUMNUM
}netconn_state;

extern int __attribute__((weak)) espconn_mbedtls_parse_internal(int socket, sint8 error);
extern int __attribute__((weak)) espconn_mbedtls_parse_thread(int socket, int event, int error);

#if (!defined(lwIP_unlikely))
#define lwIP_unlikely(Expression) !!(Expression)
#endif
Expand Down Expand Up @@ -330,4 +333,8 @@ uint32_t lwip_getul(char *str);
#define close(s) lwip_close(s)
#define getul(s) lwip_getul(s)

extern int system_overclock(void);
extern int system_restoreclock(void);
extern char *sys_itoa(int n);

#endif /* ESPCONN_SOCKT_H_ */
41 changes: 27 additions & 14 deletions app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,34 @@
//#define DISABLE_STARTUP_BANNER


// Three separate build variants are now supported. The main difference is in the
// processing of numeric data types. If LUA_NUMBER_INTEGRAL is defined, then
// When using Lua 5.1, two different builds are now supported.
// The main difference is in the // processing of numeric data types.
// If LUA_NUMBER_INTEGRAL is defined, then
// all numeric calculations are done in integer, with divide being an integer
// operations, and decimal fraction constants are illegal. Otherwise all
// numeric operations use floating point, though they are exact for integer
// expressions < 2^53.

// The main advantage of INTEGRAL builds is that the basic internal storage unit,
// the TValue, is 8 bytes long. We have now reduced the size of FP TValues to
// 12 bytes rather than the previous 16 as this gives a material RAM saving with
// no performance loss. However, you can define LUA_DWORD_ALIGNED_TVALUES and
// this will force 16 byte TValues on FP builds.
// operation, and decimal fraction constants are illegal.
// Otherwise all floating point operations use doubles. All integer values
// can be represented exactly in floating point.

//#define LUA_NUMBER_INTEGRAL
//#define LUA_DWORD_ALIGNED_TVALUES

// When using Lua 5.3, two different builds are now supported.
// The main difference is in the processing of numeric data types.
// If LUA_NUMBER_64BITS is defined, then doubles are used to hold floating
// point numbers. Integers under 2^53 are representable exactly in doubles.
// Integers are held in 64-bit variables.
// Otherwise all floating point operations use floats. Only integers under 2^24
// can be represented exactly in floating point. Integers are represented in 32 bit variables.
// Note that Lua 5.3 also supports Integers natively, but you have to be careful
// not to promote an integer to a floating point variable if you are using a float build
// as you can lose precision.

//#define LUA_NUMBER_64BITS

// The main advantage of INTEGRAL builds and non 64BITS builds is that the basic internal
// storage unit, the TValue, is 8 bytes long. For 64BITS builds, we have now reduced
// the size of FP TValues to 12 bytes rather than the previous 16 as this gives a
// material RAM saving with no performance loss.
//


// The Lua Flash Store (LFS) allows you to store Lua code in Flash memory and
Expand Down Expand Up @@ -138,7 +151,7 @@

// The net module optionally offers net info functionnality. Uncomment the following
// to enable the functionnality.
#define NET_PING_ENABLE
#define NET_PING_ENABLE

// The WiFi module optionally offers an enhanced level of WiFi connection
// management, using internal timer callbacks. Whilst many Lua developers
Expand Down Expand Up @@ -251,7 +264,7 @@
#define READLINE_INTERVAL 80
#define STRBUF_DEFAULT_INCREMENT 3
#define LUA_USE_BUILTIN_DEBUG_MINIMAL // for debug.getregistry() and debug.traceback()

#if defined(DEVELOPMENT_TOOLS) && defined(DEVELOPMENT_USE_GDB)
extern void LUA_DEBUG_HOOK (void);
#define lua_assert(x) ((x) ? (void) 0 : LUA_DEBUG_HOOK ())
Expand Down
4 changes: 4 additions & 0 deletions app/lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
#endif // #if !defined LUA_INTEGRAL_LONGLONG
#endif // #if !defined LUA_NUMBER_INTEGRAL

#ifdef LUA_NUMBER_64BITS
#error Lua 5.1 does not support 64 bit inetegers.
#endif

/*
@@ LUA_API is a mark for all core API functions.
@@ LUALIB_API is a mark for all standard library functions.
Expand Down
2 changes: 1 addition & 1 deletion app/lua53/ldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void DumpNumber (lua_Number x, DumpState *D) {
** 0TTTNNNN or 1TTTNNNN (1NNNNNNN)* 0NNNNNNN
*/
static void DumpIntTT (lu_byte tt, lua_Integer y, DumpState *D) {
int x = y < 0 ? -(y + 1) : y;
lua_Integer x = y < 0 ? -(y + 1) : y;
lu_byte buf[sizeof(lua_Integer) + 3];
lu_byte *b = buf + sizeof(buf) - 1;
*b-- = x & 0x7f; x >>= 7;
Expand Down
7 changes: 6 additions & 1 deletion app/lua53/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,17 @@ typedef union Value {

#define TValuefields Value value_; int tt_

#ifdef LUA_USE_ESP
# pragma pack(4)
#endif

typedef struct lua_TValue {
TValuefields;
} TValue;


#ifdef LUA_USE_ESP
# pragma pack()
#endif

/* macro defining a nil value */
#define NILCONSTANT {NULL}, LUA_TNIL
Expand Down
12 changes: 10 additions & 2 deletions app/lua53/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@
# define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
#endif
#endif
# define LUA_INT_TYPE LUA_INT_INT
#ifdef LUA_NUMBER_64BITS
# define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
# define LUA_INT_TYPE LUA_INT_LONGLONG
#else
# define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
//# define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
# define LUA_INT_TYPE LUA_INT_INT
#endif

#ifdef LUA_NUMBER_INTEGRAL
#error LUA_NUMBER_INTEGRAL is not supported in LUA5.3 builds
#endif

/*
** Configuration for Paths.
Expand Down
1 change: 1 addition & 0 deletions app/mbedtls/app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEN_LIBS = libapp.a

endif

STD_CFLAGS=-std=gnu11

#############################################################
# Configuration i.e. compile options etc.
Expand Down
2 changes: 2 additions & 0 deletions app/mbedtls/app/espconn_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;

#include "sys/socket.h"
#include "sys/espconn_mbedtls.h"
#include "lwip/app/espconn_tcp.h"


static os_event_t lwIPThreadQueue[lwIPThreadQueueLen];
static bool lwIPThreadFlag = false;
Expand Down
4 changes: 4 additions & 0 deletions app/mbedtls/app/lwIPSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "ets_sys.h"
#include "os_type.h"
#include <ctype.h>

#include "lwip/mem.h"
#include "sys/socket.h"
Expand All @@ -42,6 +43,9 @@
static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#endif

void *pvPortZalloc (size_t sz, const char *, unsigned);
void vPortFree (void *p, const char *, unsigned);

/** The global array of available sockets */
static lwIP_sock sockets[NUM_SOCKETS];

Expand Down
2 changes: 1 addition & 1 deletion app/net/nodemcu_mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ mdns_set_servicename(const char *name) {
char tmpBuf[128];
os_sprintf(tmpBuf, "_%s._tcp.local", name);
if (service_name_with_suffix) {
os_free(service_name_with_suffix);
os_free((void *) service_name_with_suffix);
}
service_name_with_suffix = strdup(tmpBuf);
}
Expand Down
24 changes: 21 additions & 3 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,31 @@ editing `BIT_RATE_DEFAULT` in `app/include/user_config.h`:
Note that, by default, the firmware runs an auto-baudrate detection algorithm so that typing a few characters at boot time will cause
the firmware to lock onto that baud rate (between 1200 and 230400).

### Integer build
By default a build will be generated supporting floating-point variables.
### Double build (Lua 5.3 only)
By default a build will be generated supporting floating point variables (floats) and integers.
To increase the precision of the floating point variables, a double build can be created. This
is also the default in the Lua 5.1 builds. The downside is that more memory is consumed when
storing variables.
You can change this
either by uncommenting `LUA_NUMBER_64BITS` in `app/include/user_config.h`:

```c
//#define LUA_NUMBER_64BITS
```

OR by overriding this with the `make` command

```
make EXTRA_CCFLAGS="-DLUA_NUMBER_64BITS ....
```

### Integer build (Lua 5.1 only)
By default a build will be generated supporting floating-point variables (doubles).
To reduce memory size an integer build can be created. You can change this
either by uncommenting `LUA_NUMBER_INTEGRAL` in `app/include/user_config.h`:

```c
#define LUA_NUMBER_INTEGRAL
//#define LUA_NUMBER_INTEGRAL
```

OR by overriding this with the `make` command as it's [done during the CI
Expand Down
25 changes: 22 additions & 3 deletions docs/compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,30 @@ facilitates simple OTA updates to an LFS based Lua application; the absolute for
facilitates factory installation of LFS based applications.

Also note that the `app/lua/luac_cross` make and Makefile can be executed to build
just the `luac.cross` image. You must first ensure that the following option in
`app/include/user_config.h` is matched to your target configuration:
just the `luac.cross` image. You must first ensure that the following options in
`app/include/user_config.h` are matched to your target configuration:

```c
//#define LUA_NUMBER_INTEGRAL // uncomment if you want an integer build
// When using Lua 5.1, two different builds are now supported.
// The main difference is in the // processing of numeric data types.
// If LUA_NUMBER_INTEGRAL is defined, then
// all numeric calculations are done in integer, with divide being an integer
// operation, and decimal fraction constants are illegal.
// Otherwise all floating point operations use doubles. All integer values
// can be represented exactly in floating point.

//#define LUA_NUMBER_INTEGRAL

// When using Lua 5.3, two different builds are now supported.
// If LUA_NUMBER_64BITS is defined, then doubles are used to hold floating
// point numbers and integers are 64 bits long. Integers smaller than 2^53 can be i
// converted to doubles and back without any loss of precision.
// Otherwise all floating point operations use floats, and integers are 32-bit.
// Only integers under 2^24 can be represented exactly in floating point.
// Note that Lua 5.3 also supports Integers natively, but you have to be careful
// not to promote an integer to a floating point variable as you can lose precision.

//#define LUA_NUMBER_64BITS
```

Note that the use of LFS and the LFS region size is now configured through the partition table.
Expand Down
4 changes: 4 additions & 0 deletions tools/update_buildinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ cat > $TEMPFILE << EndOfMessage
#ifdef LUA_NUMBER_INTEGRAL
#define BUILDINFO_BUILD_TYPE "integer"
#else
#ifdef LUA_NUMBER_64BITS
#define BUILDINFO_BUILD_TYPE "double"
#else
#define BUILDINFO_BUILD_TYPE "float"
#endif
#endif

#define USER_PROLOG "$USER_PROLOG"
#define BUILDINFO_BRANCH "$BRANCH"
Expand Down