From 76a203d4df1d4e968512dc547fff088e2d6378aa Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 28 Apr 2023 21:12:42 +0200 Subject: [PATCH] api change: rename builtin func sqrt16 to sqrtw --- .../src/prog8/code/core/BuiltinFunctions.kt | 2 +- .../codegen/cpu6502/BuiltinFunctionsAsmGen.kt | 10 ++--- .../codegen/intermediate/BuiltinFuncGen.kt | 4 +- compiler/res/prog8lib/prog8_funcs.asm | 6 +-- .../src/prog8/compiler/BuiltinFunctions.kt | 2 +- compiler/test/arithmetic/builtins.p8 | 4 +- compiler/test/ast/TestIntermediateAst.kt | 2 +- docs/source/programming.rst | 3 +- docs/source/todo.rst | 37 +++++++++++++++++-- examples/cx16/circles.p8 | 2 +- examples/textelite.p8 | 2 +- examples/vm/textelite.p8 | 2 +- syntax-files/IDEA/Prog8.xml | 4 +- syntax-files/NotepadPlusPlus/Prog8.xml | 2 +- syntax-files/Vim/prog8_builtins.vim | 2 +- 15 files changed, 58 insertions(+), 26 deletions(-) diff --git a/codeCore/src/prog8/code/core/BuiltinFunctions.kt b/codeCore/src/prog8/code/core/BuiltinFunctions.kt index 5b9b062cf..f3c617d7c 100644 --- a/codeCore/src/prog8/code/core/BuiltinFunctions.kt +++ b/codeCore/src/prog8/code/core/BuiltinFunctions.kt @@ -85,7 +85,7 @@ val BuiltinFunctions: Map = mapOf( // normal functions follow: "sizeof" to FSignature(true, listOf(FParam("object", DataType.values())), DataType.UBYTE), "sgn" to FSignature(true, listOf(FParam("value", NumericDatatypesNoBool)), DataType.BYTE), - "sqrt16" to FSignature(true, listOf(FParam("value", arrayOf(DataType.UWORD))), DataType.UBYTE), + "sqrtw" to FSignature(true, listOf(FParam("value", arrayOf(DataType.UWORD))), DataType.UBYTE), "divmod" to FSignature(false, listOf(FParam("number", arrayOf(DataType.UBYTE)), FParam("divident", arrayOf(DataType.UBYTE)), FParam("division", arrayOf(DataType.UBYTE)), FParam("remainder", arrayOf(DataType.UBYTE))), null), "divmodw" to FSignature(false, listOf(FParam("number", arrayOf(DataType.UWORD)), FParam("divident", arrayOf(DataType.UWORD)), FParam("division", arrayOf(DataType.UWORD)), FParam("remainder", arrayOf(DataType.UWORD))), null), "any" to FSignature(true, listOf(FParam("values", ArrayDatatypes)), DataType.UBYTE), diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 70abf2110..e7009ed50 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -34,7 +34,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, "abs" -> funcAbs(fcall, resultToStack, resultRegister, sscope) "any", "all" -> funcAnyAll(fcall, resultToStack, resultRegister, sscope) "sgn" -> funcSgn(fcall, resultToStack, resultRegister, sscope) - "sqrt16" -> funcSqrt16(fcall, resultToStack, resultRegister, sscope) + "sqrtw" -> funcSqrtw(fcall, resultToStack, resultRegister, sscope) "divmod" -> funcDivmod(fcall) "divmodw" -> funcDivmodW(fcall) "rol" -> funcRol(fcall) @@ -298,13 +298,13 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, asmgen.translateNormalAssignment(assign, fcall.definingISub()) } - private fun funcSqrt16(fcall: PtBuiltinFunctionCall, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: IPtSubroutine?) { + private fun funcSqrtw(fcall: PtBuiltinFunctionCall, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: IPtSubroutine?) { translateArguments(fcall, scope) if(resultToStack) - asmgen.out(" jsr prog8_lib.func_sqrt16_stack") + asmgen.out(" jsr prog8_lib.func_sqrtw_stack") else { - asmgen.out(" jsr prog8_lib.func_sqrt16_into_A") - assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, false, fcall.position, scope, asmgen), CpuRegister.A, false) + asmgen.out(" jsr prog8_lib.func_sqrtw_into_A") + assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, false, fcall.position, scope, asmgen), CpuRegister.A) } } diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt index 6f6329c53..43008e04c 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt @@ -16,7 +16,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe "abs" -> funcAbs(call) "cmp" -> funcCmp(call) "sgn" -> funcSgn(call) - "sqrt16" -> funcSqrt16(call) + "sqrtw" -> funcSqrtw(call) "divmod" -> funcDivmod(call, IRDataType.BYTE) "divmodw" -> funcDivmod(call, IRDataType.WORD) "pop" -> funcPop(call) @@ -209,7 +209,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe return ExpressionCodeResult(result, vmDt, resultReg, -1) } - private fun funcSqrt16(call: PtBuiltinFunctionCall): ExpressionCodeResult { + private fun funcSqrtw(call: PtBuiltinFunctionCall): ExpressionCodeResult { val result = mutableListOf() val tr = exprGen.translateExpression(call.args.single()) addToResult(result, tr, tr.resultReg, -1) diff --git a/compiler/res/prog8lib/prog8_funcs.asm b/compiler/res/prog8lib/prog8_funcs.asm index 5728d0bc1..4478cf293 100644 --- a/compiler/res/prog8lib/prog8_funcs.asm +++ b/compiler/res/prog8lib/prog8_funcs.asm @@ -203,14 +203,14 @@ func_sign_w_stack .proc rts .pend -func_sqrt16_stack .proc - jsr func_sqrt16_into_A +func_sqrtw_stack .proc + jsr func_sqrtw_into_A sta P8ESTACK_LO,x dex rts .pend -func_sqrt16_into_A .proc +func_sqrtw_into_A .proc ; integer square root from http://6502org.wikidot.com/software-math-sqrt sta P8ZP_SCRATCH_W1 sty P8ZP_SCRATCH_W1+1 diff --git a/compiler/src/prog8/compiler/BuiltinFunctions.kt b/compiler/src/prog8/compiler/BuiltinFunctions.kt index 1c80b7889..e22ee39f0 100644 --- a/compiler/src/prog8/compiler/BuiltinFunctions.kt +++ b/compiler/src/prog8/compiler/BuiltinFunctions.kt @@ -19,7 +19,7 @@ internal val constEvaluatorsForBuiltinFuncs: Map "len" to ::builtinLen, "sizeof" to ::builtinSizeof, "sgn" to ::builtinSgn, - "sqrt16" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { sqrt(it.toDouble()) } }, + "sqrtw" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { sqrt(it.toDouble()) } }, "any" to { a, p, prg -> collectionArg(a, p, prg, ::builtinAny) }, "all" to { a, p, prg -> collectionArg(a, p, prg, ::builtinAll) }, "lsb" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { x: Int -> (x and 255).toDouble() } }, diff --git a/compiler/test/arithmetic/builtins.p8 b/compiler/test/arithmetic/builtins.p8 index f8b6737c6..3e08b7b95 100644 --- a/compiler/test/arithmetic/builtins.p8 +++ b/compiler/test/arithmetic/builtins.p8 @@ -298,10 +298,10 @@ main { uw = 50000 - ub = sqrt16(uw) + ub = sqrtw(uw) txt.print_ub(ub) txt.nl() - ub = zero+sqrt16(uw)*1+zero + ub = zero+sqrtw(uw)*1+zero txt.print_ub(ub) txt.nl() diff --git a/compiler/test/ast/TestIntermediateAst.kt b/compiler/test/ast/TestIntermediateAst.kt index 13bb420fb..ed30b7c7e 100644 --- a/compiler/test/ast/TestIntermediateAst.kt +++ b/compiler/test/ast/TestIntermediateAst.kt @@ -21,7 +21,7 @@ class TestIntermediateAst: FunSpec({ ubyte cc ubyte[] array = [1,2,3] cc = 11 in array - cc = sqrt16(lsb(cc)) + cc = sqrtw(lsb(cc)) } } """ diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 17dc2668a..ffe57865b 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -768,9 +768,10 @@ abs (x) sgn (x) Get the sign of the value. Result is -1, 0 or 1 (negative, zero, positive). -sqrt16 (w) +sqrtw (w) 16 bit unsigned integer Square root. Result is unsigned byte. To do the reverse, squaring an integer, just write ``x*x``. + Floating point square root has its own function `floats.sqrt()` divmod (number, divident, division, remainder) Performs division and remainder calculation in a single call. This is faster than using separate '/' and '%' calculations. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 874d4bab1..161cc133d 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,11 @@ TODO ==== +New major release 9.0 +^^^^^^^^^^^^^^^^^^^^^^ +- renamed builtin function sqrt16 to sqrtw for consistency + + For next minor release ^^^^^^^^^^^^^^^^^^^^^^ - try to optimize newexpr a bit more @@ -8,9 +13,35 @@ For next minor release ... -For 9.0 major changes are being made in the "version_9" branch. Look there. -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - +For 9.0 major changes +^^^^^^^^^^^^^^^^^^^^^ +- copy (not move) the CBM kernal romsubs to a new 'cbm' block so programs on c128 and cx16 can also + simply refer to cbm.CHROUT rather than c64.CHROUT which looks a bit silly on the non-c64 cbm systems. + we keep the old definitions intact because of backwards compatibility reasons. +- try to reintroduce builtin functions max/maxw/min/minw that take 2 args and return the largest/smallest of them. + This is a major change because it will likely break existing code that is now using min and max as variable names. + Also add optimization that changes the word variant to byte variant if the operands are bytes. +- 6502 codegen: see if we can let for loops skip the loop if startvar>endvar, without adding a lot of code size/duplicating the loop condition. + It is documented behavior to now loop 'around' $00 but it's too easy to forget about! + Lot of work because of so many special cases in ForLoopsAsmgen..... + (vm codegen already behaves like this!) +- ?? get rid of the disknumber parameter everywhere in diskio, make it a configurable variable that defaults to 8. + the large majority of users will only deal with a single disk drive so why not make it easier for them. + But see complaint on github https://github.com/irmen/prog8/issues/106 +- duplicate diskio for cx16 (get rid of cx16diskio, just copy diskio and tweak everything) + documentation +- get f_seek_w working like in the BASIC program - this needs the changes to diskio.f_open to use suffixes ,p,m +- add special (u)word array type (or modifier such as @fast? ) that puts the array into memory as 2 separate byte-arrays 1 for LSB 1 for MSB -> allows for word arrays of length 256 and faster indexing + this is an enormous amout of work, if this type is to be treated equally as existing (u)word , because all expression / lookup / assignment routines need to know about the distinction.... + So maybe only allow the bare essentials? (store, get, bitwise operations?) +- Some more support for (64tass) SEGMENTS ? + - (What, how, isn't current BSS support enough?) + - Add a mechanism to allocate variables into golden ram (or segments really) (see GoldenRam class) + - maybe treat block "golden" in a special way: can only contain vars, every var will be allocated in the Golden ram area? + - maybe or may not needed: the variables can NOT have initialization values, they will all be set to zero on startup (simple memset) + just initialize them yourself in start() if you need a non-zero value . + - OR.... do all this automatically if 'golden' is enabled as a compiler option? So compiler allocates in ZP first, then Golden Ram, then regular ram + - OR.... make all this more generic and use some %segment option to create real segments for 64tass? + - (need separate step in codegen and IR to write the "golden" variables) Need help with diff --git a/examples/cx16/circles.p8 b/examples/cx16/circles.p8 index 3a69424c2..ce0901fdf 100644 --- a/examples/cx16/circles.p8 +++ b/examples/cx16/circles.p8 @@ -66,7 +66,7 @@ main { word dy = y as word - circle_y[cix] uword sqx = dx*dx as uword uword sqy = dy*dy as uword - return sqrt16(sqx + sqy) + return sqrtw(sqx + sqy) } ; sub distance(ubyte cix) -> uword { diff --git a/examples/textelite.p8 b/examples/textelite.p8 index 6420926f9..8eaaccc44 100644 --- a/examples/textelite.p8 +++ b/examples/textelite.p8 @@ -817,7 +817,7 @@ planet { else ay=y-py ay /= 2 - ubyte d = sqrt16(ax*ax + ay*ay) + ubyte d = sqrtw(ax*ax + ay*ay) if d>63 return 255 return d*4 diff --git a/examples/vm/textelite.p8 b/examples/vm/textelite.p8 index 1cb19fdb5..aecfa9d5f 100644 --- a/examples/vm/textelite.p8 +++ b/examples/vm/textelite.p8 @@ -744,7 +744,7 @@ planet { else ay=y-py ay /= 2 - ubyte d = sqrt16(ax*ax + ay*ay) + ubyte d = sqrtw(ax*ax + ay*ay) if d>63 return 255 return d*4 diff --git a/syntax-files/IDEA/Prog8.xml b/syntax-files/IDEA/Prog8.xml index 864051539..041177176 100644 --- a/syntax-files/IDEA/Prog8.xml +++ b/syntax-files/IDEA/Prog8.xml @@ -14,10 +14,10 @@ - + - \ No newline at end of file + diff --git a/syntax-files/NotepadPlusPlus/Prog8.xml b/syntax-files/NotepadPlusPlus/Prog8.xml index 74f8de550..db845a0cb 100644 --- a/syntax-files/NotepadPlusPlus/Prog8.xml +++ b/syntax-files/NotepadPlusPlus/Prog8.xml @@ -27,7 +27,7 @@ void const str byte ubyte bool word uword float zp shared requirezp %address %asm %ir %asmbinary %asminclude %breakpoint %import %launcher %option %output %zeropage %zpreserved inline sub asmsub romsub clobbers asm if when else if_cc if_cs if_eq if_mi if_neg if_nz if_pl if_pos if_vc if_vs if_z for in step do while repeat unroll break return goto - abs all any callfar cmp len lsb lsl lsr memory mkword msb peek peekw poke pokew push pushw pop popw rsave rsavex rrestore rrestorex reverse rnd rndw rol rol2 ror ror2 sgn sizeof sort sqrt16 swap + abs all any callfar cmp len lsb lsl lsr memory mkword msb peek peekw poke pokew push pushw pop popw rsave rsavex rrestore rrestorex reverse rnd rndw rol rol2 ror ror2 sgn sizeof sort sqrtw swap true false not and or xor as to downto |> diff --git a/syntax-files/Vim/prog8_builtins.vim b/syntax-files/Vim/prog8_builtins.vim index 8b363dbfb..cd4e897c1 100644 --- a/syntax-files/Vim/prog8_builtins.vim +++ b/syntax-files/Vim/prog8_builtins.vim @@ -7,7 +7,7 @@ " Built-in functions " Math functions -syn keyword prog8BuiltInFunc sgn sqrt16 +syn keyword prog8BuiltInFunc sgn sqrtw " Array functions syn keyword prog8BuiltInFunc any all len reverse sort